github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/version-suspend.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  	"fmt"
    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 versionSuspendCmd = cli.Command{
    32  	Name:         "suspend",
    33  	Usage:        "suspend bucket versioning",
    34  	Action:       mainVersionSuspend,
    35  	OnUsageError: onUsageError,
    36  	Before:       setGlobalsFromContext,
    37  	Flags:        globalFlags,
    38  	CustomHelpTemplate: `NAME:
    39    {{.HelpName}} - {{.Usage}}
    40  
    41  USAGE:
    42    {{.HelpName}} ALIAS/BUCKET
    43  
    44  FLAGS:
    45    {{range .VisibleFlags}}{{.}}
    46    {{end}}
    47  EXAMPLES:
    48    1. Suspend versioning on bucket "mybucket" for alias "myminio".
    49       {{.Prompt}} {{.HelpName}} myminio/mybucket
    50  `,
    51  }
    52  
    53  // checkVersionSuspendSyntax - validate all the passed arguments
    54  func checkVersionSuspendSyntax(ctx *cli.Context) {
    55  	if len(ctx.Args()) != 1 {
    56  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    57  	}
    58  }
    59  
    60  type versionSuspendMessage struct {
    61  	Op         string
    62  	Status     string `json:"status"`
    63  	URL        string `json:"url"`
    64  	Versioning struct {
    65  		Status    string `json:"status"`
    66  		MFADelete string `json:"MFADelete"`
    67  	} `json:"versioning"`
    68  }
    69  
    70  func (v versionSuspendMessage) JSON() string {
    71  	v.Status = "success"
    72  	jsonMessageBytes, e := json.MarshalIndent(v, "", " ")
    73  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    74  	return string(jsonMessageBytes)
    75  }
    76  
    77  func (v versionSuspendMessage) String() string {
    78  	return console.Colorize("versionSuspendMessage", fmt.Sprintf("%s versioning is suspended", v.URL))
    79  }
    80  
    81  func mainVersionSuspend(cliCtx *cli.Context) error {
    82  	ctx, cancelVersionSuspend := context.WithCancel(globalContext)
    83  	defer cancelVersionSuspend()
    84  
    85  	console.SetColor("versionSuspendMessage", color.New(color.FgGreen))
    86  
    87  	checkVersionSuspendSyntax(cliCtx)
    88  
    89  	// Get the alias parameter from cli
    90  	args := cliCtx.Args()
    91  	aliasedURL := args.Get(0)
    92  
    93  	// Create a new Client
    94  	client, err := newClient(aliasedURL)
    95  	fatalIf(err, "Unable to initialize connection.")
    96  	fatalIf(client.SetVersion(ctx, "suspend", nil, false), "Unable to suspend versioning")
    97  	printMsg(versionSuspendMessage{
    98  		Op:     cliCtx.Command.Name,
    99  		Status: "success",
   100  		URL:    aliasedURL,
   101  	})
   102  	return nil
   103  }