forked from openshift/origin
/
buildlogs.go
66 lines (53 loc) · 1.86 KB
/
buildlogs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
buildLogs_long = `Retrieve logs from the containers where the build occured.
NOTE: This command may be moved in the future.`
buildLogs_example = ` // Stream logs from container to stdout
$ %[1]s build-logs 566bed879d2d`
)
// NewCmdBuildLogs implements the OpenShift cli build-logs command
func NewCmdBuildLogs(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := api.BuildLogOptions{}
cmd := &cobra.Command{
Use: "build-logs BUILD",
Short: "Show container logs from the build container",
Long: buildLogs_long,
Example: fmt.Sprintf(buildLogs_example, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := RunBuildLogs(f, out, cmd, opts, args)
cmdutil.CheckErr(err)
},
}
cmd.Flags().BoolVarP(&opts.Follow, "follow", "f", true, "Specify whether logs should be followed; default is true.")
cmd.Flags().BoolVarP(&opts.NoWait, "nowait", "w", false, "Specify whether to return immediately without waiting for logs to be available; default is false.")
return cmd
}
// RunBuildLogs contains all the necessary functionality for the OpenShift cli build-logs command
func RunBuildLogs(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, opts api.BuildLogOptions, args []string) error {
if len(args) != 1 {
return cmdutil.UsageError(cmd, "A build name is required")
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
c, _, err := f.Clients()
if err != nil {
return err
}
readCloser, err := c.BuildLogs(namespace).Get(args[0], opts).Stream()
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err
}