github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/batch-list.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 "strings" 23 24 humanize "github.com/dustin/go-humanize" 25 "github.com/minio/cli" 26 json "github.com/minio/colorjson" 27 "github.com/minio/madmin-go/v3" 28 "github.com/minio/mc/pkg/probe" 29 "github.com/olekukonko/tablewriter" 30 ) 31 32 var batchListFlags = []cli.Flag{ 33 cli.StringFlag{ 34 Name: "type", 35 Usage: "list all current batch jobs via job type", 36 }, 37 } 38 39 var batchListCmd = cli.Command{ 40 Name: "list", 41 ShortName: "ls", 42 Usage: "list all current batch jobs", 43 Action: mainBatchList, 44 OnUsageError: onUsageError, 45 Before: setGlobalsFromContext, 46 Flags: append(batchListFlags, globalFlags...), 47 CustomHelpTemplate: `NAME: 48 {{.HelpName}} - {{.Usage}} 49 50 USAGE: 51 {{.HelpName}} TARGET 52 53 FLAGS: 54 {{range .VisibleFlags}}{{.}} 55 {{end}} 56 EXAMPLES: 57 1. List all current batch jobs: 58 {{.Prompt}} {{.HelpName}} myminio 59 60 2. List all current batch jobs of type 'replicate': 61 {{.Prompt}} {{.HelpName}} myminio/ --type "replicate" 62 `, 63 } 64 65 // batchListMessage container for file batchList messages 66 type batchListMessage struct { 67 Status string `json:"status"` 68 Jobs []madmin.BatchJobResult `json:"jobs"` 69 } 70 71 // String colorized batchList message 72 func (c batchListMessage) String() string { 73 if len(c.Jobs) == 0 { 74 return "currently no jobs are running" 75 } 76 77 var s strings.Builder 78 79 // Set table header 80 table := tablewriter.NewWriter(&s) 81 table.SetAutoWrapText(false) 82 table.SetAutoFormatHeaders(true) 83 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) 84 table.SetAlignment(tablewriter.ALIGN_LEFT) 85 table.SetCenterSeparator("") 86 table.SetColumnSeparator("") 87 table.SetRowSeparator("") 88 table.SetHeaderLine(false) 89 table.SetBorder(false) 90 table.SetTablePadding("\t") // pad with tabs 91 table.SetNoWhiteSpace(true) 92 93 table.SetHeader([]string{"ID", "TYPE", "USER", "STARTED"}) 94 data := make([][]string, 0, 4) 95 96 for _, job := range c.Jobs { 97 data = append(data, []string{ 98 job.ID, 99 string(job.Type), 100 job.User, 101 humanize.Time(job.Started), 102 }) 103 } 104 105 table.AppendBulk(data) 106 table.Render() 107 108 return s.String() 109 } 110 111 // JSON jsonified batchList message 112 func (c batchListMessage) JSON() string { 113 c.Status = "success" 114 batchListMessageBytes, e := json.MarshalIndent(c, "", " ") 115 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 116 117 return string(batchListMessageBytes) 118 } 119 120 // checkBatchListSyntax - validate all the passed arguments 121 func checkBatchListSyntax(ctx *cli.Context) { 122 if len(ctx.Args()) != 1 { 123 showCommandHelpAndExit(ctx, 1) // last argument is exit code 124 } 125 } 126 127 // mainBatchList is the handle for "mc batch create" command. 128 func mainBatchList(ctx *cli.Context) error { 129 checkBatchListSyntax(ctx) 130 131 // Get the alias parameter from cli 132 args := ctx.Args() 133 aliasedURL := args.Get(0) 134 135 // Start a new MinIO Admin Client 136 adminClient, err := newAdminClient(aliasedURL) 137 fatalIf(err, "Unable to initialize admin connection.") 138 139 ctxt, cancel := context.WithCancel(globalContext) 140 defer cancel() 141 142 res, e := adminClient.ListBatchJobs(ctxt, &madmin.ListBatchJobsFilter{ 143 ByJobType: ctx.String("type"), 144 }) 145 fatalIf(probe.NewError(e), "Unable to list jobs") 146 147 printMsg(batchListMessage{ 148 Status: "success", 149 Jobs: res.Jobs, 150 }) 151 return nil 152 }