harness-drone/drone/build/build_logs.go

85 lines
1.5 KiB
Go
Raw Normal View History

2017-04-29 17:03:45 +00:00
package build
import (
"encoding/json"
"fmt"
"strconv"
2017-03-16 10:14:02 +00:00
"github.com/cncd/pipeline/pipeline/rpc"
2017-04-29 17:03:45 +00:00
"github.com/drone/drone/drone/internal"
2017-03-16 10:14:02 +00:00
"github.com/urfave/cli"
)
var buildLogsCmd = cli.Command{
Name: "logs",
Usage: "show build logs",
Action: buildLogsDisabled,
}
func buildLogsDisabled(c *cli.Context) error {
return fmt.Errorf("Command temporarily disabled. See https://github.com/drone/drone/issues/2005")
}
func buildLogs(c *cli.Context) error {
repo := c.Args().First()
2017-04-29 17:03:45 +00:00
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
2017-04-29 17:03:45 +00:00
client, err := internal.NewClient(c)
if err != nil {
return err
}
buildArg := c.Args().Get(1)
var number int
if buildArg == "" {
// Fetch the build number from the last build
build, err := client.BuildLast(owner, name, "")
if err != nil {
return err
}
number = build.Number
} else {
number, err = strconv.Atoi(buildArg)
if err != nil {
return fmt.Errorf("Error: Invalid number or missing job number. eg 100")
}
}
job, _ := strconv.Atoi(c.Args().Get(2))
if job == 0 {
job = 1
}
r, err := client.BuildLogs(owner, name, number, job)
if err != nil {
return err
}
defer r.Close()
dec := json.NewDecoder(r)
fmt.Printf("Logs for build %s/%s#%d.%d\n", owner, name, number, job)
2017-03-16 10:14:02 +00:00
var line rpc.Line
_, err = dec.Token()
if err != nil {
return err
}
for dec.More() {
if err = dec.Decode(&line); err != nil {
return err
}
fmt.Printf("%s", line.Out)
}
_, err = dec.Token()
if err != nil {
return err
}
return nil
}