github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/replicate-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  	"context"
    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/minio-go/v7/pkg/replication"
    29  	"github.com/minio/pkg/v2/console"
    30  )
    31  
    32  var replicateImportCmd = cli.Command{
    33  	Name:         "import",
    34  	Usage:        "import server side replication configuration in JSON format",
    35  	Action:       mainReplicateImport,
    36  	OnUsageError: onUsageError,
    37  	Before:       setGlobalsFromContext,
    38  	Flags:        globalFlags,
    39  	CustomHelpTemplate: `NAME:
    40    {{.HelpName}} - {{.Usage}}
    41  
    42  USAGE:
    43    {{.HelpName}} TARGET
    44  
    45  FLAGS:
    46    {{range .VisibleFlags}}{{.}}
    47    {{end}}
    48  EXAMPLES:
    49    1. Set replication configuration from '/data/replication/config' on bucket "mybucket" for alias "myminio".
    50       {{.Prompt}} {{.HelpName}} myminio/mybucket < '/data/replication/config'
    51  
    52    2. Import replication configuration for bucket "mybucket" on alias "myminio" from STDIN.
    53       {{.Prompt}} {{.HelpName}} myminio/mybucket
    54  `,
    55  }
    56  
    57  // checkReplicateImportSyntax - validate all the passed arguments
    58  func checkReplicateImportSyntax(ctx *cli.Context) {
    59  	if len(ctx.Args()) != 1 {
    60  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    61  	}
    62  }
    63  
    64  type replicateImportMessage struct {
    65  	Op                string             `json:"op"`
    66  	Status            string             `json:"status"`
    67  	URL               string             `json:"url"`
    68  	ReplicationConfig replication.Config `json:"config"`
    69  }
    70  
    71  func (r replicateImportMessage) JSON() string {
    72  	r.Status = "success"
    73  	jsonMessageBytes, e := json.MarshalIndent(r, "", " ")
    74  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    75  	return string(jsonMessageBytes)
    76  }
    77  
    78  func (r replicateImportMessage) String() string {
    79  	return console.Colorize("replicateImportMessage", "Replication configuration successfully set on `"+r.URL+"`.")
    80  }
    81  
    82  // readReplicationConfig read from stdin, returns XML.
    83  func readReplicationConfig() (*replication.Config, *probe.Error) {
    84  	// User is expected to enter the replication configuration in JSON format
    85  	cfg := replication.Config{}
    86  
    87  	// Consume json from STDIN
    88  	dec := json.NewDecoder(os.Stdin)
    89  	if e := dec.Decode(&cfg); e != nil {
    90  		return &cfg, probe.NewError(e)
    91  	}
    92  
    93  	return &cfg, nil
    94  }
    95  
    96  func mainReplicateImport(cliCtx *cli.Context) error {
    97  	ctx, cancelReplicateImport := context.WithCancel(globalContext)
    98  	defer cancelReplicateImport()
    99  
   100  	console.SetColor("replicateImportMessage", color.New(color.FgGreen))
   101  	checkReplicateImportSyntax(cliCtx)
   102  
   103  	// Get the alias parameter from cli
   104  	args := cliCtx.Args()
   105  	aliasedURL := args.Get(0)
   106  	// Create a new Client
   107  	client, err := newClient(aliasedURL)
   108  	fatalIf(err, "Unable to initialize connection.")
   109  	rCfg, err := readReplicationConfig()
   110  	fatalIf(err.Trace(args...), "Unable to read replication configuration")
   111  
   112  	fatalIf(client.SetReplication(ctx, rCfg, replication.Options{Op: replication.ImportOption}).Trace(aliasedURL), "Unable to set replication configuration")
   113  	printMsg(replicateImportMessage{
   114  		Op:     cliCtx.Command.Name,
   115  		Status: "success",
   116  		URL:    aliasedURL,
   117  	})
   118  	return nil
   119  }