diff --git a/cmd/removeRepo.go b/cmd/removeRepo.go index d2f7003..3201bde 100644 --- a/cmd/removeRepo.go +++ b/cmd/removeRepo.go @@ -45,7 +45,10 @@ var removeCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { PrintMsg("Removing repository...") - mod := repositories.RemoveRepository(args[0]) + mod, err := repositories.RemoveRepository(args[0]) + if err != nil { + PrintError("unable to get registry modifier: %s \n", err) + } if err := repositories.UpdateRegistry(mod); err != nil { PrintError("unable to update repository registry: %s \n", err) } diff --git a/go.mod b/go.mod index de99358..6a8ab43 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,6 @@ require ( github.com/coreos/etcd v3.3.10+incompatible github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f - github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect - github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 // indirect - github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 // indirect - github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect github.com/fatih/color v1.9.0 github.com/fatih/structs v1.1.0 github.com/go-git/go-git/v5 v5.1.0 diff --git a/go.sum b/go.sum index b286607..abf61d3 100644 --- a/go.sum +++ b/go.sum @@ -64,14 +64,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= -github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw= -github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA= -github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 h1:mOp33BLbcbJ8fvTAmZacbBiOASfxN+MLcLxymZCIrGE= -github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434/go.mod h1:KigFdumBXUPSwzLDbeuzyt0elrL7+CP7TKuhrhT4bcU= -github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 h1:nXeeRHmgNgjLxi+7dY9l9aDvSS1uwVlNLqUWIY4Ath0= -github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2/go.mod h1:TUV/fX3XrTtBQb5+ttSUJzcFgLNpILONFTKmBuk5RSw= -github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 h1:0YtRCqIZs2+Tz49QuH6cJVw/IFqzo39gEqZ0iYLxD2M= -github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4/go.mod h1:vsJz7uE339KUCpBXx3JAJzSRH7Uk4iGGyJzR529qDIA= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= @@ -273,12 +265,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= -github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= -github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= -github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= -github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= -github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/pkg/api/api.go b/pkg/api/api.go index 4a42416..3286258 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -17,6 +17,7 @@ func RegisterHandlersV1(e *echo.Group, serv *Serv) { e.GET(RepositoriesApiPath, serv.GetRepositories) e.GET(RepositoriesApiPath+"/:id", serv.GetRepository) e.PUT(RepositoriesApiPath+"/:id", serv.PutRepository) + e.DELETE(RepositoriesApiPath+"/:id", serv.DeleteRepository) e.GET(RepositoriesApiPath+"/:id"+ConceptsApiPath, serv.GetRepositoryConcepts) e.GET(RepositoriesApiPath+"/:id"+ConceptsApiPath+"/:path", serv.GetRepositoryConcept) } diff --git a/pkg/api/client/v1/concepts_test.go b/pkg/api/client/v1/concepts_test.go index 292447e..3b61466 100644 --- a/pkg/api/client/v1/concepts_test.go +++ b/pkg/api/client/v1/concepts_test.go @@ -38,44 +38,82 @@ func TestConceptsClient_List_None(t *testing.T) { assert.Equal(t, api.NewConceptsPayload(), *concepts) } -func TestConceptsClient_List_Some(t *testing.T) { - client, err := addDemoHttps(t) +func TestConceptsClient_List_All(t *testing.T) { + removeMod, client, err := addDemoHttps(t) assert.NoError(t, err) concepts, response, err := client.Concepts.List(context.Background(), nil) assert.NoError(t, err) assert.Equal(t, 200, response.StatusCode) - act := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"apps/grafana@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "kuehnert.ralph@gmail.com"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "apps/sentry@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "kuehnert.ralph@gmail.com"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}} - assert.Equal(t, act, *concepts) + exp := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"e2e-test/testconcept1@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "e2e-test/testconcept2@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}} + assert.Equal(t, exp, *concepts) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) } -func TestConceptsClient_ListFromRepository_Some(t *testing.T) { - client, err := addDemoHttps(t) +func TestConceptsClient_ListFromRepository_All(t *testing.T) { + removeMod, client, err := addDemoHttps(t) assert.NoError(t, err) concepts, response, err := client.Concepts.ListFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, nil) assert.NoError(t, err) assert.Equal(t, 200, response.StatusCode) - act := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"apps/grafana@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "kuehnert.ralph@gmail.com"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "apps/sentry@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "kuehnert.ralph@gmail.com"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}} - assert.Equal(t, act, *concepts) + exp := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"e2e-test/testconcept1@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "e2e-test/testconcept2@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}} + assert.Equal(t, exp, *concepts) + + concepts, response, err = client.Concepts.ListFromRepository(context.Background(), "dummyname", nil) + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) +} + +func TestConceptsClient_GetFromRepository_All(t *testing.T) { + removeMod, client, err := addDemoHttps(t) + assert.NoError(t, err) + + concepts, response, err := client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "e2e-test/testconcept1") + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + exp := api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}} + assert.Equal(t, exp, *concepts) + + concepts, response, err = client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "e2e-test/testconcept") + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + + concepts, response, err = client.Concepts.GetFromRepository(context.Background(), "dummyname", "e2e-test/testconcept1") + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) } -func TestConceptsClient_GetFromRepository_Some(t *testing.T) { - client, err := addDemoHttps(t) +func TestConceptsClient_Get(t *testing.T) { + removeMod, client, err := addDemoHttps(t) assert.NoError(t, err) - concepts, response, err := client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "apps/sentry") + concepts, response, err := client.Concepts.Get(context.Background(), "e2e-test/testconcept1@demo-https") assert.NoError(t, err) assert.Equal(t, 200, response.StatusCode) - act := api.ConceptPayload(api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "kuehnert.ralph@gmail.com"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}) - assert.Equal(t, act, *concepts) + exp := api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "demo.maintainer@kab.le"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}} + assert.Equal(t, exp, *concepts) + + concepts, response, err = client.Concepts.Get(context.Background(), "e2e-test/testconcept@demo-https") + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + + concepts, response, err = client.Concepts.Get(nil, "e2e-test/testconcept@demo-https") + assert.Error(t, err) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) } ///////////// // HELPERS // ///////////// -func addDemoHttps(t *testing.T) (*Client, error) { +func addDemoHttps(t *testing.T) (repositories.RegistryModification, *Client, error) { viper.Set(repositories.StoreKey, repositories.MockStoreConfigMap().Map()) client := NewClient(nil, &uri) @@ -83,5 +121,7 @@ func addDemoHttps(t *testing.T) (*Client, error) { addMod, err := repositories.AddRepository(repositories.DemoHttpsRepository) assert.NoError(t, err) assert.NoError(t, repositories.UpdateRegistry(addMod)) - return client, err + removeMod, err := repositories.RemoveRepository(repositories.DemoHttpsRepository.Name) + assert.NoError(t, err) + return removeMod, client, err } diff --git a/pkg/api/client/v1/repositories.go b/pkg/api/client/v1/repositories.go index 3be9863..74b4cb6 100644 --- a/pkg/api/client/v1/repositories.go +++ b/pkg/api/client/v1/repositories.go @@ -2,48 +2,129 @@ package v1 import ( "context" + "net/http" + "net/url" + "github.com/google/go-querystring/query" "github.com/redradrat/kable/pkg/api" - "github.com/redradrat/kable/pkg/concepts" ) const repositoriessBasePath = "v1/repositories" type RepositoriesService interface { - List(context.Context, *ListOptions) (api.ConceptsPayload, *Response, error) - ListByTag(context.Context, string, *ListOptions) (api.ConceptsPayload, *Response, error) - Get(context.Context, concepts.ConceptIdentifier) (api.ConceptPayload, *Response, error) - GetFromRepository(context.Context, string, string) (api.ConceptPayload, *Response, error) - Delete(context.Context, int) (*Response, error) - DeleteByTag(context.Context, string) (*Response, error) + List(ctx context.Context, options *ListOptions) (*api.RepositoriesPayload, *Response, error) + Get(ctx context.Context, name string) (*api.RepositoryPayload, *Response, error) + Put(ctx context.Context, name, giturl, gitref string) (*Response, error) + Delete(ctx context.Context, name string) (*Response, error) } var _ RepositoriesService = &RepositoriesClient{} type RepositoriesClient struct { - *Client + client *Client } -func (r RepositoriesClient) List(ctx context.Context, options *ListOptions) (api.ConceptsPayload, *Response, error) { - panic("implement me") -} +func (r RepositoriesClient) List(ctx context.Context, options *ListOptions) (*api.RepositoriesPayload, *Response, error) { + uri, err := url.Parse(repositoriessBasePath) + if err != nil { + return nil, nil, err + } -func (r RepositoriesClient) ListByTag(ctx context.Context, s string, options *ListOptions) (api.ConceptsPayload, *Response, error) { - panic("implement me") -} + vals, err := query.Values(options) + if err != nil { + return nil, nil, err + } + + uri.RawQuery = vals.Encode() + + req, err := r.client.NewRequest(http.MethodGet, uri.String(), nil) + if err != nil { + return nil, nil, err + } + + payload := api.NewRepositoriesPayload() + resp, err := r.client.Do(ctx, req, &payload) + if err != nil { + return nil, resp, err + } -func (r RepositoriesClient) Get(ctx context.Context, identifier concepts.ConceptIdentifier) (api.ConceptPayload, *Response, error) { - panic("implement me") + return &payload, resp, err } -func (r RepositoriesClient) GetFromRepository(ctx context.Context, s string, s2 string) (api.ConceptPayload, *Response, error) { - panic("implement me") +func (r RepositoriesClient) Get(ctx context.Context, name string) (*api.RepositoryPayload, *Response, error) { + uri, err := url.Parse(repositoriessBasePath + "/") + if err != nil { + return nil, nil, err + } + + getUri, err := uri.Parse(name) + if err != nil { + return nil, nil, err + } + + req, err := r.client.NewRequest(http.MethodGet, getUri.String(), nil) + if err != nil { + return nil, nil, err + } + + payload := api.RepositoryPayload{} + resp, err := r.client.Do(ctx, req, &payload) + if err != nil { + return nil, resp, err + } + + return &payload, resp, err } -func (r RepositoriesClient) Delete(ctx context.Context, i int) (*Response, error) { - panic("implement me") +func (r RepositoriesClient) Delete(ctx context.Context, name string) (*Response, error) { + uri, err := url.Parse(repositoriessBasePath + "/") + if err != nil { + return nil, err + } + + getUri, err := uri.Parse(name) + if err != nil { + return nil, err + } + + req, err := r.client.NewRequest(http.MethodDelete, getUri.String(), nil) + if err != nil { + return nil, err + } + + resp, err := r.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, err } -func (r RepositoriesClient) DeleteByTag(ctx context.Context, s string) (*Response, error) { - panic("implement me") +func (r RepositoriesClient) Put(ctx context.Context, name, giturl, gitref string) (*Response, error) { + uri, err := url.Parse(repositoriessBasePath + "/") + if err != nil { + return nil, err + } + + getUri, err := uri.Parse(name) + if err != nil { + return nil, err + } + + payload := api.RepositoryPayload{ + URL: giturl, + GitRef: gitref, + } + + req, err := r.client.NewRequest(http.MethodPut, getUri.String(), payload) + if err != nil { + return nil, err + } + + resp, err := r.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, err } diff --git a/pkg/api/client/v1/repositories_test.go b/pkg/api/client/v1/repositories_test.go new file mode 100644 index 0000000..62689e9 --- /dev/null +++ b/pkg/api/client/v1/repositories_test.go @@ -0,0 +1,85 @@ +package v1 + +import ( + "context" + "strings" + "testing" + + "github.com/redradrat/kable/pkg/api" + "github.com/redradrat/kable/pkg/repositories" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestRepositoriesClient_List_None(t *testing.T) { + viper.Set(repositories.StoreKey, repositories.MockStoreConfigMap().Map()) + client := NewClient(nil, &uri) + + repos, response, err := client.Repositories.List(context.Background(), nil) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + exp := api.NewRepositoriesPayload() + assert.Equal(t, exp, *repos) +} + +func TestRepositoriesClient_List_All(t *testing.T) { + removeMod, client, err := addDemoHttps(t) + assert.NoError(t, err) + + repos, response, err := client.Repositories.List(context.Background(), nil) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + exp := api.RepositoriesPayload(api.RepositoriesPayload{Repositories: api.RepositoriesMapPayload{"demo-https": api.RepositoryPayload{URL: "https://github.com/redradrat/kable", GitRef: "refs/heads/master"}}}) + assert.Equal(t, exp, *repos) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) +} + +func TestRepositoriesClient_Get(t *testing.T) { + removeMod, client, err := addDemoHttps(t) + assert.NoError(t, err) + + concepts, response, err := client.Repositories.Get(context.Background(), repositories.DemoHttpsRepository.Name) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + exp := api.RepositoryPayload(api.RepositoryPayload{URL: strings.TrimSuffix(repositories.DemoHttpsRepository.URL, ".git"), GitRef: repositories.DemoHttpsRepository.GitRef}) + assert.Equal(t, exp, *concepts) + + concepts, response, err = client.Repositories.Get(context.Background(), "dummyname") + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + + concepts, response, err = client.Repositories.Get(nil, repositories.DemoHttpsRepository.Name) + assert.Error(t, err) + + assert.NoError(t, repositories.UpdateRegistry(removeMod)) +} + +func TestRepositoriesClient_Delete(t *testing.T) { + _, client, err := addDemoHttps(t) + assert.NoError(t, err) + + response, err := client.Repositories.Delete(context.Background(), repositories.DemoHttpsRepository.Name) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + + _, response, err = client.Repositories.Get(context.Background(), repositories.DemoHttpsRepository.Name) + assert.Error(t, err) + assert.Equal(t, 404, response.StatusCode) + +} + +func TestRepositoriesClient_Put(t *testing.T) { + viper.Set(repositories.StoreKey, repositories.MockStoreConfigMap().Map()) + client := NewClient(nil, &uri) + + response, err := client.Repositories.Put(context.Background(), repositories.DemoHttpsRepository.Name, repositories.DemoHttpsRepository.URL, repositories.DemoHttpsRepository.GitRef) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + + concepts, response, err := client.Repositories.Get(context.Background(), repositories.DemoHttpsRepository.Name) + assert.NoError(t, err) + assert.Equal(t, 200, response.StatusCode) + exp := api.RepositoryPayload(api.RepositoryPayload{URL: strings.TrimSuffix(repositories.DemoHttpsRepository.URL, ".git"), GitRef: repositories.DemoHttpsRepository.GitRef}) + assert.Equal(t, exp, *concepts) +} diff --git a/pkg/api/server.go b/pkg/api/server.go index ec8dd66..0041064 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -33,11 +33,9 @@ func (serv Serv) GetRepository(ctx echo.Context) error { if err != nil { return err } - payload := RepositoriesMapPayload{ - id: { - URL: repo.URL, - GitRef: repo.GitRef, - }, + payload := RepositoryPayload{ + URL: repo.URL, + GitRef: repo.GitRef, } return ctx.JSON(http.StatusOK, payload) } @@ -79,9 +77,24 @@ func (serv Serv) PutRepository(ctx echo.Context) error { ctx.Logger().Errorf("error updating registry: %v", err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("error updating registry: %v", err)) } + return ctx.JSON(http.StatusOK, NewMessage("Successfully added repository '%s'", name)) } +func (serv Serv) DeleteRepository(ctx echo.Context) error { + name := ctx.Param("id") + removeMod, err := repositories.RemoveRepository(name) + if err != nil { + ctx.Logger().Errorf("unable to add repository: %v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("unable to remove repository: %s", err)) + } + if err := repositories.UpdateRegistry(removeMod); err != nil { + ctx.Logger().Errorf("error updating registry: %v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("error updating registry: %v", err)) + } + return ctx.JSON(http.StatusOK, NewMessage("Successfully removed repository '%s'", name)) +} + func (serv Serv) GetRepositories(ctx echo.Context) error { repos, err := repositories.ListRepositories() if err != nil { diff --git a/pkg/repositories/repositories.go b/pkg/repositories/repositories.go index 76c64f9..986ba70 100644 --- a/pkg/repositories/repositories.go +++ b/pkg/repositories/repositories.go @@ -98,14 +98,18 @@ func AddRepository(repo Repository) (RegistryModification, error) { return addrepofunc, nil } -func RemoveRepository(name string) RegistryModification { - return func(registry RepoRegistry) RepoRegistry { +func RemoveRepository(name string) (RegistryModification, error) { + if !IsValidRepositoryName(name) { + return nil, fmt.Errorf("repository name can only be lowercase letters (a-z) and '-'") + } + removeFunc := func(registry RepoRegistry) RepoRegistry { if registry.Repositories == nil { return registry } delete(registry.Repositories, name) return registry } + return removeFunc, nil } func UpdateRegistry(updates ...RegistryModification) error { diff --git a/vendor/modules.txt b/vendor/modules.txt index beeedbb..d1654df 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -33,14 +33,6 @@ github.com/emirpasic/gods/lists/arraylist github.com/emirpasic/gods/trees github.com/emirpasic/gods/trees/binaryheap github.com/emirpasic/gods/utils -# github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a -## explicit -# github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 -## explicit -# github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 -## explicit -# github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 -## explicit # github.com/fatih/color v1.9.0 ## explicit github.com/fatih/color