1
0
mirror of https://github.com/minio/mc.git synced 2025-11-28 08:03:56 +03:00

Move version commands to top level (#3329)

This commit is contained in:
poornas
2020-07-30 09:53:15 -07:00
committed by GitHub
parent 0631315535
commit 7ffa755cd9
9 changed files with 367 additions and 160 deletions

View File

@@ -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,

View File

@@ -33,7 +33,6 @@ var bucketCmd = cli.Command{
bucketILMCmd,
bucketLockCmd,
bucketReplicateCmd,
bucketVersionCmd,
},
}

View File

@@ -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
}

View File

@@ -344,6 +344,7 @@ var appCmds = []cli.Command{
watchCmd,
policyCmd,
tagCmd,
versionCmd,
bucketCmd,
adminCmd,
configCmd,

99
cmd/version-enable.go Normal file
View File

@@ -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
}

112
cmd/version-info.go Normal file
View File

@@ -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
}

40
cmd/version-main.go Normal file
View File

@@ -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.
}

99
cmd/version-suspend.go Normal file
View File

@@ -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
}

View File

@@ -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
```
<a name="encrypt"></a>