github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/batch-cancel.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 batchCancelFlags = []cli.Flag{
    32  	cli.StringFlag{
    33  		Name:  "id",
    34  		Usage: "job id",
    35  	},
    36  }
    37  
    38  var batchCancelCmd = cli.Command{
    39  	Name:         "cancel",
    40  	Usage:        "cancel ongoing batch job",
    41  	Action:       mainBatchCancel,
    42  	OnUsageError: onUsageError,
    43  	Before:       setGlobalsFromContext,
    44  	Flags:        append(batchCancelFlags, globalFlags...),
    45  	CustomHelpTemplate: `NAME:
    46    {{.HelpName}} - {{.Usage}}
    47  
    48  USAGE:
    49    {{.HelpName}} TARGET JOBFILE
    50  
    51  FLAGS:
    52    {{range .VisibleFlags}}{{.}}
    53    {{end}}
    54  EXAMPLES:
    55    1. Cancel ongoing batch job:
    56       {{.Prompt}} {{.HelpName}} myminio <job-id>
    57  `,
    58  }
    59  
    60  // batchCancelMessage container for file batchCancel messages
    61  type batchCancelMessage struct {
    62  	Status string `json:"status"`
    63  	JobID  string `json:"job-id"`
    64  }
    65  
    66  // String colorized batchCancel message
    67  func (c batchCancelMessage) String() string {
    68  	return console.Colorize("batchCancel", fmt.Sprintf("Successfully canceled batch job `%s`", c.JobID))
    69  }
    70  
    71  // JSON jsonified batchCancel message
    72  func (c batchCancelMessage) JSON() string {
    73  	c.Status = "success"
    74  	batchCancelMessageBytes, e := json.MarshalIndent(c, "", " ")
    75  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    76  
    77  	return string(batchCancelMessageBytes)
    78  }
    79  
    80  // checkBatchCancelSyntax - validate all the passed arguments
    81  func checkBatchCancelSyntax(ctx *cli.Context) {
    82  	if len(ctx.Args()) != 2 {
    83  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    84  	}
    85  }
    86  
    87  // mainBatchCancel is the handle for "mc batch cancel" command.
    88  func mainBatchCancel(ctx *cli.Context) error {
    89  	checkBatchCancelSyntax(ctx)
    90  
    91  	console.SetColor("BatchCancel", color.New(color.FgGreen, color.Bold))
    92  
    93  	// Get the alias parameter from cli
    94  	args := ctx.Args()
    95  	aliasedURL := args.Get(0)
    96  	jobID := args.Get(1)
    97  	// Start a new MinIO Admin Client
    98  	adminClient, err := newAdminClient(aliasedURL)
    99  	fatalIf(err, "Unable to initialize admin connection.")
   100  	ctxt, cancel := context.WithCancel(globalContext)
   101  	defer cancel()
   102  
   103  	e := adminClient.CancelBatchJob(ctxt, jobID)
   104  	fatalIf(probe.NewError(e), "Unable to cancel job")
   105  
   106  	printMsg(batchCancelMessage{
   107  		Status: "Canceled",
   108  		JobID:  jobID,
   109  	})
   110  	return nil
   111  }