github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/cmd/version-enable.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "fmt" 23 "strings" 24 25 "github.com/fatih/color" 26 "github.com/minio/cli" 27 json "github.com/minio/colorjson" 28 "github.com/minio/mc/pkg/probe" 29 "github.com/minio/pkg/v2/console" 30 ) 31 32 var versionEnableFlags = []cli.Flag{ 33 cli.StringFlag{ 34 Name: "excluded-prefixes", 35 Usage: "exclude versioning on these prefix patterns", 36 }, 37 cli.BoolFlag{ 38 Name: "exclude-folders", 39 Usage: "exclude versioning on folder objects", 40 }, 41 } 42 43 var versionEnableCmd = cli.Command{ 44 Name: "enable", 45 Usage: "enable bucket versioning", 46 Action: mainVersionEnable, 47 OnUsageError: onUsageError, 48 Before: setGlobalsFromContext, 49 Flags: append(globalFlags, versionEnableFlags...), 50 CustomHelpTemplate: `NAME: 51 {{.HelpName}} - {{.Usage}} 52 53 USAGE: 54 {{.HelpName}} ALIAS/BUCKET 55 56 FLAGS: 57 {{range .VisibleFlags}}{{.}} 58 {{end}} 59 EXAMPLES: 60 1. Enable versioning on bucket "mybucket" for alias "myminio". 61 {{.Prompt}} {{.HelpName}} myminio/mybucket 62 63 2. Enable versioning on bucket "mybucket" while excluding versioning on a few select prefixes. 64 {{.Prompt}} {{.HelpName}} myminio/mybucket --excluded-prefixes "app1/*/_temporary/,app2/*/_staging/" 65 66 3. Enable versioning on bucket "mybucket" while excluding versioning on a few select prefixes and all folders. 67 Note: this is useful on buckets used with Spark/Hadoop workloads. 68 {{.Prompt}} {{.HelpName}} myminio/mybucket --excluded-prefixes "app1/*/_temporary/,app2/*/_staging/" --exclude-folders 69 `, 70 } 71 72 // checkVersionEnableSyntax - validate all the passed arguments 73 func checkVersionEnableSyntax(ctx *cli.Context) { 74 if len(ctx.Args()) != 1 { 75 showCommandHelpAndExit(ctx, 1) // last argument is exit code 76 } 77 } 78 79 type versionEnableMessage struct { 80 Op string 81 Status string `json:"status"` 82 URL string `json:"url"` 83 Versioning struct { 84 Status string `json:"status"` 85 MFADelete string `json:"MFADelete"` 86 ExcludedPrefixes []string `json:"ExcludedPrefixes,omitempty"` 87 ExcludeFolders bool `json:"ExcludeFolders,,omitempty"` 88 } `json:"versioning"` 89 } 90 91 func (v versionEnableMessage) JSON() string { 92 v.Status = "success" 93 jsonMessageBytes, e := json.MarshalIndent(v, "", " ") 94 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 95 return string(jsonMessageBytes) 96 } 97 98 func (v versionEnableMessage) String() string { 99 return console.Colorize("versionEnableMessage", fmt.Sprintf("%s versioning is enabled", v.URL)) 100 } 101 102 func mainVersionEnable(cliCtx *cli.Context) error { 103 ctx, cancelVersionEnable := context.WithCancel(globalContext) 104 defer cancelVersionEnable() 105 106 console.SetColor("versionEnableMessage", color.New(color.FgGreen)) 107 108 checkVersionEnableSyntax(cliCtx) 109 110 // Get the alias parameter from cli 111 args := cliCtx.Args() 112 aliasedURL := args.Get(0) 113 114 var excludedPrefixes []string 115 prefixesStr := cliCtx.String("excluded-prefixes") 116 if prefixesStr != "" { 117 excludedPrefixes = strings.Split(prefixesStr, ",") 118 } 119 excludeFolders := cliCtx.Bool("exclude-folders") 120 121 // Create a new Client 122 client, err := newClient(aliasedURL) 123 fatalIf(err, "Unable to initialize connection.") 124 fatalIf(client.SetVersion(ctx, "enable", excludedPrefixes, excludeFolders), "Unable to enable versioning") 125 printMsg(versionEnableMessage{ 126 Op: cliCtx.Command.Name, 127 Status: "success", 128 URL: aliasedURL, 129 }) 130 return nil 131 }