alias: mc alias list command outputs the alias information specified by MC_CONFIG_ENV_FILE (#5028)
* Update alias-list.go
This commit is contained in:
+55
-37
@@ -19,11 +19,14 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/pkg/v3/console"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var aliasListCmd = cli.Command{
|
||||
@@ -75,6 +78,7 @@ func mainAliasList(ctx *cli.Context, deprecated bool) error {
|
||||
console.SetColor("SecretKey", color.New(color.FgCyan))
|
||||
console.SetColor("API", color.New(color.FgBlue))
|
||||
console.SetColor("Path", color.New(color.FgCyan))
|
||||
console.SetColor("Src", color.New(color.FgCyan))
|
||||
|
||||
alias := cleanAlias(ctx.Args().Get(0))
|
||||
|
||||
@@ -115,51 +119,65 @@ 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 }
|
||||
|
||||
func buildAliasMessage(alias string, deprecated bool, aliasCfg *aliasConfigV10) aliasMessage {
|
||||
aliasMsg := aliasMessage{
|
||||
prettyPrint: false,
|
||||
Alias: alias,
|
||||
URL: aliasCfg.URL,
|
||||
AccessKey: aliasCfg.AccessKey,
|
||||
SecretKey: aliasCfg.SecretKey,
|
||||
API: aliasCfg.API,
|
||||
Src: aliasCfg.Src,
|
||||
}
|
||||
|
||||
if deprecated {
|
||||
aliasMsg.Lookup = aliasCfg.Path
|
||||
} else {
|
||||
aliasMsg.Path = aliasCfg.Path
|
||||
}
|
||||
return aliasMsg
|
||||
}
|
||||
|
||||
// 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}
|
||||
aliasCfg := mustGetHostConfig(alias)
|
||||
if aliasCfg != nil {
|
||||
return []aliasMessage{buildAliasMessage(alias, deprecated, aliasCfg)}
|
||||
}
|
||||
|
||||
fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.")
|
||||
}
|
||||
|
||||
// list alias from the environment variable.
|
||||
for _, envLine := range os.Environ() {
|
||||
pair := strings.SplitN(envLine, "=", 2)
|
||||
if len(pair) != 2 {
|
||||
continue
|
||||
}
|
||||
if len(strings.TrimPrefix(pair[0], mcEnvHostPrefix)) != 0 {
|
||||
aliasCfg, _ := expandAliasFromEnv(pair[1])
|
||||
if aliasCfg == nil {
|
||||
continue
|
||||
}
|
||||
alias := strings.ReplaceAll(pair[0], mcEnvHostPrefix, "")
|
||||
aliases = append(aliases, buildAliasMessage(alias, deprecated, aliasCfg))
|
||||
}
|
||||
}
|
||||
|
||||
// list alias from the customized configuration.
|
||||
for s, aliasCfg := range aliasToConfigMap {
|
||||
aliases = append(aliases, buildAliasMessage(s, deprecated, aliasCfg))
|
||||
}
|
||||
|
||||
// list alias from the default configuration.
|
||||
conf, err := loadMcConfig()
|
||||
fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.")
|
||||
|
||||
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)
|
||||
v.Src = mustGetMcConfigPath()
|
||||
aliases = append(aliases, buildAliasMessage(k, deprecated, &v))
|
||||
}
|
||||
|
||||
// Sort by alias names lexically.
|
||||
|
||||
+3
-1
@@ -73,6 +73,7 @@ type aliasMessage struct {
|
||||
SecretKey string `json:"secretKey,omitempty"`
|
||||
API string `json:"api,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Src string `json:"src,omitempty"`
|
||||
// Deprecated field, replaced by Path
|
||||
Lookup string `json:"lookup,omitempty"`
|
||||
}
|
||||
@@ -91,13 +92,14 @@ func (h aliasMessage) String() string {
|
||||
Row{"SecretKey", "SecretKey"},
|
||||
Row{"API", "API"},
|
||||
Row{"Path", "Path"},
|
||||
Row{"Src", "Src"},
|
||||
)
|
||||
// Handle deprecated lookup
|
||||
path := h.Path
|
||||
if path == "" {
|
||||
path = h.Lookup
|
||||
}
|
||||
return t.buildRecord(h.Alias, h.URL, h.AccessKey, h.SecretKey, h.API, path)
|
||||
return t.buildRecord(h.Alias, h.URL, h.AccessKey, h.SecretKey, h.API, path, h.Src)
|
||||
case "remove":
|
||||
return console.Colorize("AliasMessage", "Removed `"+h.Alias+"` successfully.")
|
||||
case "add": // add is deprecated
|
||||
|
||||
@@ -46,6 +46,7 @@ type aliasConfigV10 struct {
|
||||
Path string `json:"path"`
|
||||
License string `json:"license,omitempty"`
|
||||
APIKey string `json:"apiKey,omitempty"`
|
||||
Src string `json:"src,omitempty"`
|
||||
}
|
||||
|
||||
// configV10 config version.
|
||||
|
||||
+11
-5
@@ -185,6 +185,7 @@ func getAliasConfig(alias string) (*aliasConfigV10, *probe.Error) {
|
||||
// if host is exact return quickly.
|
||||
if _, ok := mcCfg.Aliases[alias]; ok {
|
||||
hostCfg := mcCfg.Aliases[alias]
|
||||
hostCfg.Src = mustGetMcConfigPath()
|
||||
return &hostCfg, nil
|
||||
}
|
||||
|
||||
@@ -194,15 +195,18 @@ func getAliasConfig(alias string) (*aliasConfigV10, *probe.Error) {
|
||||
|
||||
// mustGetHostConfig retrieves host specific configuration such as access keys, signature type.
|
||||
func mustGetHostConfig(alias string) *aliasConfigV10 {
|
||||
aliasCfg, _ := getAliasConfig(alias)
|
||||
// look for it in the environment variable first.
|
||||
aliasCfg, _ := expandAliasFromEnv(env.Get(mcEnvHostPrefix+alias, ""))
|
||||
|
||||
// If alias is not found,
|
||||
// look for it in the environment variable.
|
||||
if aliasCfg == nil {
|
||||
aliasCfg, _ = expandAliasFromEnv(env.Get(mcEnvHostPrefix+alias, ""))
|
||||
}
|
||||
// look for it in the customized configuration.
|
||||
if aliasCfg == nil {
|
||||
aliasCfg = aliasToConfigMap[alias]
|
||||
}
|
||||
|
||||
if aliasCfg == nil {
|
||||
aliasCfg, _ = getAliasConfig(alias)
|
||||
}
|
||||
return aliasCfg
|
||||
}
|
||||
|
||||
@@ -286,6 +290,7 @@ func readAliasesFromFile(envConfigFile string) *probe.Error {
|
||||
if err != nil {
|
||||
return err.Trace(envLine)
|
||||
}
|
||||
aliasConfig.Src = envConfigFile
|
||||
aliasToConfigMap[alias] = aliasConfig
|
||||
}
|
||||
if e := scanner.Err(); e != nil {
|
||||
@@ -306,6 +311,7 @@ func expandAliasFromEnv(envURL string) (*aliasConfigV10, *probe.Error) {
|
||||
AccessKey: accessKey,
|
||||
SecretKey: secretKey,
|
||||
SessionToken: sessionToken,
|
||||
Src: "env",
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user