github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-get.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/madmin-go/v3"
    31  	"github.com/minio/mc/pkg/probe"
    32  	"github.com/minio/pkg/v2/console"
    33  )
    34  
    35  var adminConfigGetCmd = cli.Command{
    36  	Name:         "get",
    37  	Usage:        "interactively retrieve a config key parameters",
    38  	Before:       setGlobalsFromContext,
    39  	Action:       mainAdminConfigGet,
    40  	OnUsageError: onUsageError,
    41  	Flags:        globalFlags,
    42  	CustomHelpTemplate: `NAME:
    43    {{.HelpName}} - {{.Usage}}
    44  
    45  USAGE:
    46    {{.HelpName}} TARGET
    47  
    48  FLAGS:
    49    {{range .VisibleFlags}}{{.}}
    50    {{end}}
    51  EXAMPLES:
    52    The output includes environment variables set on the server. These cannot be overridden from the client.
    53  
    54    1. Get the current region setting on MinIO server.
    55       {{.Prompt}} {{.HelpName}} play/ region
    56       region name=us-east-1
    57  
    58    2. Get the current notification settings for Webhook target on MinIO server
    59       {{.Prompt}} {{.HelpName}} myminio/ notify_webhook
    60       notify_webhook endpoint="http://localhost:8080" auth_token= queue_limit=10000 queue_dir="/home/events"
    61  
    62    3. Get the current compression settings on MinIO server
    63       {{.Prompt}} {{.HelpName}} myminio/ compression
    64       compression extensions=".txt,.csv" mime_types="text/*"
    65  `,
    66  }
    67  
    68  type configGetMessage struct {
    69  	Status string                `json:"status"`
    70  	Config []madmin.SubsysConfig `json:"config"`
    71  	value  []byte
    72  }
    73  
    74  // String colorized service status message.
    75  func (u configGetMessage) String() string {
    76  	console.SetColor("EnvVar", color.New(color.FgYellow))
    77  	bio := bufio.NewReader(bytes.NewReader(u.value))
    78  	var lines []string
    79  	for {
    80  		s, e := bio.ReadString('\n')
    81  		// Make lines displaying environment variables bold.
    82  		if strings.HasPrefix(s, "# MINIO_") {
    83  			s = strings.TrimPrefix(s, "# ")
    84  			parts := strings.SplitN(s, "=", 2)
    85  			s = fmt.Sprintf("# %s=%s", console.Colorize("EnvVar", parts[0]), parts[1])
    86  			lines = append(lines, s)
    87  		} else {
    88  			lines = append(lines, s)
    89  		}
    90  		if e == io.EOF {
    91  			break
    92  		}
    93  		fatalIf(probe.NewError(e), "Unable to marshal to string.")
    94  	}
    95  	return strings.Join(lines, "")
    96  }
    97  
    98  // JSON jsonified service status Message message.
    99  func (u configGetMessage) JSON() string {
   100  	u.Status = "success"
   101  	var err error
   102  	u.Config, err = madmin.ParseServerConfigOutput(string(u.value))
   103  	fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
   104  
   105  	statusJSONBytes, e := json.MarshalIndent(u, "", " ")
   106  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
   107  
   108  	return string(statusJSONBytes)
   109  }
   110  
   111  // checkAdminConfigGetSyntax - validate all the passed arguments
   112  func checkAdminConfigGetSyntax(ctx *cli.Context) {
   113  	if !ctx.Args().Present() || len(ctx.Args()) < 1 {
   114  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
   115  	}
   116  }
   117  
   118  func mainAdminConfigGet(ctx *cli.Context) error {
   119  	checkAdminConfigGetSyntax(ctx)
   120  
   121  	// Get the alias parameter from cli
   122  	args := ctx.Args()
   123  	aliasedURL := args.Get(0)
   124  
   125  	// Create a new MinIO Admin Client
   126  	client, err := newAdminClient(aliasedURL)
   127  	fatalIf(err, "Unable to initialize admin connection.")
   128  
   129  	if len(ctx.Args()) == 1 {
   130  		// Call get config API
   131  		hr, e := client.HelpConfigKV(globalContext, "", "", false)
   132  		fatalIf(probe.NewError(e), "Unable to get help for the sub-system")
   133  
   134  		// Print
   135  		printMsg(configHelpMessage{
   136  			Value:   hr,
   137  			envOnly: false,
   138  		})
   139  
   140  		return nil
   141  	}
   142  
   143  	subSys := strings.Join(args.Tail(), " ")
   144  
   145  	// Call get config API
   146  	buf, e := client.GetConfigKV(globalContext, subSys)
   147  	fatalIf(probe.NewError(e), "Unable to get server '%s' config", args.Tail())
   148  
   149  	if globalJSON {
   150  		printMsg(configGetMessage{
   151  			value: buf,
   152  		})
   153  	} else {
   154  		// Print
   155  		printMsg(configGetMessage{
   156  			value: buf,
   157  		})
   158  	}
   159  
   160  	return nil
   161  }