-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_pipeline_file.go
176 lines (151 loc) · 5.43 KB
/
get_pipeline_file.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pipeline
import (
"encoding/json"
"fmt"
"io"
"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 NewCmdGetPipelineFiles(config *cfg.Config) *cobra.Command {
var pipelineKey string
var last uint
var outputFormat, goTemplate string
var showIDs bool
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_files",
Short: "Get pipeline files",
RunE: func(cmd *cobra.Command, args []string) error {
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return err
}
ff, err := config.Cloud.PipelineFiles(config.Ctx, pipelineID, cloud.PipelineFilesParams{
Last: &last,
})
if err != nil {
return fmt.Errorf("could not fetch your pipeline files: %w", err)
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, ff.Items)
}
switch outputFormat {
case "table":
renderPipelineFiles(cmd.OutOrStdout(), ff.Items, showIDs)
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(ff.Items)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(ff.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 files. 0 means no limit")
fs.BoolVar(&showIDs, "show-ids", false, "Include status IDs in table output")
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 NewCmdGetPipelineFile(config *cfg.Config) *cobra.Command {
var pipelineKey string
var name string
var outputFormat, goTemplate string
var showIDs, onlyContents bool
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_file",
Short: "Get a single file from a pipeline by its name",
RunE: func(cmd *cobra.Command, args []string) error {
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return err
}
ff, err := config.Cloud.PipelineFiles(config.Ctx, pipelineID, cloud.PipelineFilesParams{})
if err != nil {
return fmt.Errorf("could not find your pipeline file by name: %w", err)
}
var file cloud.PipelineFile
var found bool
for _, f := range ff.Items {
if f.Name == name {
file = f
found = true
break
}
}
if !found {
return fmt.Errorf("could not find pipeline file %q", name)
}
if onlyContents {
if !file.Encrypted {
cmd.Print(string(file.Contents))
} else {
cmd.Print(file.Contents)
}
return nil
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, file)
}
switch outputFormat {
case "table":
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 4, 1, ' ', 0)
if showIDs {
fmt.Fprint(tw, "ID\t")
}
fmt.Fprintln(tw, "NAME\tENCRYPTED\tAGE")
if showIDs {
fmt.Fprintf(tw, "%s\t", file.ID)
}
fmt.Fprintf(tw, "%s\t%v\t%s\n", file.Name, file.Encrypted, formatters.FmtTime(file.CreatedAt))
tw.Flush()
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(file)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(file)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
return nil
},
}
fs := cmd.Flags()
fs.StringVar(&pipelineKey, "pipeline", "", "Parent pipeline ID or name")
fs.StringVar(&name, "name", "", "File name")
fs.BoolVar(&showIDs, "show-ids", false, "Include status IDs in table output")
fs.BoolVar(&onlyContents, "only-contents", false, "Only print file contents")
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("pipeline", completer.CompletePipelines)
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
_ = cmd.MarkFlagRequired("pipeline") // TODO: use default pipeline key from config cmd.
_ = cmd.MarkFlagRequired("name")
return cmd
}
func renderPipelineFiles(w io.Writer, ff []cloud.PipelineFile, showIDs bool) {
tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0)
if showIDs {
fmt.Fprint(tw, "ID\t")
}
fmt.Fprintln(tw, "NAME\tENCRYPTED\tAGE")
for _, f := range ff {
if showIDs {
fmt.Fprintf(tw, "%s\t", f.ID)
}
fmt.Fprintf(tw, "%s\t%v\t%s\n", f.Name, f.Encrypted, formatters.FmtTime(f.CreatedAt))
}
tw.Flush()
}