Initial commit

This commit is contained in:
Correl Roush 2018-01-17 14:23:16 -05:00
commit d3c60f8620
6 changed files with 253 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
elm.js
elm-stuff/
node_modules/

33
Makefile Normal file
View file

@ -0,0 +1,33 @@
.PHONY: all node-deps clean run
TARGET=elm.js
SOURCE=src/App.elm
ELM_FILES = $(shell find . -type f -name '*.elm')
NODE_BIN = ./node_modules/.bin
ELM-MAKE = $(NODE_BIN)/elm-make
ELM-REACTOR = $(NODE_BIN)/elm-reactor
ELMMAKE_FLAGS = --yes
ifeq ($(DEBUG),1)
ELMMAKE_FLAGS += --debug
endif
all: node-deps $(TARGET)
node-deps:
npm i
$(TARGET): $(ELM_FILES)
$(ELM-MAKE) $(ELMMAKE_FLAGS) $(SOURCE) --output $@
clean-deps:
rm -rf elm-stuff
rm -rf node_modules
clean:
rm -f $(TARGET)
rm -rf elm-stuff/build-artifacts
run: all
$(ELM-REACTOR)

19
elm-package.json Normal file
View file

@ -0,0 +1,19 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"src"
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
"elm-community/maybe-extra": "4.0.0 <= v < 5.0.0",
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"krisajenkins/remotedata": "4.3.3 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}

7
package.json Normal file
View file

@ -0,0 +1,7 @@
{
"name": "elm-pass",
"version": "1.0.0",
"dependencies": {
"elm": "0.18"
}
}

90
src/App.elm Normal file
View file

@ -0,0 +1,90 @@
module App exposing (..)
import Gitlab
import Html
import RemoteData
repo : Gitlab.Repo
repo =
Gitlab.Repo "correlr" "password-store"
client : Gitlab.Client
client =
Gitlab.Client "http://git.phoenixinquis.net" (Just "AuGrRWBdXfSJbm36itUG")
type alias Model =
{ objects : RemoteData.WebData (List Gitlab.Object) }
type Msg
= GetObjects
| GotObjects (RemoteData.WebData (List Gitlab.Object))
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
init : ( Model, Cmd Msg )
init =
( { objects = RemoteData.NotAsked }
, getObjects
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotObjects objects ->
( { model | objects = objects }
, Cmd.none
)
_ ->
( model, Cmd.none )
view : Model -> Html.Html Msg
view model =
Html.body []
[ Html.h1 [] [ Html.text "Files" ]
, viewFiles model.objects
]
viewFiles : RemoteData.WebData (List Gitlab.Object) -> Html.Html Msg
viewFiles data =
case data of
RemoteData.NotAsked ->
Html.div [] [ Html.text "Not loaded." ]
RemoteData.Loading ->
Html.div [] [ Html.text "Loading..." ]
RemoteData.Failure _ ->
Html.div [] [ Html.text "Whoops." ]
RemoteData.Success files ->
Html.ul [] <|
List.map (\x -> Html.li [] [ viewFile x ]) files
viewFile : Gitlab.Object -> Html.Html Msg
viewFile file =
Html.text file.path
getObjects : Cmd Msg
getObjects =
Gitlab.getFiles repo client
|> RemoteData.sendRequest
|> Cmd.map GotObjects

101
src/Gitlab.elm Normal file
View file

@ -0,0 +1,101 @@
module Gitlab exposing (..)
import Http
import Json.Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (decode, required)
import Maybe.Extra
type Msg
= FetchFiles
| AddFiles (Result Http.Error (List Object))
type alias Client =
{ url : String
, token : Maybe String
}
type alias Repo =
{ owner : String
, name : String
}
type ObjectType
= Tree
| Blob
type alias Object =
{ id : String
, name : String
, objectType : ObjectType
, path : String
}
type alias Path =
List String
request : String -> Path -> Decoder a -> Client -> Http.Request a
request method path decoder client =
let
url =
String.join "/" <|
client.url
:: "api/v4"
:: path
in
Http.request
{ method = method
, headers =
Maybe.Extra.values
[ Maybe.map (Http.header "Private-Token") client.token ]
, url = url
, body = Http.emptyBody
, expect = Http.expectJson decoder
, timeout = Maybe.Nothing
, withCredentials = False
}
get : Path -> Decoder a -> Client -> Http.Request a
get =
request "GET"
getFiles : Repo -> Client -> Http.Request (List Object)
getFiles repo client =
let
typeFromString s =
case s of
"tree" ->
Tree
"blob" ->
Blob
_ ->
Tree
decoder =
Json.Decode.list
(decode Object
|> required "id" Json.Decode.string
|> required "name" Json.Decode.string
|> required "type" (Json.Decode.map typeFromString Json.Decode.string)
|> required "path" Json.Decode.string
)
|> Json.Decode.map (List.filter (\x -> x.objectType == Blob))
in
get
[ "projects"
, Http.encodeUri (repo.owner ++ "/" ++ repo.name)
, "repository"
, "tree?recursive=true&per_page=100&page=2"
]
decoder
client