github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-export.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "bufio" 22 "bytes" 23 "fmt" 24 "io" 25 "strings" 26 27 "github.com/fatih/color" 28 "github.com/minio/cli" 29 json "github.com/minio/colorjson" 30 "github.com/minio/mc/pkg/probe" 31 "github.com/minio/pkg/v2/console" 32 ) 33 34 var adminConfigExportCmd = cli.Command{ 35 Name: "export", 36 Usage: "export all config keys to STDOUT", 37 Before: setGlobalsFromContext, 38 Action: mainAdminConfigExport, 39 OnUsageError: onUsageError, 40 Flags: globalFlags, 41 CustomHelpTemplate: `NAME: 42 {{.HelpName}} - {{.Usage}} 43 44 USAGE: 45 {{.HelpName}} TARGET 46 47 FLAGS: 48 {{range .VisibleFlags}}{{.}} 49 {{end}} 50 EXAMPLES: 51 The output includes environment variables set on the server. These cannot be overridden from the client. 52 53 1. Export the current config from MinIO server 54 {{.Prompt}} {{.HelpName}} play/ > config.txt 55 `, 56 } 57 58 // configExportMessage container to hold locks information. 59 type configExportMessage struct { 60 Status string `json:"status"` 61 Value []byte `json:"value"` 62 } 63 64 // String colorized service status message. 65 func (u configExportMessage) String() string { 66 console.SetColor("EnvVar", color.New(color.FgYellow)) 67 bio := bufio.NewReader(bytes.NewReader(u.Value)) 68 var lines []string 69 for { 70 s, e := bio.ReadString('\n') 71 // Make lines displaying environment variables bold. 72 if strings.HasPrefix(s, "# MINIO_") { 73 s = strings.TrimPrefix(s, "# ") 74 parts := strings.SplitN(s, "=", 2) 75 s = fmt.Sprintf("# %s=%s", console.Colorize("EnvVar", parts[0]), parts[1]) 76 lines = append(lines, s) 77 } else { 78 lines = append(lines, s) 79 } 80 if e == io.EOF { 81 break 82 } 83 fatalIf(probe.NewError(e), "Unable to marshal to string.") 84 } 85 return strings.Join(lines, "") 86 } 87 88 // JSON jsonified service status Message message. 89 func (u configExportMessage) JSON() string { 90 u.Status = "success" 91 statusJSONBytes, e := json.MarshalIndent(u, "", " ") 92 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 93 94 return string(statusJSONBytes) 95 } 96 97 // checkAdminConfigExportSyntax - validate all the passed arguments 98 func checkAdminConfigExportSyntax(ctx *cli.Context) { 99 if !ctx.Args().Present() || len(ctx.Args()) > 1 { 100 showCommandHelpAndExit(ctx, 1) // last argument is exit code 101 } 102 } 103 104 func mainAdminConfigExport(ctx *cli.Context) error { 105 checkAdminConfigExportSyntax(ctx) 106 107 // Export the alias parameter from cli 108 args := ctx.Args() 109 aliasedURL := args.Get(0) 110 111 // Create a new MinIO Admin Client 112 client, err := newAdminClient(aliasedURL) 113 fatalIf(err, "Unable to initialize admin connection.") 114 115 // Call get config API 116 buf, e := client.GetConfig(globalContext) 117 fatalIf(probe.NewError(e), "Unable to get server config") 118 119 // Print 120 printMsg(configExportMessage{ 121 Value: buf, 122 }) 123 124 return nil 125 }