-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_pipeline_log.go
78 lines (64 loc) · 2.2 KB
/
create_pipeline_log.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
67
68
69
70
71
72
73
74
75
76
77
78
package pipeline
import (
"encoding/json"
"fmt"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdCreatePipelineLog(config *cfg.Config) *cobra.Command {
var pipelineKey string
var lines int
var outputFormat, goTemplate string
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_log",
Short: "Create a new log request within a pipeline",
RunE: func(cmd *cobra.Command, args []string) error {
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return err
}
params := cloud.CreatePipelineLog{
PipelineID: pipelineID,
}
if lines > 0 {
params.Lines = lines
}
out, err := config.Cloud.CreatePipelineLog(config.Ctx, params)
if err != nil {
return err
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, out)
}
switch outputFormat {
case "table":
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 4, 1, ' ', 0)
fmt.Fprintln(tw, "ID\tAGE")
fmt.Fprintf(tw, "%s\t%s\n", out.ID, formatters.FmtTime(out.CreatedAt))
tw.Flush()
return nil
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(out)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(out)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
},
}
fs := cmd.Flags()
fs.StringVar(&pipelineKey, "pipeline", "", "Pipeline ID or name")
fs.IntVar(&lines, "lines", 100, "Lines of logs to retrieve from the cluster")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
_ = cmd.MarkFlagRequired("pipeline")
_ = cmd.RegisterFlagCompletionFunc("pipeline", completer.CompletePipelines)
return cmd
}