github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/version-info.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 versionInfoCmd = cli.Command{ 33 Name: "info", 34 Usage: "show bucket versioning status", 35 Action: mainVersionInfo, 36 OnUsageError: onUsageError, 37 Before: setGlobalsFromContext, 38 Flags: globalFlags, 39 CustomHelpTemplate: `NAME: 40 {{.HelpName}} - {{.Usage}} 41 42 USAGE: 43 {{.HelpName}} ALIAS/BUCKET 44 45 FLAGS: 46 {{range .VisibleFlags}}{{.}} 47 {{end}} 48 EXAMPLES: 49 1. Display bucket versioning status for bucket "mybucket". 50 {{.Prompt}} {{.HelpName}} myminio/mybucket 51 `, 52 } 53 54 // checkVersionInfoSyntax - validate all the passed arguments 55 func checkVersionInfoSyntax(ctx *cli.Context) { 56 if len(ctx.Args()) != 1 { 57 showCommandHelpAndExit(ctx, 1) // last argument is exit code 58 } 59 } 60 61 type versioningInfoMessage struct { 62 Op string 63 Status string `json:"status"` 64 URL string `json:"url"` 65 Versioning struct { 66 Status string `json:"status"` 67 MFADelete string `json:"MFADelete"` 68 ExcludedPrefixes []string `json:"ExcludedPrefixes,omitempty"` 69 ExcludeFolders bool `json:"ExcludeFolders,omitempty"` 70 } `json:"versioning"` 71 } 72 73 func (v versioningInfoMessage) JSON() string { 74 v.Status = "success" 75 jsonMessageBytes, e := json.MarshalIndent(v, "", " ") 76 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 77 return string(jsonMessageBytes) 78 } 79 80 func (v versioningInfoMessage) String() string { 81 msg := "" 82 switch v.Versioning.Status { 83 case "": 84 msg = fmt.Sprintf("%s is un-versioned", v.URL) 85 default: 86 msg = fmt.Sprintf("%s versioning is %s", v.URL, strings.ToLower(v.Versioning.Status)) 87 } 88 return console.Colorize("versioningInfoMessage", msg) 89 } 90 91 func mainVersionInfo(cliCtx *cli.Context) error { 92 ctx, cancelVersioningInfo := context.WithCancel(globalContext) 93 defer cancelVersioningInfo() 94 95 console.SetColor("versioningInfoMessage", color.New(color.FgGreen)) 96 97 checkVersionInfoSyntax(cliCtx) 98 99 // Get the alias parameter from cli 100 args := cliCtx.Args() 101 aliasedURL := args.Get(0) 102 // Create a new Client 103 client, err := newClient(aliasedURL) 104 fatalIf(err, "Unable to initialize connection.") 105 vConfig, e := client.GetVersion(ctx) 106 fatalIf(e, "Unable to get versioning info") 107 vMsg := versioningInfoMessage{ 108 Op: cliCtx.Command.Name, 109 Status: "success", 110 URL: aliasedURL, 111 } 112 vMsg.Versioning.Status = vConfig.Status 113 vMsg.Versioning.MFADelete = vConfig.MFADelete 114 vMsg.Versioning.ExcludeFolders = vConfig.ExcludeFolders 115 if len(vConfig.ExcludedPrefixes) > 0 { 116 prefixes := make([]string, 0, len(vConfig.ExcludedPrefixes)) 117 for _, eprefix := range vConfig.ExcludedPrefixes { 118 prefixes = append(prefixes, eprefix.Prefix) 119 } 120 vMsg.Versioning.ExcludedPrefixes = prefixes 121 } 122 123 printMsg(vMsg) 124 return nil 125 }