github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-import.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  	"fmt"
    22  	"os"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	json "github.com/minio/colorjson"
    27  	"github.com/minio/mc/pkg/probe"
    28  	"github.com/minio/pkg/v2/console"
    29  )
    30  
    31  var adminConfigImportCmd = cli.Command{
    32  	Name:         "import",
    33  	Usage:        "import multiple config keys from STDIN",
    34  	Before:       setGlobalsFromContext,
    35  	Action:       mainAdminConfigImport,
    36  	OnUsageError: onUsageError,
    37  	Flags:        globalFlags,
    38  	CustomHelpTemplate: `NAME:
    39    {{.HelpName}} - {{.Usage}}
    40  
    41  USAGE:
    42    {{.HelpName}} TARGET
    43  
    44  FLAGS:
    45    {{range .VisibleFlags}}{{.}}
    46    {{end}}
    47  EXAMPLES:
    48    1. Import the new local config and apply to the MinIO server
    49       {{.Prompt}} {{.HelpName}} play/ < config.txt
    50  `,
    51  }
    52  
    53  // configImportMessage container to hold locks information.
    54  type configImportMessage struct {
    55  	Status      string `json:"status"`
    56  	targetAlias string
    57  }
    58  
    59  // String colorized service status message.
    60  func (u configImportMessage) String() (msg string) {
    61  	msg += console.Colorize("SetConfigSuccess",
    62  		"Setting new key has been successful.\n")
    63  	suggestion := fmt.Sprintf("mc admin service restart %s", u.targetAlias)
    64  	msg += console.Colorize("SetConfigSuccess",
    65  		fmt.Sprintf("Please restart your server with `%s`.\n", suggestion))
    66  	return msg
    67  }
    68  
    69  // JSON jsonified service status Message message.
    70  func (u configImportMessage) JSON() string {
    71  	u.Status = "success"
    72  	statusJSONBytes, e := json.MarshalIndent(u, "", " ")
    73  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    74  
    75  	return string(statusJSONBytes)
    76  }
    77  
    78  // checkAdminConfigImportSyntax - validate all the passed arguments
    79  func checkAdminConfigImportSyntax(ctx *cli.Context) {
    80  	if !ctx.Args().Present() || len(ctx.Args()) > 1 {
    81  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    82  	}
    83  }
    84  
    85  func mainAdminConfigImport(ctx *cli.Context) error {
    86  	checkAdminConfigImportSyntax(ctx)
    87  
    88  	// Set color preference of command outputs
    89  	console.SetColor("SetConfigSuccess", color.New(color.FgGreen, color.Bold))
    90  
    91  	// Import the alias parameter from cli
    92  	args := ctx.Args()
    93  	aliasedURL := args.Get(0)
    94  
    95  	// Create a new MinIO Admin Client
    96  	client, err := newAdminClient(aliasedURL)
    97  	fatalIf(err, "Unable to initialize admin connection.")
    98  
    99  	// Call set config API
   100  	fatalIf(probe.NewError(client.SetConfig(globalContext, os.Stdin)), "Unable to set server config")
   101  
   102  	// Print
   103  	printMsg(configImportMessage{
   104  		targetAlias: aliasedURL,
   105  	})
   106  
   107  	return nil
   108  }