github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/alias-list.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 "fmt" 22 "sort" 23 24 "github.com/fatih/color" 25 "github.com/minio/cli" 26 "github.com/minio/pkg/v2/console" 27 ) 28 29 var aliasListCmd = cli.Command{ 30 Name: "list", 31 ShortName: "ls", 32 Usage: "list aliases in configuration file", 33 Action: func(ctx *cli.Context) error { 34 return mainAliasList(ctx, false) 35 }, 36 Before: setGlobalsFromContext, 37 Flags: globalFlags, 38 OnUsageError: onUsageError, 39 HideHelpCommand: true, 40 CustomHelpTemplate: `NAME: 41 {{.HelpName}} - {{.Usage}} 42 43 USAGE: 44 {{.HelpName}} [ALIAS] 45 46 FLAGS: 47 {{range .VisibleFlags}}{{.}} 48 {{end}} 49 EXAMPLES: 50 1. List all aliases. 51 {{.Prompt}} {{.HelpName}} 52 53 2. List a specific alias. 54 {{.Prompt}} {{.HelpName}} s3 55 `, 56 } 57 58 // Input argument validator.. 59 func checkAliasListSyntax(ctx *cli.Context) { 60 args := ctx.Args() 61 62 if len(ctx.Args()) > 1 { 63 fatalIf(errInvalidArgument().Trace(args...), 64 "Incorrect number of arguments to alias list command.") 65 } 66 } 67 68 func mainAliasList(ctx *cli.Context, deprecated bool) error { 69 checkAliasListSyntax(ctx) 70 71 // Additional command specific theme customization. 72 console.SetColor("Alias", color.New(color.FgCyan, color.Bold)) 73 console.SetColor("URL", color.New(color.FgYellow)) 74 console.SetColor("AccessKey", color.New(color.FgCyan)) 75 console.SetColor("SecretKey", color.New(color.FgCyan)) 76 console.SetColor("API", color.New(color.FgBlue)) 77 console.SetColor("Path", color.New(color.FgCyan)) 78 79 alias := cleanAlias(ctx.Args().Get(0)) 80 81 aliasesMsgs := listAliases(alias, deprecated) // List all configured hosts. 82 for i := range aliasesMsgs { 83 aliasesMsgs[i].op = "list" 84 } 85 printAliases(aliasesMsgs...) 86 return nil 87 } 88 89 // Prints all the aliases 90 func printAliases(aliases ...aliasMessage) { 91 maxAlias := 0 92 for _, alias := range aliases { 93 if len(alias.Alias) > maxAlias { 94 maxAlias = len(alias.Alias) 95 } 96 } 97 for _, alias := range aliases { 98 if !globalJSON { 99 // Format properly for alignment based on alias length only in non json mode. 100 alias.Alias = fmt.Sprintf("%-*.*s", maxAlias, maxAlias, alias.Alias) 101 } 102 if alias.AccessKey == "" || alias.SecretKey == "" { 103 alias.AccessKey = "" 104 alias.SecretKey = "" 105 alias.API = "" 106 } 107 printMsg(alias) 108 } 109 } 110 111 // byAlias is a collection satisfying sort.Interface 112 type byAlias []aliasMessage 113 114 func (d byAlias) Len() int { return len(d) } 115 func (d byAlias) Swap(i, j int) { d[i], d[j] = d[j], d[i] } 116 func (d byAlias) Less(i, j int) bool { return d[i].Alias < d[j].Alias } 117 118 // listAliases - list one or all aliases 119 func listAliases(alias string, deprecated bool) (aliases []aliasMessage) { 120 conf, err := loadMcConfig() 121 fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.") 122 123 // If specific alias is requested, look for it and print. 124 if alias != "" { 125 if v, ok := conf.Aliases[alias]; ok { 126 aliasMsg := aliasMessage{ 127 prettyPrint: false, 128 Alias: alias, 129 URL: v.URL, 130 AccessKey: v.AccessKey, 131 SecretKey: v.SecretKey, 132 API: v.API, 133 } 134 135 if deprecated { 136 aliasMsg.Lookup = v.Path 137 } else { 138 aliasMsg.Path = v.Path 139 } 140 141 return []aliasMessage{aliasMsg} 142 } 143 fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.") 144 } 145 146 for k, v := range conf.Aliases { 147 aliasMsg := aliasMessage{ 148 prettyPrint: true, 149 Alias: k, 150 URL: v.URL, 151 AccessKey: v.AccessKey, 152 SecretKey: v.SecretKey, 153 API: v.API, 154 } 155 156 if deprecated { 157 aliasMsg.Lookup = v.Path 158 } else { 159 aliasMsg.Path = v.Path 160 } 161 162 aliases = append(aliases, aliasMsg) 163 } 164 165 // Sort by alias names lexically. 166 sort.Sort(byAlias(aliases)) 167 return 168 }