From 6c84ae7d3a941a367565eb5400ab39882ffd79dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Tue, 19 May 2020 12:09:37 +0200 Subject: [PATCH] feat(registry): add full name support for tag and list on list and get --- internal/namespaces/registry/v1/custom.go | 6 + .../namespaces/registry/v1/custom_image.go | 106 ++++++++++++++++++ internal/namespaces/registry/v1/custom_tag.go | 104 +++++++++++++++++ 3 files changed, 216 insertions(+) diff --git a/internal/namespaces/registry/v1/custom.go b/internal/namespaces/registry/v1/custom.go index 45083bc7c6..46f3e75be1 100644 --- a/internal/namespaces/registry/v1/custom.go +++ b/internal/namespaces/registry/v1/custom.go @@ -19,6 +19,12 @@ func GetCommands() *core.Commands { registryLogoutCommand(), )) + cmds.MustFind("registry", "tag", "get").Override(tagGetBuilder) + cmds.MustFind("registry", "tag", "list").Override(tagListBuilder) + + cmds.MustFind("registry", "image", "get").Override(imageGetBuilder) + cmds.MustFind("registry", "image", "list").Override(imageListBuilder) + human.RegisterMarshalerFunc(registry.NamespaceStatus(0), human.EnumMarshalFunc(namespaceStatusMarshalSpecs)) human.RegisterMarshalerFunc(registry.ImageStatus(0), human.EnumMarshalFunc(imageStatusMarshalSpecs)) human.RegisterMarshalerFunc(registry.TagStatus(0), human.EnumMarshalFunc(tagStatusMarshalSpecs)) diff --git a/internal/namespaces/registry/v1/custom_image.go b/internal/namespaces/registry/v1/custom_image.go index 5d58814afb..401d9890ca 100644 --- a/internal/namespaces/registry/v1/custom_image.go +++ b/internal/namespaces/registry/v1/custom_image.go @@ -1,9 +1,14 @@ package registry import ( + "context" + "fmt" + "github.com/fatih/color" + "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-cli/internal/human" "github.com/scaleway/scaleway-sdk-go/api/registry/v1" + "github.com/scaleway/scaleway-sdk-go/scw" ) // @@ -19,3 +24,104 @@ var ( registry.ImageStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue}, } ) + +type customImage struct { + registry.Image + FullName string +} + +func imageGetBuilder(c *core.Command) *core.Command { + + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + getImageResp, err := runner(ctx, argsI) + image := getImageResp.(*registry.Image) + + client := core.ExtractClient(ctx) + api := registry.NewAPI(client) + + namespace, err := api.GetNamespace(®istry.GetNamespaceRequest{ + NamespaceID: image.NamespaceID, + }) + if err != nil { + return getImageResp, nil + } + + res := customImage{ + Image: *image, + FullName: fmt.Sprintf("%s/%s", namespace.Endpoint, image.Name), + } + + return res, nil + } + + return c +} + +func imageListBuilder(c *core.Command) *core.Command { + + c.View = &core.View{ + Fields: []*core.ViewField{ + { + Label: "ID", + FieldName: "ID", + }, + { + Label: "Full Name", + FieldName: "FullName", + }, + { + Label: "Size", + FieldName: "Size", + }, + { + Label: "Visibility", + FieldName: "Visibility", + }, + { + Label: "Status", + FieldName: "Status", + }, + { + Label: "Created At", + FieldName: "CreatedAt", + }, + { + Label: "Updated At", + FieldName: "UpdatedAt", + }, + }, + } + + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + listImageResp, err := runner(ctx, argsI) + if err != nil { + return listImageResp, err + } + listImage := listImageResp.([]*registry.Image) + + client := core.ExtractClient(ctx) + api := registry.NewAPI(client) + + namespaces, err := api.ListNamespaces(®istry.ListNamespacesRequest{}, scw.WithAllPages()) + if err != nil { + return listImageResp, err + } + + namespaceEndpointByID := make(map[string]string) + for _, namespace := range namespaces.Namespaces { + namespaceEndpointByID[namespace.ID] = namespace.Endpoint + } + + var customRes []customImage + for _, image := range listImage { + customRes = append(customRes, customImage{ + Image: *image, + FullName: fmt.Sprintf("%s/%s", namespaceEndpointByID[image.NamespaceID], image.Name), + }) + } + + return customRes, nil + } + + return c +} diff --git a/internal/namespaces/registry/v1/custom_tag.go b/internal/namespaces/registry/v1/custom_tag.go index 33cfe6cf10..f2ebb66e54 100644 --- a/internal/namespaces/registry/v1/custom_tag.go +++ b/internal/namespaces/registry/v1/custom_tag.go @@ -1,7 +1,11 @@ package registry import ( + "context" + "fmt" + "github.com/fatih/color" + "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-cli/internal/human" "github.com/scaleway/scaleway-sdk-go/api/registry/v1" ) @@ -19,3 +23,103 @@ var ( registry.TagStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue}, } ) + +type customTag struct { + registry.Tag + FullName string +} + +func tagGetBuilder(c *core.Command) *core.Command { + + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + getTagResp, err := runner(ctx, argsI) + tag := getTagResp.(*registry.Tag) + + client := core.ExtractClient(ctx) + api := registry.NewAPI(client) + + image, err := api.GetImage(®istry.GetImageRequest{ + ImageID: tag.ImageID, + }) + if err != nil { + return getTagResp, nil + } + + namespace, err := api.GetNamespace(®istry.GetNamespaceRequest{ + NamespaceID: image.NamespaceID, + }) + if err != nil { + return getTagResp, nil + } + + res := customTag{ + Tag: *tag, + FullName: fmt.Sprintf("%s/%s:%s", namespace.Endpoint, image.Name, tag.Name), + } + + return res, nil + } + + return c +} + +func tagListBuilder(c *core.Command) *core.Command { + + c.View = &core.View{ + Fields: []*core.ViewField{ + { + Label: "ID", + FieldName: "ID", + }, + { + Label: "Full Name", + FieldName: "FullName", + }, + { + Label: "Status", + FieldName: "Status", + }, + }, + } + + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + listTagResp, err := runner(ctx, argsI) + if err != nil { + return listTagResp, err + } + + client := core.ExtractClient(ctx) + api := registry.NewAPI(client) + + request := argsI.(*registry.ListTagsRequest) + image, err := api.GetImage(®istry.GetImageRequest{ + ImageID: request.ImageID, + }) + if err != nil { + return listTagResp, err + } + + namespace, err := api.GetNamespace(®istry.GetNamespaceRequest{ + NamespaceID: image.NamespaceID, + }) + if err != nil { + return listTagResp, err + } + + var customRes []customTag + for _, tag := range listTagResp.([]*registry.Tag) { + customRes = append(customRes, customTag{ + Tag: *tag, + FullName: fmt.Sprintf("%s/%s:%s", + namespace.Endpoint, + image.Name, + tag.Name, + ), + }) + } + + return customRes, nil + } + + return c +}