Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(registry): add full name support for tag and list on list and get #1014

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/namespaces/registry/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
106 changes: 106 additions & 0 deletions internal/namespaces/registry/v1/custom_image.go
Original file line number Diff line number Diff line change
@@ -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"
)

//
Expand All @@ -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(&registry.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(&registry.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
}
104 changes: 104 additions & 0 deletions internal/namespaces/registry/v1/custom_tag.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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(&registry.GetImageRequest{
ImageID: tag.ImageID,
})
if err != nil {
return getTagResp, nil
}

namespace, err := api.GetNamespace(&registry.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(&registry.GetImageRequest{
ImageID: request.ImageID,
})
if err != nil {
return listTagResp, err
}

namespace, err := api.GetNamespace(&registry.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
}