From 5f05b159fb49b4f42d62bc7d8ee0be23d618cefb Mon Sep 17 00:00:00 2001 From: Laure-di <62625835+Laure-di@users.noreply.github.com> Date: Wed, 9 Oct 2024 01:08:40 -0700 Subject: [PATCH] fix(object): disable command in console (#4151) --- internal/namespaces/object/v1/custom.go | 65 +++++---- .../namespaces/object/v1/custom_bucket.go | 112 +--------------- .../namespaces/object/v1/custom_command.go | 30 +++++ .../namespaces/object/v1/custom_config_get.go | 2 + .../object/v1/custom_config_install.go | 2 + .../namespaces/object/v1/custom_disabled.go | 45 +++++++ .../namespaces/object/v1/custom_marshaler.go | 124 ++++++++++++++++++ internal/namespaces/object/v1/s3_helpers.go | 7 +- internal/namespaces/object/v1/s3configfile.go | 2 + 9 files changed, 240 insertions(+), 149 deletions(-) create mode 100644 internal/namespaces/object/v1/custom_command.go create mode 100644 internal/namespaces/object/v1/custom_disabled.go create mode 100644 internal/namespaces/object/v1/custom_marshaler.go diff --git a/internal/namespaces/object/v1/custom.go b/internal/namespaces/object/v1/custom.go index d3d96229a..b3f19b1e4 100644 --- a/internal/namespaces/object/v1/custom.go +++ b/internal/namespaces/object/v1/custom.go @@ -7,46 +7,43 @@ import ( ) func GetCommands() *core.Commands { + cmds := core.NewCommands() + human.RegisterMarshalerFunc(BucketResponse{}, bucketResponseMarshalerFunc) human.RegisterMarshalerFunc(bucketInfo{}, bucketInfoMarshalerFunc) human.RegisterMarshalerFunc(BucketGetResult{}, bucketGetResultMarshalerFunc) human.RegisterMarshalerFunc(s3.ListBucketsOutput{}.Buckets, bucketMarshalerFunc) - return core.NewCommands( - objectRoot(), - objectConfig(), - objectBucket(), - bucketCreateCommand(), - bucketDeleteCommand(), - bucketGetCommand(), - bucketListCommand(), - bucketUpdateCommand(), - configGetCommand(), - configInstallCommand(), - ) -} - -func objectRoot() *core.Command { - return &core.Command{ - Short: `Object-storage utils`, - Namespace: "object", + if cmdObjectRoot := objectRoot(); cmdObjectRoot != nil { + cmds.Add(cmdObjectRoot) } -} - -func objectConfig() *core.Command { - return &core.Command{ - Short: `Manage configuration files for popular S3 tools`, - Long: `Configuration generation for S3 tools.`, - Namespace: "object", - Resource: `config`, + if cmdObjectConfig := objectConfig(); cmdObjectConfig != nil { + cmds.Add(cmdObjectConfig) } -} - -func objectBucket() *core.Command { - return &core.Command{ - Short: `Manage S3 buckets`, - Long: `Manage S3 buckets creation, deletion and updates to properties like tags, ACL and versioning.`, - Namespace: "object", - Resource: `bucket`, + if cmdObjectBucket := objectBucket(); cmdObjectBucket != nil { + cmds.Add(cmdObjectBucket) + } + if cmdBucketCreate := bucketCreateCommand(); cmdBucketCreate != nil { + cmds.Add(cmdBucketCreate) } + if cmdBucketDelete := bucketDeleteCommand(); cmdBucketDelete != nil { + cmds.Add(cmdBucketDelete) + } + if cmdBucketGet := bucketGetCommand(); cmdBucketGet != nil { + cmds.Add(cmdBucketGet) + } + if cmdBucketList := bucketListCommand(); cmdBucketList != nil { + cmds.Add(cmdBucketList) + } + if cmdBucketUpdate := bucketUpdateCommand(); cmdBucketUpdate != nil { + cmds.Add(cmdBucketUpdate) + } + if cmdConfigGet := configGetCommand(); cmdConfigGet != nil { + cmds.Add(cmdConfigGet) + } + if cmdConfigInstall := configInstallCommand(); cmdConfigInstall != nil { + cmds.Add(cmdConfigInstall) + } + + return cmds } diff --git a/internal/namespaces/object/v1/custom_bucket.go b/internal/namespaces/object/v1/custom_bucket.go index 75cc1a0d9..070a84ac4 100644 --- a/internal/namespaces/object/v1/custom_bucket.go +++ b/internal/namespaces/object/v1/custom_bucket.go @@ -1,3 +1,5 @@ +//go:build darwin || linux || windows + package object import ( @@ -11,66 +13,9 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/scaleway/scaleway-cli/v2/core" - "github.com/scaleway/scaleway-cli/v2/internal/human" "github.com/scaleway/scaleway-sdk-go/scw" ) -type bucketInfo struct { - ID string - Region scw.Region - APIEndpoint string - BucketEndpoint string - EnableVersioning bool - Tags []types.Tag - ACL []CustomS3ACLGrant - Owner string -} - -func bucketInfoMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { - // To avoid recursion of human.Marshal we create a dummy type - type tmp bucketInfo - info := tmp(i.(bucketInfo)) - - opt.Sections = []*human.MarshalSection{ - { - FieldName: "Tags", - HideIfEmpty: true, - }, - { - FieldName: "ACL", - HideIfEmpty: true, - }, - } - str, err := human.Marshal(info, opt) - if err != nil { - return "", err - } - return str, nil -} - -type BucketResponse struct { - SuccessResult *core.SuccessResult - BucketInfo *bucketInfo -} - -func bucketResponseMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { - resp := i.(BucketResponse) - - messageStr, err := resp.SuccessResult.MarshalHuman() - if err != nil { - return "", err - } - bucketStr, err := bucketInfoMarshalerFunc(*resp.BucketInfo, opt) - if err != nil { - return "", err - } - - return strings.Join([]string{ - messageStr, - bucketStr, - }, "\n"), nil -} - type bucketConfigArgs struct { Region scw.Region Name string @@ -205,59 +150,6 @@ func bucketDeleteCommand() *core.Command { } } -type bucketGetArgs struct { - Region scw.Region - Name string - WithSize bool `json:"with-size"` -} - -type BucketGetResult struct { - *bucketInfo - Size *scw.Size - NbObjects *int64 - NbParts *int64 -} - -func bucketGetResultMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { - type tmp BucketGetResult - result := tmp(i.(BucketGetResult)) - opt.Sections = []*human.MarshalSection{ - { - FieldName: "Tags", - HideIfEmpty: true, - }, - { - FieldName: "ACL", - HideIfEmpty: true, - }, - } - str, err := human.Marshal(result, opt) - if err != nil { - return "", err - } - return str, nil -} - -func bucketMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { - type tmp []types.Bucket - result := tmp(i.([]types.Bucket)) - opt.Fields = []*human.MarshalFieldOpt{ - { - FieldName: "Name", - Label: "Name", - }, - { - FieldName: "CreationDate", - Label: "Creation Date", - }, - } - str, err := human.Marshal(result, opt) - if err != nil { - return "", err - } - return str, nil -} - func bucketGetCommand() *core.Command { return &core.Command{ Namespace: "object", diff --git a/internal/namespaces/object/v1/custom_command.go b/internal/namespaces/object/v1/custom_command.go new file mode 100644 index 000000000..5942d6e2a --- /dev/null +++ b/internal/namespaces/object/v1/custom_command.go @@ -0,0 +1,30 @@ +//go:build darwin || linux || windows + +package object + +import "github.com/scaleway/scaleway-cli/v2/core" + +func objectRoot() *core.Command { + return &core.Command{ + Short: `Object-storage utils`, + Namespace: "object", + } +} + +func objectConfig() *core.Command { + return &core.Command{ + Short: `Manage configuration files for popular S3 tools`, + Long: `Configuration generation for S3 tools.`, + Namespace: "object", + Resource: `config`, + } +} + +func objectBucket() *core.Command { + return &core.Command{ + Short: `Manage S3 buckets`, + Long: `Manage S3 buckets creation, deletion and updates to properties like tags, ACL and versioning.`, + Namespace: "object", + Resource: `bucket`, + } +} diff --git a/internal/namespaces/object/v1/custom_config_get.go b/internal/namespaces/object/v1/custom_config_get.go index fc1c0c8d6..da92897a0 100644 --- a/internal/namespaces/object/v1/custom_config_get.go +++ b/internal/namespaces/object/v1/custom_config_get.go @@ -1,3 +1,5 @@ +//go:build darwin || linux || windows + package object import ( diff --git a/internal/namespaces/object/v1/custom_config_install.go b/internal/namespaces/object/v1/custom_config_install.go index 0cfd66259..cffc04969 100644 --- a/internal/namespaces/object/v1/custom_config_install.go +++ b/internal/namespaces/object/v1/custom_config_install.go @@ -1,3 +1,5 @@ +//go:build darwin || linux || windows + package object import ( diff --git a/internal/namespaces/object/v1/custom_disabled.go b/internal/namespaces/object/v1/custom_disabled.go new file mode 100644 index 000000000..ed8d97629 --- /dev/null +++ b/internal/namespaces/object/v1/custom_disabled.go @@ -0,0 +1,45 @@ +//go:build !(darwin || linux || windows) + +package object + +import "github.com/scaleway/scaleway-cli/v2/core" + +func objectRoot() *core.Command { + return nil +} + +func objectConfig() *core.Command { + return nil +} + +func objectBucket() *core.Command { + return nil +} + +func bucketCreateCommand() *core.Command { + return nil +} + +func bucketDeleteCommand() *core.Command { + return nil +} + +func bucketListCommand() *core.Command { + return nil +} + +func bucketGetCommand() *core.Command { + return nil +} + +func bucketUpdateCommand() *core.Command { + return nil +} + +func configGetCommand() *core.Command { + return nil +} + +func configInstallCommand() *core.Command { + return nil +} diff --git a/internal/namespaces/object/v1/custom_marshaler.go b/internal/namespaces/object/v1/custom_marshaler.go new file mode 100644 index 000000000..579fd1989 --- /dev/null +++ b/internal/namespaces/object/v1/custom_marshaler.go @@ -0,0 +1,124 @@ +package object + +import ( + "strings" + + "github.com/aws/aws-sdk-go-v2/service/s3/types" + "github.com/scaleway/scaleway-cli/v2/core" + "github.com/scaleway/scaleway-cli/v2/internal/human" + "github.com/scaleway/scaleway-sdk-go/scw" +) + +type CustomS3ACLGrant struct { + Grantee *string + Permission types.Permission +} + +type BucketResponse struct { + SuccessResult *core.SuccessResult + BucketInfo *bucketInfo +} + +func bucketResponseMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { + resp := i.(BucketResponse) + + messageStr, err := resp.SuccessResult.MarshalHuman() + if err != nil { + return "", err + } + bucketStr, err := bucketInfoMarshalerFunc(*resp.BucketInfo, opt) + if err != nil { + return "", err + } + + return strings.Join([]string{ + messageStr, + bucketStr, + }, "\n"), nil +} + +type bucketInfo struct { + ID string + Region scw.Region + APIEndpoint string + BucketEndpoint string + EnableVersioning bool + Tags []types.Tag + ACL []CustomS3ACLGrant + Owner string +} + +func bucketInfoMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { + // To avoid recursion of human.Marshal we create a dummy type + type tmp bucketInfo + info := tmp(i.(bucketInfo)) + + opt.Sections = []*human.MarshalSection{ + { + FieldName: "Tags", + HideIfEmpty: true, + }, + { + FieldName: "ACL", + HideIfEmpty: true, + }, + } + str, err := human.Marshal(info, opt) + if err != nil { + return "", err + } + return str, nil +} + +type BucketGetResult struct { + *bucketInfo + Size *scw.Size + NbObjects *int64 + NbParts *int64 +} + +func bucketGetResultMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { + type tmp BucketGetResult + result := tmp(i.(BucketGetResult)) + opt.Sections = []*human.MarshalSection{ + { + FieldName: "Tags", + HideIfEmpty: true, + }, + { + FieldName: "ACL", + HideIfEmpty: true, + }, + } + str, err := human.Marshal(result, opt) + if err != nil { + return "", err + } + return str, nil +} + +type bucketGetArgs struct { + Region scw.Region + Name string + WithSize bool `json:"with-size"` +} + +func bucketMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) { + type tmp []types.Bucket + result := tmp(i.([]types.Bucket)) + opt.Fields = []*human.MarshalFieldOpt{ + { + FieldName: "Name", + Label: "Name", + }, + { + FieldName: "CreationDate", + Label: "Creation Date", + }, + } + str, err := human.Marshal(result, opt) + if err != nil { + return "", err + } + return str, nil +} diff --git a/internal/namespaces/object/v1/s3_helpers.go b/internal/namespaces/object/v1/s3_helpers.go index 191cb79b8..7773c17fe 100644 --- a/internal/namespaces/object/v1/s3_helpers.go +++ b/internal/namespaces/object/v1/s3_helpers.go @@ -1,3 +1,5 @@ +//go:build darwin || linux || windows + package object import ( @@ -78,11 +80,6 @@ func verifyACLInput(aclInput string) (bool, []types.BucketCannedACL) { return false, possibleValues } -type CustomS3ACLGrant struct { - Grantee *string - Permission types.Permission -} - func awsACLToCustomGrants(output *s3.GetBucketAclOutput) []CustomS3ACLGrant { customGrants := []CustomS3ACLGrant(nil) for _, grant := range output.Grants { diff --git a/internal/namespaces/object/v1/s3configfile.go b/internal/namespaces/object/v1/s3configfile.go index 52e8ce5f7..a6624ee3b 100644 --- a/internal/namespaces/object/v1/s3configfile.go +++ b/internal/namespaces/object/v1/s3configfile.go @@ -1,3 +1,5 @@ +//go:build darwin || linux || windows + package object import (