github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/alias-export.go (about) 1 package cmd 2 3 import ( 4 "github.com/minio/cli" 5 json "github.com/minio/colorjson" 6 "github.com/minio/mc/pkg/probe" 7 "github.com/minio/pkg/v2/console" 8 ) 9 10 var aliasExportCmd = cli.Command{ 11 Name: "export", 12 ShortName: "e", 13 Usage: "export configuration info to stdout", 14 Action: mainAliasExport, 15 OnUsageError: onUsageError, 16 Before: setGlobalsFromContext, 17 Flags: globalFlags, 18 HideHelpCommand: true, 19 CustomHelpTemplate: `NAME: 20 {{.HelpName}} - {{.Usage}} 21 22 USAGE: 23 {{.HelpName}} ALIAS 24 25 Credentials to be exported will be in the following JSON format: 26 27 { 28 "url": "http://localhost:9000", 29 "accessKey": "YJ0RI0F4R5HWY38MD873", 30 "secretKey": "OHz5CT7xdMHiXnKZP0BmZ5P4G5UvWvVaxR8gljLG", 31 "api": "s3v4", 32 "path": "auto" 33 } 34 35 FLAGS: 36 {{range .VisibleFlags}}{{.}} 37 {{end}} 38 EXAMPLES: 39 1. Export the provided 'alias' to credentials.json file: 40 {{ .Prompt }} {{ .HelpName }} myminio/ > credentials.json 41 42 2. Export the credentials to standard output and pipe it to import command 43 {{ .Prompt }} {{ .HelpName }} alias1/ | mc alias import alias2/ 44 `, 45 } 46 47 // checkAliasExportSyntax - verifies input arguments to 'alias export'. 48 func checkAliasExportSyntax(ctx *cli.Context) { 49 args := ctx.Args() 50 if ctx.NArg() == 0 { 51 showCommandHelpAndExit(ctx, 1) 52 } 53 if ctx.NArg() > 1 { 54 fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), 55 "Incorrect number of arguments for alias export command.") 56 } 57 58 alias := cleanAlias(args.Get(0)) 59 if !isValidAlias(alias) { 60 fatalIf(errInvalidAlias(alias), "Unable to validate alias") 61 } 62 } 63 64 // exportAlias - get an alias config 65 func exportAlias(alias string) { 66 mcCfgV10, err := loadMcConfig() 67 fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config `"+mustGetMcConfigPath()+"`.") 68 69 cfg, ok := mcCfgV10.Aliases[alias] 70 if !ok { 71 fatalIf(errInvalidArgument().Trace(alias), "Unable to export credentials") 72 } 73 74 buf, e := json.Marshal(cfg) 75 fatalIf(probe.NewError(e).Trace(alias), "Unable to export credentials") 76 77 console.Println(string(buf)) 78 } 79 80 func mainAliasExport(cli *cli.Context) error { 81 args := cli.Args() 82 83 checkAliasExportSyntax(cli) 84 85 exportAlias(cleanAlias(args.Get(0))) 86 87 return nil 88 }