MCPcopy Create free account
hub / github.com/OctopusDeploy/cli / apiRun

Function apiRun

pkg/cmd/api/api.go:42–81  ·  view source on GitHub ↗
(cmd *cobra.Command, f factory.Factory, path string)

Source from the content-addressed store, hash-verified

40}
41
42func apiRun(cmd *cobra.Command, f factory.Factory, path string) error {
43 if err := validateAPIPath(path); err != nil {
44 return err
45 }
46
47 client, err := f.GetSystemClient(apiclient.NewRequester(cmd))
48 if err != nil {
49 return err
50 }
51
52 req, err := http.NewRequest("GET", path, nil)
53 if err != nil {
54 return err
55 }
56
57 resp, err := client.HttpSession().DoRawRequest(req)
58 if err != nil {
59 return err
60 }
61 defer resp.Body.Close()
62
63 body, err := io.ReadAll(resp.Body)
64 if err != nil {
65 return err
66 }
67
68 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
69 return errors.New(string(body))
70 }
71
72 // Pretty-print if valid JSON, otherwise output raw
73 var prettyJSON bytes.Buffer
74 if err := json.Indent(&prettyJSON, body, "", " "); err == nil {
75 cmd.Println(prettyJSON.String())
76 } else {
77 cmd.Print(string(body))
78 }
79
80 return nil
81}
82
83func validateAPIPath(path string) error {
84 trimmed := strings.TrimLeft(path, "/")

Callers 1

NewCmdAPIFunction · 0.85

Calls 5

NewRequesterFunction · 0.92
validateAPIPathFunction · 0.85
GetSystemClientMethod · 0.65
PrintMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected