2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
2024-07-18 01:26:35 +02:00
|
|
|
"context"
|
2022-10-18 03:24:12 +02:00
|
|
|
"fmt"
|
2024-07-18 20:39:18 +02:00
|
|
|
"os"
|
2022-10-18 03:24:12 +02:00
|
|
|
"strconv"
|
2024-07-18 20:39:18 +02:00
|
|
|
"text/template"
|
2022-10-18 03:24:12 +02:00
|
|
|
|
2024-07-18 01:26:35 +02:00
|
|
|
"github.com/urfave/cli/v3"
|
2023-12-08 10:36:53 +02:00
|
|
|
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
|
2024-07-18 20:39:18 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
|
2022-10-18 03:24:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var pipelineLogsCmd = &cli.Command{
|
|
|
|
Name: "logs",
|
|
|
|
Usage: "show pipeline logs",
|
2024-07-19 09:29:22 +02:00
|
|
|
ArgsUsage: "<repo-id|repo-full-name> <pipeline> [step-number|step-name]",
|
|
|
|
Action: pipelineLogs,
|
2022-10-18 03:24:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:26:35 +02:00
|
|
|
func pipelineLogs(ctx context.Context, c *cli.Command) error {
|
2023-06-13 01:07:52 +02:00
|
|
|
repoIDOrFullName := c.Args().First()
|
2024-07-18 01:26:35 +02:00
|
|
|
client, err := internal.NewClient(ctx, c)
|
2022-10-18 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-18 20:39:18 +02:00
|
|
|
if len(repoIDOrFullName) == 0 {
|
|
|
|
return fmt.Errorf("missing required argument repo-id / repo-full-name")
|
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
|
2022-10-18 03:24:12 +02:00
|
|
|
if err != nil {
|
2024-07-18 20:39:18 +02:00
|
|
|
return fmt.Errorf("invalid repo '%s': %w ", repoIDOrFullName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineArg := c.Args().Get(1)
|
|
|
|
if len(pipelineArg) == 0 {
|
|
|
|
return fmt.Errorf("missing required argument pipeline")
|
|
|
|
}
|
|
|
|
number, err := strconv.ParseInt(pipelineArg, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid pipeline '%s': %w", pipelineArg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stepArg := c.Args().Get(2) //nolint:mnd
|
|
|
|
if len(stepArg) == 0 {
|
|
|
|
return showPipelineLog(client, repoID, number)
|
|
|
|
}
|
|
|
|
|
|
|
|
step, err := internal.ParseStep(client, repoID, number, stepArg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid step '%s': %w", stepArg, err)
|
2022-10-18 03:24:12 +02:00
|
|
|
}
|
2024-07-18 20:39:18 +02:00
|
|
|
return showStepLog(client, repoID, number, step)
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
|
2024-07-18 20:39:18 +02:00
|
|
|
func showPipelineLog(client woodpecker.Client, repoID, number int64) error {
|
|
|
|
pipeline, err := client.Pipeline(repoID, number)
|
2022-10-18 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:39:18 +02:00
|
|
|
tmpl, err := template.New("_").Parse(tmplPipelineLogs + "\n")
|
2022-10-18 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:39:18 +02:00
|
|
|
for _, workflow := range pipeline.Workflows {
|
|
|
|
for _, step := range workflow.Children {
|
|
|
|
if err := tmpl.Execute(os.Stdout, map[string]any{"workflow": workflow, "step": step}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := showStepLog(client, repoID, number, step.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func showStepLog(client woodpecker.Client, repoID, number, step int64) error {
|
2023-06-13 01:07:52 +02:00
|
|
|
logs, err := client.StepLogEntries(repoID, number, step)
|
2022-10-18 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, log := range logs {
|
2024-07-18 20:39:18 +02:00
|
|
|
fmt.Println(string(log.Data))
|
2022-10-18 03:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-18 20:39:18 +02:00
|
|
|
|
|
|
|
// template for pipeline ps information.
|
|
|
|
var tmplPipelineLogs = "\x1b[33m{{ .workflow.Name }} > {{ .step.Name }} (#{{ .step.PID }}):\x1b[0m"
|