diff --git a/cmd/alias-export.go b/cmd/alias-export.go new file mode 100644 index 00000000..fdc95734 --- /dev/null +++ b/cmd/alias-export.go @@ -0,0 +1,88 @@ +package cmd + +import ( + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/v2/console" +) + +var aliasExportCmd = cli.Command{ + Name: "export", + ShortName: "e", + Usage: "export configuration info to stdout", + Action: mainAliasExport, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} ALIAS + + Credentials to be exported will be in the following JSON format: + + { + "url": "http://localhost:9000", + "accessKey": "YJ0RI0F4R5HWY38MD873", + "secretKey": "OHz5CT7xdMHiXnKZP0BmZ5P4G5UvWvVaxR8gljLG", + "api": "s3v4", + "path": "auto" + } + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Export the provided 'alias' to credentials.json file: + {{ .Prompt }} {{ .HelpName }} myminio/ > credentials.json + + 2. Export the credentials to standard output and pipe it to import command + {{ .Prompt }} {{ .HelpName }} alias1/ | mc alias import alias2/ +`, +} + +// checkAliasExportSyntax - verifies input arguments to 'alias export'. +func checkAliasExportSyntax(ctx *cli.Context) { + args := ctx.Args() + if ctx.NArg() == 0 { + showCommandHelpAndExit(ctx, 1) + } + if ctx.NArg() > 1 { + fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), + "Incorrect number of arguments for alias export command.") + } + + alias := cleanAlias(args.Get(0)) + if !isValidAlias(alias) { + fatalIf(errInvalidAlias(alias), "Unable to validate alias") + } +} + +// exportAlias - get an alias config +func exportAlias(alias string) { + mcCfgV10, err := loadMcConfig() + fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config `"+mustGetMcConfigPath()+"`.") + + cfg, ok := mcCfgV10.Aliases[alias] + if !ok { + fatalIf(errInvalidArgument().Trace(alias), "Unable to export credentials") + } + + buf, e := json.Marshal(cfg) + fatalIf(probe.NewError(e).Trace(alias), "Unable to export credentials") + + console.Println(string(buf)) +} + +func mainAliasExport(cli *cli.Context) error { + args := cli.Args() + + checkAliasExportSyntax(cli) + + exportAlias(cleanAlias(args.Get(0))) + + return nil +} diff --git a/cmd/alias-import.go b/cmd/alias-import.go index 99be1632..3f18ff96 100644 --- a/cmd/alias-import.go +++ b/cmd/alias-import.go @@ -22,9 +22,8 @@ import ( "os" "strings" - "github.com/minio/mc/pkg/probe" - "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" ) var aliasImportCmd = cli.Command{ diff --git a/cmd/alias-main.go b/cmd/alias-main.go index 874806b1..ad16d1db 100644 --- a/cmd/alias-main.go +++ b/cmd/alias-main.go @@ -42,6 +42,7 @@ var aliasSubcommands = []cli.Command{ aliasListCmd, aliasRemoveCmd, aliasImportCmd, + aliasExportCmd, } var aliasCmd = cli.Command{ diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 3f0e0360..6af90f84 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -468,6 +468,7 @@ var completeCmds = map[string]complete.Predictor{ "/alias/list": aliasCompleter, "/alias/remove": aliasCompleter, "/alias/import": nil, + "/alias/export": aliasCompleter, "/support/callhome": aliasCompleter, "/support/register": aliasCompleter,