From d3c60f862050f29b96fc5af07d04a05e46e0766b Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 17 Jan 2018 14:23:16 -0500 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ Makefile | 33 ++++++++++++++++ elm-package.json | 19 +++++++++ package.json | 7 ++++ src/App.elm | 90 +++++++++++++++++++++++++++++++++++++++++ src/Gitlab.elm | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 elm-package.json create mode 100644 package.json create mode 100644 src/App.elm create mode 100644 src/Gitlab.elm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75b0e64 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +elm.js +elm-stuff/ +node_modules/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8a479de --- /dev/null +++ b/Makefile @@ -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) diff --git a/elm-package.json b/elm-package.json new file mode 100644 index 0000000..4cd56bd --- /dev/null +++ b/elm-package.json @@ -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" +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0e57575 --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "elm-pass", + "version": "1.0.0", + "dependencies": { + "elm": "0.18" + } +} diff --git a/src/App.elm b/src/App.elm new file mode 100644 index 0000000..96976dd --- /dev/null +++ b/src/App.elm @@ -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 diff --git a/src/Gitlab.elm b/src/Gitlab.elm new file mode 100644 index 0000000..d5bbbe3 --- /dev/null +++ b/src/Gitlab.elm @@ -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