github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/batch-generate.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 "strings" 23 24 "github.com/minio/cli" 25 "github.com/minio/madmin-go/v3" 26 "github.com/minio/mc/pkg/probe" 27 ) 28 29 var batchGenerateCmd = cli.Command{ 30 Name: "generate", 31 Usage: "generate a new batch job definition", 32 Action: mainBatchGenerate, 33 OnUsageError: onUsageError, 34 Before: setGlobalsFromContext, 35 Flags: globalFlags, 36 CustomHelpTemplate: `NAME: 37 {{.HelpName}} - {{.Usage}} 38 39 USAGE: 40 {{.HelpName}} TARGET JOBTYPE 41 42 JOBTYPE: 43 ` + supportedJobTypes() + ` 44 FLAGS: 45 {{range .VisibleFlags}}{{.}} 46 {{end}} 47 EXAMPLES: 48 1. Generate a new batch 'replication' job definition: 49 {{.Prompt}} {{.HelpName}} myminio replicate > replication.yaml 50 `, 51 } 52 53 func supportedJobTypes() string { 54 var builder strings.Builder 55 for _, jobType := range madmin.SupportedJobTypes { 56 builder.WriteString(" - ") 57 builder.WriteString(string(jobType)) 58 builder.WriteString("\n") 59 } 60 return builder.String() 61 } 62 63 // checkBatchGenerateSyntax - validate all the passed arguments 64 func checkBatchGenerateSyntax(ctx *cli.Context) { 65 if len(ctx.Args()) != 2 { 66 showCommandHelpAndExit(ctx, 1) // last argument is exit code 67 } 68 } 69 70 // mainBatchGenerate is the handle for "mc batch generate" command. 71 func mainBatchGenerate(ctx *cli.Context) error { 72 checkBatchGenerateSyntax(ctx) 73 74 // Get the alias parameter from cli 75 args := ctx.Args() 76 aliasedURL := args.Get(0) 77 jobType := args.Get(1) 78 79 // Start a new MinIO Admin Client 80 adminClient, err := newAdminClient(aliasedURL) 81 fatalIf(err, "Unable to initialize admin connection.") 82 83 var found bool 84 for _, job := range madmin.SupportedJobTypes { 85 if jobType == string(job) { 86 found = true 87 break 88 } 89 } 90 if !found { 91 fatalIf(errInvalidArgument().Trace(jobType), "Unable to generate a job template for the specified job type") 92 } 93 94 out, e := adminClient.GenerateBatchJob(globalContext, madmin.GenerateBatchJobOpts{ 95 Type: madmin.BatchJobType(jobType), 96 }) 97 fatalIf(probe.NewError(e), "Unable to generate %s", args.Get(1)) 98 99 fmt.Println(string(out)) 100 return nil 101 }