From 63ea185b4bd6a3661f2b31f4561e7b81f0de8530 Mon Sep 17 00:00:00 2001 From: Lucas Pinheiro Date: Thu, 20 Apr 2023 11:35:07 -0300 Subject: [PATCH] feat: add troubleshoot command (#214) --- api/client/troubleshoot_v1_alpha.go | 69 ++++++++++++++++++++++++ api/models/troubleshoot_v1_alpha.go | 33 ++++++++++++ cmd/troubleshoot.go | 81 +++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 api/client/troubleshoot_v1_alpha.go create mode 100644 api/models/troubleshoot_v1_alpha.go create mode 100644 cmd/troubleshoot.go diff --git a/api/client/troubleshoot_v1_alpha.go b/api/client/troubleshoot_v1_alpha.go new file mode 100644 index 0000000..99c38c3 --- /dev/null +++ b/api/client/troubleshoot_v1_alpha.go @@ -0,0 +1,69 @@ +package client + +import ( + "fmt" + + models "github.com/semaphoreci/cli/api/models" +) + +type TroubleshootApiV1AlphaApi struct { + BaseClient BaseClient + ResourceNameSingular string + ResourceNamePlural string +} + +func NewTroubleshootV1AlphaApi() TroubleshootApiV1AlphaApi { + baseClient := NewBaseClientFromConfig() + baseClient.SetApiVersion("v1alpha") + + return TroubleshootApiV1AlphaApi{ + BaseClient: baseClient, + ResourceNamePlural: "troubleshoot", + ResourceNameSingular: "troubleshoot", + } +} + +func (c *TroubleshootApiV1AlphaApi) TroubleshootWorkflow(workflowID string) (*models.TroubleshootV1Alpha, error) { + urlEncode := fmt.Sprintf("%s/workflow", c.ResourceNamePlural) + body, status, err := c.BaseClient.Get(urlEncode, workflowID) + + if err != nil { + return nil, fmt.Errorf("connecting to Semaphore failed '%s'", err) + } + + if status != 200 { + return nil, fmt.Errorf("http status %d with message \"%s\" received from upstream", status, body) + } + + return models.NewTroubleshootV1AlphaFromJson(body) +} + +func (c *TroubleshootApiV1AlphaApi) TroubleshootJob(jobID string) (*models.TroubleshootV1Alpha, error) { + urlEncode := fmt.Sprintf("%s/job", c.ResourceNamePlural) + body, status, err := c.BaseClient.Get(urlEncode, jobID) + + if err != nil { + return nil, fmt.Errorf("connecting to Semaphore failed '%s'", err) + } + + if status != 200 { + return nil, fmt.Errorf("http status %d with message \"%s\" received from upstream", status, body) + } + + return models.NewTroubleshootV1AlphaFromJson(body) +} + +func (c *TroubleshootApiV1AlphaApi) TroubleshootPipeline(pplID string) (*models.TroubleshootV1Alpha, error) { + urlEncode := fmt.Sprintf("%s/pipeline/", c.ResourceNamePlural) + body, status, err := c.BaseClient.Get(urlEncode, pplID) + + if err != nil { + return nil, fmt.Errorf("connecting to Semaphore failed '%s'", err) + } + + if status != 200 { + return nil, fmt.Errorf("http status %d with message \"%s\" received from upstream", status, body) + } + + return models.NewTroubleshootV1AlphaFromJson(body) +} diff --git a/api/models/troubleshoot_v1_alpha.go b/api/models/troubleshoot_v1_alpha.go new file mode 100644 index 0000000..c4df751 --- /dev/null +++ b/api/models/troubleshoot_v1_alpha.go @@ -0,0 +1,33 @@ +package models + +import ( + "encoding/json" + + yaml "gopkg.in/yaml.v2" +) + +type TroubleshootV1Alpha struct { + Workflow map[string]interface{} `json:"workflow,omitempty" yaml:"workflow,omitempty"` + Project map[string]interface{} `json:"project,omitempty" yaml:"project,omitempty"` + Pipeline map[string]interface{} `json:"pipeline,omitempty" yaml:"pipeline,omitempty"` + Job map[string]interface{} `json:"job,omitempty" yaml:"job,omitempty"` + Block map[string]interface{} `json:"block,omitempty" yaml:"block,omitempty"` +} + +func NewTroubleshootV1AlphaFromJson(data []byte) (*TroubleshootV1Alpha, error) { + t := TroubleshootV1Alpha{} + err := json.Unmarshal(data, &t) + if err != nil { + return nil, err + } + + return &t, nil +} + +func (j *TroubleshootV1Alpha) ToJson() ([]byte, error) { + return json.Marshal(j) +} + +func (j *TroubleshootV1Alpha) ToYaml() ([]byte, error) { + return yaml.Marshal(j) +} diff --git a/cmd/troubleshoot.go b/cmd/troubleshoot.go new file mode 100644 index 0000000..a761e96 --- /dev/null +++ b/cmd/troubleshoot.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "fmt" + + client "github.com/semaphoreci/cli/api/client" + "github.com/semaphoreci/cli/cmd/utils" + "github.com/spf13/cobra" +) + +var troubleshootCmd = &cobra.Command{ + Use: "troubleshoot [KIND]", + Short: "Troubleshoot resource.", + Long: ``, + Args: cobra.RangeArgs(1, 2), +} + +var troubleshootPipelineCmd = &cobra.Command{ + Use: "pipeline [id]", + Short: "Troubleshoot pipeline.", + Long: ``, + Aliases: []string{"pipelines", "ppl"}, + Args: cobra.ExactArgs(1), + + Run: func(cmd *cobra.Command, args []string) { + id := args[0] + troubleshootClient := client.NewTroubleshootV1AlphaApi() + t, err := troubleshootClient.TroubleshootPipeline(id) + utils.Check(err) + + v, err := t.ToYaml() + utils.Check(err) + fmt.Printf("%s", v) + }, +} + +var troubleshootJobCmd = &cobra.Command{ + Use: "job [id]", + Short: "Troubleshoot job.", + Long: ``, + Aliases: []string{"jobs"}, + Args: cobra.ExactArgs(1), + + Run: func(cmd *cobra.Command, args []string) { + id := args[0] + troubleshootClient := client.NewTroubleshootV1AlphaApi() + t, err := troubleshootClient.TroubleshootJob(id) + utils.Check(err) + + v, err := t.ToYaml() + utils.Check(err) + fmt.Printf("%s", v) + }, +} + +var troubleshootWorkflowCmd = &cobra.Command{ + Use: "workflow [id]", + Short: "Troubleshoot workflow.", + Long: ``, + Aliases: []string{"workflows", "wf"}, + Args: cobra.ExactArgs(1), + + Run: func(cmd *cobra.Command, args []string) { + id := args[0] + troubleshootClient := client.NewTroubleshootV1AlphaApi() + t, err := troubleshootClient.TroubleshootWorkflow(id) + utils.Check(err) + + v, err := t.ToYaml() + utils.Check(err) + fmt.Printf("%s", v) + }, +} + +func init() { + RootCmd.AddCommand(troubleshootCmd) + + troubleshootCmd.AddCommand(troubleshootPipelineCmd) + troubleshootCmd.AddCommand(troubleshootJobCmd) + troubleshootCmd.AddCommand(troubleshootWorkflowCmd) +}