diff --git a/api/bb-api.go b/api/bb-api.go index 44d306f..0de381c 100644 --- a/api/bb-api.go +++ b/api/bb-api.go @@ -361,17 +361,25 @@ func GetPipeline(repository string, id string) <-chan Pipeline { return channel } -func GetPipelineSteps(repository string, id string) <-chan PipelineStep { - channel := make(chan PipelineStep) +func GetPipelineSteps(repository string, id string) <-chan []PipelineStep { + channel := make(chan []PipelineStep) go func() { defer close(channel) var steps BBPaginatedResponse[PipelineStep] response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%s/steps", repository, id)) err := json.Unmarshal(response, &steps) cobra.CheckErr(err) - for _, step := range steps.Values { - channel <- step - } + channel <- steps.Values + }() + return channel +} + +func GetPipelineStepLogs(repository string, id string, stepUUID string) <-chan string { + channel := make(chan string) + go func() { + defer close(channel) + response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%s/steps/%s/log", repository, id, stepUUID)) + channel <- string(response) }() return channel } diff --git a/cmd/pipeline/logs.go b/cmd/pipeline/logs.go index 09bc7b6..8441a9d 100644 --- a/cmd/pipeline/logs.go +++ b/cmd/pipeline/logs.go @@ -2,7 +2,10 @@ package pipeline import ( "bb/api" + "bb/util" "fmt" + "strconv" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -11,11 +14,50 @@ import ( var LogsCmd = &cobra.Command{ Use: "logs", Short: "Show logs of a pipeline", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - api.GetPipelineList(viper.GetString("repo"), 0, "") - fmt.Println("Not implemented") + repo := viper.GetString("repo") + + var id int + var err error + if len(args) == 0 { + branch, err := util.GetCurrentBranch() + cobra.CheckErr(err) + // retrieve id of pr for current branch + pipeline := <-api.GetPipelineList(repo, 1, branch) + if pipeline.BuildNumber == 0 { + cobra.CheckErr("No pipelines found for this branch") + } + id = pipeline.BuildNumber + } else { + id, err = strconv.Atoi(args[0]) + cobra.CheckErr(err) + } + + var stepUUID = "" + steps := <-api.GetPipelineSteps(repo, fmt.Sprintf("%d", id)) + selectedStep, _ := cmd.Flags().GetString("step") + if selectedStep == "" { + optIndex := util.SelectFZF(steps, fmt.Sprintf("Step to Log > "), func(i int) string { + return fmt.Sprintf("%s", steps[i].Name) + }) + if len(optIndex) > 0 { + stepUUID = steps[optIndex[0]].UUID + } + } else { + for _, step := range steps { + if step.Name == selectedStep || strings.ToLower(step.Name) == selectedStep { + stepUUID = step.UUID + } + } + if stepUUID == "" { + cobra.CheckErr("Step not found") + } + } + fmt.Print(<-api.GetPipelineStepLogs(repo, fmt.Sprintf("%d", id), stepUUID)) }, } func init() { + LogsCmd.Flags().StringP("step", "s", "", "select step. Without this option the step is prompet interactively") } diff --git a/cmd/pipeline/view.go b/cmd/pipeline/view.go index 7eb6729..7a295f8 100644 --- a/cmd/pipeline/view.go +++ b/cmd/pipeline/view.go @@ -54,7 +54,7 @@ var ViewCmd = &cobra.Command{ fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name) fmt.Println() - for step := range stepsChannel { + for _, step := range <-stepsChannel { if step.State.Result.Name != "" { fmt.Printf("%s %s \033[37m%s\033[m", step.Name, util.FormatPipelineState(step.State.Result.Name), util.TimeDuration(time.Duration(step.DurationInSeconds*1000000000))) } else {