From 7ffa755cd94d11b0f5df1788b03174589828158c Mon Sep 17 00:00:00 2001 From: poornas Date: Thu, 30 Jul 2020 09:53:15 -0700 Subject: [PATCH] Move version commands to top level (#3329) --- cmd/auto-complete.go | 5 +- cmd/bucket-main.go | 1 - cmd/bucket-version.go | 151 ---------------------------- cmd/main.go | 1 + cmd/version-enable.go | 99 ++++++++++++++++++ cmd/version-info.go | 112 +++++++++++++++++++++ cmd/version-main.go | 40 ++++++++ cmd/version-suspend.go | 99 ++++++++++++++++++ docs/minio-client-complete-guide.md | 19 ++-- 9 files changed, 367 insertions(+), 160 deletions(-) delete mode 100644 cmd/bucket-version.go create mode 100644 cmd/version-enable.go create mode 100644 cmd/version-info.go create mode 100644 cmd/version-main.go create mode 100644 cmd/version-suspend.go diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 707ebfa1..cefd8075 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -259,6 +259,10 @@ var completeCmds = map[string]complete.Predictor{ "/tag/remove": s3Completer, "/tag/set": s3Completer, + "/version/info": s3Complete{deepLevel: 2}, + "/version/enable": s3Complete{deepLevel: 2}, + "/version/suspend": s3Complete{deepLevel: 2}, + "/bucket/lock/compliance": s3Completer, "/bucket/lock/governance": s3Completer, "/bucket/lock/clear": s3Completer, @@ -270,7 +274,6 @@ var completeCmds = map[string]complete.Predictor{ "/bucket/ilm": s3Complete{deepLevel: 2}, "/bucket/replicate": s3Complete{deepLevel: 2}, - "/bucket/version": s3Complete{deepLevel: 2}, // Admin API commands MinIO only. "/admin/heal": s3Completer, diff --git a/cmd/bucket-main.go b/cmd/bucket-main.go index b710b4d9..7cbe228d 100644 --- a/cmd/bucket-main.go +++ b/cmd/bucket-main.go @@ -33,7 +33,6 @@ var bucketCmd = cli.Command{ bucketILMCmd, bucketLockCmd, bucketReplicateCmd, - bucketVersionCmd, }, } diff --git a/cmd/bucket-version.go b/cmd/bucket-version.go deleted file mode 100644 index 5dba6bd4..00000000 --- a/cmd/bucket-version.go +++ /dev/null @@ -1,151 +0,0 @@ -/* - * MinIO Client (C) 2020 MinIO, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cmd - -import ( - "fmt" - "strings" - - "github.com/fatih/color" - "github.com/minio/cli" - json "github.com/minio/mc/pkg/colorjson" - "github.com/minio/mc/pkg/probe" - - "github.com/minio/minio/pkg/console" -) - -var bucketVersionCmd = cli.Command{ - Name: "version", - Usage: "manage bucket versioning", - Action: mainBucketVersion, - Before: setGlobalsFromContext, - Flags: globalFlags, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - - USAGE: - {{.HelpName}} TARGET - - FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} - EXAMPLES: - 1. Display bucket versioning status for bucket "mybucket". - {{.Prompt}} {{.HelpName}} myminio/mybucket info - - 2. Enable versioning on bucket "mybucket" for alias "myminio". - {{.Prompt}} {{.HelpName}} myminio/mybucket enable - - 3. Suspend versioning on bucket "mybucket" for alias "myminio". - {{.Prompt}} {{.HelpName}} myminio/mybucket suspend -`, -} - -// checkBucketVersionSyntax - validate all the passed arguments -func checkBucketVersionSyntax(ctx *cli.Context) { - if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 { - cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code - } -} - -type bucketVersionMessage struct { - Op string - Status string `json:"status"` - URL string `json:"url"` - Versioning struct { - Status string `json:"status"` - MFADelete string `json:"MFADelete"` - } `json:"versioning"` -} - -func (v bucketVersionMessage) JSON() string { - v.Status = "success" - jsonMessageBytes, e := json.MarshalIndent(v, "", " ") - fatalIf(probe.NewError(e), "Unable to marshal into JSON.") - return string(jsonMessageBytes) -} - -func (v bucketVersionMessage) String() string { - switch v.Op { - case "info": - msg := "" - switch v.Versioning.Status { - case "": - msg = fmt.Sprintf("%s is un-versioned", v.URL) - default: - msg = fmt.Sprintf("%s versioning status is %s", v.URL, strings.ToLower(v.Versioning.Status)) - } - return console.Colorize("BucketVersionMessage", msg) - case "enable": - return console.Colorize("BucketVersionMessage", fmt.Sprintf("%s versioning is enabled", v.URL)) - case "suspend": - return console.Colorize("BucketVersionMessage", fmt.Sprintf("%s versioning is suspended", v.URL)) - } - return "" -} - -// mainBucketVersion is the handler for "mc bucket version" command. -func mainBucketVersion(ctx *cli.Context) error { - checkBucketVersionSyntax(ctx) - - console.SetColor("BucketVersionMessage", color.New(color.FgGreen)) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - // Create a new Client - client, err := newClient(aliasedURL) - fatalIf(err, "Unable to initialize connection.") - - if len(args) != 2 { - cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code - } - op := "" - switch strings.ToLower(args[1]) { - case "enable": - op = strings.ToLower(args[1]) - case "suspend": - op = strings.ToLower(args[1]) - case "info": - op = strings.ToLower(args[1]) - default: - fatalIf(probe.NewError(fmt.Errorf("Unknown argument %s passed", args[1])), "Invalid argument") - } - - switch op { - case "info": - vConfig, e := client.GetVersioning(globalContext) - fatalIf(e, "Cannot get version info") - vMsg := bucketVersionMessage{ - Op: op, - Status: "success", - URL: aliasedURL, - } - vMsg.Versioning.Status = vConfig.Status - vMsg.Versioning.MFADelete = vConfig.MFADelete - printMsg(vMsg) - case "enable", "suspend": - fatalIf(client.SetVersioning(globalContext, args[1]), "Cannot set versioning status") - printMsg(bucketVersionMessage{ - Op: op, - Status: "success", - URL: aliasedURL, - }) - } - - return nil -} diff --git a/cmd/main.go b/cmd/main.go index 98f0f95f..27427234 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -344,6 +344,7 @@ var appCmds = []cli.Command{ watchCmd, policyCmd, tagCmd, + versionCmd, bucketCmd, adminCmd, configCmd, diff --git a/cmd/version-enable.go b/cmd/version-enable.go new file mode 100644 index 00000000..e2756dcc --- /dev/null +++ b/cmd/version-enable.go @@ -0,0 +1,99 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "context" + "fmt" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var versionEnableCmd = cli.Command{ + Name: "enable", + Usage: "Enable bucket versioning", + Action: mainVersionEnable, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Enable versioning on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkVersionEnableSyntax - validate all the passed arguments +func checkVersionEnableSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "enable", 1) // last argument is exit code + } +} + +type versionEnableMessage struct { + Op string + Status string `json:"status"` + URL string `json:"url"` + Versioning struct { + Status string `json:"status"` + MFADelete string `json:"MFADelete"` + } `json:"versioning"` +} + +func (v versionEnableMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v versionEnableMessage) String() string { + return console.Colorize("versionEnableMessage", fmt.Sprintf("%s versioning is enabled", v.URL)) +} + +func mainVersionEnable(cliCtx *cli.Context) error { + ctx, cancelVersionEnable := context.WithCancel(globalContext) + defer cancelVersionEnable() + + console.SetColor("versionEnableMessage", color.New(color.FgGreen)) + + checkVersionEnableSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + fatalIf(client.SetVersioning(ctx, "enable"), "Cannot enable versioning") + printMsg(versionEnableMessage{ + Op: "enable", + Status: "success", + URL: aliasedURL, + }) + return nil +} diff --git a/cmd/version-info.go b/cmd/version-info.go new file mode 100644 index 00000000..b10e4eda --- /dev/null +++ b/cmd/version-info.go @@ -0,0 +1,112 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "context" + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var versionInfoCmd = cli.Command{ + Name: "info", + Usage: "Show bucket versioning status", + Action: mainversionInfo, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Display bucket versioning status for bucket "mybucket". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkversionInfoSyntax - validate all the passed arguments +func checkversionInfoSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code + } +} + +type versionInfoMessage struct { + Op string + Status string `json:"status"` + URL string `json:"url"` + Versioning struct { + Status string `json:"status"` + MFADelete string `json:"MFADelete"` + } `json:"versioning"` +} + +func (v versionInfoMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v versionInfoMessage) String() string { + msg := "" + switch v.Versioning.Status { + case "": + msg = fmt.Sprintf("%s is un-versioned", v.URL) + default: + msg = fmt.Sprintf("%s versioning status is %s", v.URL, strings.ToLower(v.Versioning.Status)) + } + return console.Colorize("versionInfoMessage", msg) +} + +func mainversionInfo(cliCtx *cli.Context) error { + ctx, cancelVersionInfo := context.WithCancel(globalContext) + defer cancelVersionInfo() + + console.SetColor("versionInfoMessage", color.New(color.FgGreen)) + + checkversionInfoSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + vConfig, e := client.GetVersioning(ctx) + fatalIf(e, "Cannot get version info") + vMsg := versionInfoMessage{ + Op: "info", + Status: "success", + URL: aliasedURL, + } + vMsg.Versioning.Status = vConfig.Status + vMsg.Versioning.MFADelete = vConfig.MFADelete + printMsg(vMsg) + return nil +} diff --git a/cmd/version-main.go b/cmd/version-main.go new file mode 100644 index 00000000..d824d15c --- /dev/null +++ b/cmd/version-main.go @@ -0,0 +1,40 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import "github.com/minio/cli" + +var versionCmd = cli.Command{ + Name: "version", + Usage: "manage bucket versioning", + HideHelpCommand: true, + Action: mainVersion, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: []cli.Command{ + versionEnableCmd, + versionSuspendCmd, + versionInfoCmd, + }, +} + +// mainVersion is the handle for "mc version" command. +func mainVersion(ctx *cli.Context) error { + cli.ShowCommandHelp(ctx, ctx.Args().First()) + return nil + // Sub-commands like "info", "enable", "suspend" have their own main. +} diff --git a/cmd/version-suspend.go b/cmd/version-suspend.go new file mode 100644 index 00000000..8ea9a8e7 --- /dev/null +++ b/cmd/version-suspend.go @@ -0,0 +1,99 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "context" + "fmt" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var versionSuspendCmd = cli.Command{ + Name: "suspend", + Usage: "Suspend bucket versioning", + Action: mainVersionSuspend, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Suspend versioning on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkVersionSuspendSyntax - validate all the passed arguments +func checkVersionSuspendSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "suspend", 1) // last argument is exit code + } +} + +type versionSuspendMessage struct { + Op string + Status string `json:"status"` + URL string `json:"url"` + Versioning struct { + Status string `json:"status"` + MFADelete string `json:"MFADelete"` + } `json:"versioning"` +} + +func (v versionSuspendMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v versionSuspendMessage) String() string { + return console.Colorize("versionSuspendMessage", fmt.Sprintf("%s versioning is suspended", v.URL)) +} + +func mainVersionSuspend(cliCtx *cli.Context) error { + ctx, cancelVersionSuspend := context.WithCancel(globalContext) + defer cancelVersionSuspend() + + console.SetColor("versionSuspendMessage", color.New(color.FgGreen)) + + checkVersionSuspendSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + fatalIf(client.SetVersioning(ctx, "suspend"), "Cannot suspend versioning") + printMsg(versionSuspendMessage{ + Op: "suspend", + Status: "success", + URL: aliasedURL, + }) + return nil +} diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index e20be93e..67e6a27d 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -1585,30 +1585,35 @@ Metadata : `version` manages bucket versioning ``` +NAME: + mc version - manage bucket versioning + USAGE: - mc bucket version TARGET [enable | suspend | info] + mc version COMMAND [COMMAND FLAGS | -h] [ARGUMENTS...] -FLAGS: - --help, -h show help +COMMANDS: + enable Enable bucket versioning + suspend Suspend bucket versioning + info Show bucket versioning status ``` -*Example: Enable versioning on bucket `mybucket`* +*Example: Enable versioning on bucket `mybucket`* ``` -mc bucket version myminio/mybucket enable +mc version enable myminio/mybucket myminio/mybucket versioning is enabled ``` *Example: Display the versioning configuration for bucket `mybucket`* ``` -mc bucket version myminio/mybucket info +mc version info myminio/mybucket myminio/mybucket versioning status is enabled ``` *Example: Suspend versioning for bucket `mybucket`* ``` -mc bucket version myminio/mybucket suspend +mc version suspend myminio/mybucket myminio/mybucket versioning is suspended ```