github.com/jfrog/jfrog-cli-platform-services@v1.2.0/commands/list_cmd.go (about) 1 package commands 2 3 import ( 4 "encoding/csv" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9 "strings" 10 11 plugins_common "github.com/jfrog/jfrog-cli-core/v2/plugins/common" 12 "github.com/jfrog/jfrog-cli-core/v2/plugins/components" 13 "github.com/jfrog/jfrog-client-go/utils/log" 14 15 "github.com/jfrog/jfrog-cli-platform-services/model" 16 ) 17 18 type getAllResponse struct { 19 Workers []*model.WorkerDetails `json:"workers"` 20 } 21 22 func GetListCommand() components.Command { 23 return components.Command{ 24 Name: "list", 25 Description: "List workers. The default output is a CSV format with columns <name>,<action>,<description>,<enabled>.", 26 Aliases: []string{"ls"}, 27 Flags: []components.Flag{ 28 plugins_common.GetServerIdFlag(), 29 model.GetJsonOutputFlag("Use JSON instead of CSV as output"), 30 model.GetTimeoutFlag(), 31 }, 32 Arguments: []components.Argument{ 33 { 34 Name: "action", 35 Description: fmt.Sprintf("Only show workers of this type.\n\t\tShould be one of (%s).", strings.Join(strings.Split(model.ActionNames(), "|"), ", ")), 36 Optional: true, 37 }, 38 }, 39 Action: func(c *components.Context) error { 40 server, err := model.GetServerDetails(c) 41 if err != nil { 42 return err 43 } 44 return runListCommand(c, server.GetUrl(), server.GetAccessToken()) 45 }, 46 } 47 } 48 49 func runListCommand(ctx *components.Context, serverUrl string, token string) error { 50 api := "workers" 51 52 if len(ctx.Arguments) > 0 { 53 api = fmt.Sprintf("%s?action=%s", api, url.QueryEscape(ctx.Arguments[0])) 54 } 55 56 res, discardReq, err := callWorkerApi(ctx, serverUrl, token, http.MethodGet, nil, api) 57 if discardReq != nil { 58 defer discardReq() 59 } 60 if err != nil { 61 return err 62 } 63 64 if ctx.GetBoolFlagValue(model.FlagJsonOutput) { 65 return outputApiResponse(res, http.StatusOK) 66 } 67 68 return formatListResponseAsCsv(res, http.StatusOK) 69 } 70 71 func formatListResponseAsCsv(res *http.Response, okStatus int) error { 72 return processApiResponse(res, func(responseBytes []byte, statusCode int) error { 73 var err error 74 75 if res.StatusCode != okStatus { 76 err = fmt.Errorf("command failed with status %d", res.StatusCode) 77 } 78 79 if err == nil { 80 allWorkers := getAllResponse{} 81 82 err = json.Unmarshal(responseBytes, &allWorkers) 83 if err != nil { 84 return nil 85 } 86 87 writer := csv.NewWriter(cliOut) 88 89 for _, wk := range allWorkers.Workers { 90 err = writer.Write([]string{ 91 wk.Key, wk.Action, wk.Description, fmt.Sprint(wk.Enabled), 92 }) 93 if err != nil { 94 return err 95 } 96 } 97 98 writer.Flush() 99 100 return writer.Error() 101 } else if len(responseBytes) > 0 { 102 // We will report the previous error, but we still want to display the response body 103 if _, writeErr := cliOut.Write(prettifyJson(responseBytes)); writeErr != nil { 104 log.Debug(fmt.Sprintf("Write error: %+v", writeErr)) 105 } 106 } 107 108 return err 109 }) 110 }