github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/replicate-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  	"context"
    22  
    23  	"github.com/fatih/color"
    24  	"github.com/minio/cli"
    25  	json "github.com/minio/colorjson"
    26  	"github.com/minio/mc/pkg/probe"
    27  	"github.com/minio/minio-go/v7/pkg/replication"
    28  	"github.com/minio/pkg/v2/console"
    29  )
    30  
    31  var replicateExportCmd = cli.Command{
    32  	Name:         "export",
    33  	Usage:        "export server side replication configuration",
    34  	Action:       mainReplicateExport,
    35  	OnUsageError: onUsageError,
    36  	Before:       setGlobalsFromContext,
    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. Print replication configuration on bucket "mybucket" for alias "myminio" to STDOUT.
    49       {{.Prompt}} {{.HelpName}} myminio/mybucket
    50  
    51    2. Export replication configuration on bucket "mybucket" for alias "myminio" to '/data/replicate/config'.
    52       {{.Prompt}} {{.HelpName}} myminio/mybucket > /data/replicate/config
    53  `,
    54  }
    55  
    56  // checkReplicateExportSyntax - validate all the passed arguments
    57  func checkReplicateExportSyntax(ctx *cli.Context) {
    58  	if len(ctx.Args()) != 1 {
    59  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    60  	}
    61  }
    62  
    63  type replicateExportMessage struct {
    64  	Op                string             `json:"op"`
    65  	Status            string             `json:"status"`
    66  	URL               string             `json:"url"`
    67  	ReplicationConfig replication.Config `json:"config"`
    68  }
    69  
    70  func (r replicateExportMessage) JSON() string {
    71  	r.Status = "success"
    72  	jsonMessageBytes, e := json.MarshalIndent(r, "", " ")
    73  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    74  	return string(jsonMessageBytes)
    75  }
    76  
    77  func (r replicateExportMessage) String() string {
    78  	if r.ReplicationConfig.Empty() {
    79  		return console.Colorize("ReplicateNMessage", "No replication configuration found for "+r.URL+".")
    80  	}
    81  	msgBytes, e := json.MarshalIndent(r.ReplicationConfig, "", " ")
    82  	fatalIf(probe.NewError(e), "Unable to marshal replication configuration")
    83  	return string(msgBytes)
    84  }
    85  
    86  func mainReplicateExport(cliCtx *cli.Context) error {
    87  	ctx, cancelReplicateExport := context.WithCancel(globalContext)
    88  	defer cancelReplicateExport()
    89  
    90  	console.SetColor("replicateExportMessage", color.New(color.FgGreen))
    91  	console.SetColor("replicateExportFailure", color.New(color.FgRed))
    92  
    93  	checkReplicateExportSyntax(cliCtx)
    94  
    95  	// Get the alias parameter from cli
    96  	args := cliCtx.Args()
    97  	aliasedURL := args.Get(0)
    98  	// Create a new Client
    99  	client, err := newClient(aliasedURL)
   100  	fatalIf(err, "Unable to initialize connection.")
   101  	rCfg, err := client.GetReplication(ctx)
   102  	fatalIf(err.Trace(args...), "Unable to get replication configuration")
   103  	printMsg(replicateExportMessage{
   104  		Op:                cliCtx.Command.Name,
   105  		Status:            "success",
   106  		URL:               aliasedURL,
   107  		ReplicationConfig: rCfg,
   108  	})
   109  	return nil
   110  }