-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_pipeline_config.go
80 lines (67 loc) · 2.53 KB
/
get_pipeline_config.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
79
80
package pipeline
import (
"encoding/json"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdGetPipelineConfigHistory(config *cfg.Config) *cobra.Command {
var outputFormat, goTemplate string
var pipelineKey string
var last uint
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_config_history",
Short: "Display latest config history from a pipeline",
RunE: func(cmd *cobra.Command, args []string) error {
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return err
}
cc, err := config.Cloud.PipelineConfigHistory(config.Ctx, pipelineID, types.PipelineConfigHistoryParams{
Last: &last,
})
if err != nil {
return fmt.Errorf("could not fetch your pipeline config history: %w", err)
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, cc.Items)
}
switch outputFormat {
case "table":
renderPipelineConfigHistory(cmd.OutOrStdout(), cc.Items)
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(cc.Items)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(cc.Items)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
return nil
},
}
fs := cmd.Flags()
fs.StringVar(&pipelineKey, "pipeline", "", "Parent pipeline ID or name")
fs.UintVarP(&last, "last", "l", 0, "Last `N` pipeline config history entries. 0 means no limit")
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.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
_ = cmd.RegisterFlagCompletionFunc("pipeline", completer.CompletePipelines)
_ = cmd.MarkFlagRequired("pipeline") // TODO: use default pipeline key from config cmd.
return cmd
}
func renderPipelineConfigHistory(w io.Writer, cc []types.PipelineConfig) {
tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0)
fmt.Fprintln(tw, "ID\tAGE")
for _, c := range cc {
fmt.Fprintf(tw, "%s\t%s\n", c.ID, formatters.FmtTime(c.CreatedAt))
}
tw.Flush()
}