diff --git a/alias-json.go b/alias-json.go new file mode 100644 index 00000000..ddffe06e --- /dev/null +++ b/alias-json.go @@ -0,0 +1,44 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "encoding/json" + "fmt" + + "github.com/minio/mc/pkg/console" +) + +// AliasMessage container for content message structure +type AliasMessage struct { + Alias string `json:"alias"` + URL string `json:"url"` +} + +// String string printer for Content metadata +func (a AliasMessage) String() string { + if !globalJSONFlag { + message := console.Colorize("Alias", fmt.Sprintf("[%s] <-", a.Alias)) + message += console.Colorize("URL", fmt.Sprintf("%s", a.URL)) + return message + "\n" + } + jsonMessageBytes, err := json.Marshal(a) + if err != nil { + panic(err) + } + return string(jsonMessageBytes) + "\n" +} diff --git a/config-main.go b/config-main.go index f028934c..db9a9110 100644 --- a/config-main.go +++ b/config-main.go @@ -21,9 +21,11 @@ import ( "fmt" "strings" + "github.com/minio/mc/internal/github.com/fatih/color" "github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" + "github.com/minio/mc/pkg/console" ) // Configure minio client @@ -48,7 +50,10 @@ USAGE: EXAMPLES: 1. Add alias URLs. - $ mc config alias zek https://s3.amazonaws.com/ + $ mc {{.Name}} alias zek https://s3.amazonaws.com/ + + 2. List all aliased URLs. + $ mc {{.Name}} alias list `, } @@ -70,6 +75,26 @@ func mainConfig(ctx *cli.Context) { switch arg { case "alias": + if tailArgs.First() == "list" { + conf := newConfigV2() + config, err := quick.New(conf) + fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.") + + err = config.Load(mustGetMcConfigPath()) + fatalIf(err.Trace(), "Unable to load config path") + + // convert interface{} back to its original struct + newConf := config.Data().(*configV2) + // set new custom coloring + console.SetCustomTheme(map[string]*color.Color{ + "Alias": color.New(color.FgCyan, color.Bold), + "URL": color.New(color.FgWhite), + }) + for k, v := range newConf.Aliases { + console.Print(AliasMessage{k, v}) + } + return + } if len(tailArgs) < 2 { cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code } @@ -86,7 +111,7 @@ func addAlias(alias, url string) { if alias == "" || url == "" { fatalIf(probe.NewError(errors.New("")), "Alias or URL cannot be empty.") } - conf := newConfigV1() + conf := newConfigV2() config, err := quick.New(conf) fatalIf(err.Trace(conf.Version), "Failed to initialize ‘quick’ configuration data structure.") @@ -104,7 +129,7 @@ func addAlias(alias, url string) { fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias)) } // convert interface{} back to its original struct - newConf := config.Data().(*configV1) + newConf := config.Data().(*configV2) if oldURL, ok := newConf.Aliases[alias]; ok { fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL)) }