-
Notifications
You must be signed in to change notification settings - Fork 515
/
alias-list.go
168 lines (144 loc) · 4.33 KB
/
alias-list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"sort"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/pkg/v2/console"
)
var aliasListCmd = cli.Command{
Name: "list",
ShortName: "ls",
Usage: "list aliases in configuration file",
Action: func(ctx *cli.Context) error {
return mainAliasList(ctx, false)
},
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
HideHelpCommand: true,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [ALIAS]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. List all aliases.
{{.Prompt}} {{.HelpName}}
2. List a specific alias.
{{.Prompt}} {{.HelpName}} s3
`,
}
// Input argument validator..
func checkAliasListSyntax(ctx *cli.Context) {
args := ctx.Args()
if len(ctx.Args()) > 1 {
fatalIf(errInvalidArgument().Trace(args...),
"Incorrect number of arguments to alias list command.")
}
}
func mainAliasList(ctx *cli.Context, deprecated bool) error {
checkAliasListSyntax(ctx)
// Additional command specific theme customization.
console.SetColor("Alias", color.New(color.FgCyan, color.Bold))
console.SetColor("URL", color.New(color.FgYellow))
console.SetColor("AccessKey", color.New(color.FgCyan))
console.SetColor("SecretKey", color.New(color.FgCyan))
console.SetColor("API", color.New(color.FgBlue))
console.SetColor("Path", color.New(color.FgCyan))
alias := cleanAlias(ctx.Args().Get(0))
aliasesMsgs := listAliases(alias, deprecated) // List all configured hosts.
for i := range aliasesMsgs {
aliasesMsgs[i].op = "list"
}
printAliases(aliasesMsgs...)
return nil
}
// Prints all the aliases
func printAliases(aliases ...aliasMessage) {
maxAlias := 0
for _, alias := range aliases {
if len(alias.Alias) > maxAlias {
maxAlias = len(alias.Alias)
}
}
for _, alias := range aliases {
if !globalJSON {
// Format properly for alignment based on alias length only in non json mode.
alias.Alias = fmt.Sprintf("%-*.*s", maxAlias, maxAlias, alias.Alias)
}
if alias.AccessKey == "" || alias.SecretKey == "" {
alias.AccessKey = ""
alias.SecretKey = ""
alias.API = ""
}
printMsg(alias)
}
}
// byAlias is a collection satisfying sort.Interface
type byAlias []aliasMessage
func (d byAlias) Len() int { return len(d) }
func (d byAlias) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d byAlias) Less(i, j int) bool { return d[i].Alias < d[j].Alias }
// listAliases - list one or all aliases
func listAliases(alias string, deprecated bool) (aliases []aliasMessage) {
conf, err := loadMcConfig()
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.")
// If specific alias is requested, look for it and print.
if alias != "" {
if v, ok := conf.Aliases[alias]; ok {
aliasMsg := aliasMessage{
prettyPrint: false,
Alias: alias,
URL: v.URL,
AccessKey: v.AccessKey,
SecretKey: v.SecretKey,
API: v.API,
}
if deprecated {
aliasMsg.Lookup = v.Path
} else {
aliasMsg.Path = v.Path
}
return []aliasMessage{aliasMsg}
}
fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.")
}
for k, v := range conf.Aliases {
aliasMsg := aliasMessage{
prettyPrint: true,
Alias: k,
URL: v.URL,
AccessKey: v.AccessKey,
SecretKey: v.SecretKey,
API: v.API,
}
if deprecated {
aliasMsg.Lookup = v.Path
} else {
aliasMsg.Path = v.Path
}
aliases = append(aliases, aliasMsg)
}
// Sort by alias names lexically.
sort.Sort(byAlias(aliases))
return
}