/
update_pipeline_file.go
67 lines (53 loc) · 1.66 KB
/
update_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
package pipeline
import (
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
)
func NewCmdUpdatePipelineFile(config *cfg.Config) *cobra.Command {
var pipelineKey string
var file string
var encrypt bool
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_file",
Short: "Update a file from a pipeline by its name",
RunE: func(cmd *cobra.Command, args []string) error {
contents, err := cfg.ReadFile(file)
if err != nil {
return err
}
name := filepath.Base(file)
name = strings.TrimSuffix(name, filepath.Ext(name))
pipelineID, err := completer.LoadPipelineID(pipelineKey)
if err != nil {
return err
}
ff, err := config.Cloud.PipelineFiles(config.Ctx, pipelineID, cloud.PipelineFilesParams{})
if err != nil {
return err
}
for _, f := range ff.Items {
if f.Name == name {
return config.Cloud.UpdatePipelineFile(config.Ctx, f.ID, cloud.UpdatePipelineFile{
Contents: &contents,
Encrypted: &encrypt,
})
}
}
return fmt.Errorf("pipeline file %q not found", name)
},
}
fs := cmd.Flags()
fs.StringVar(&pipelineKey, "pipeline", "", "Parent pipeline ID or name")
fs.StringVar(&file, "file", "", "File path. The file you want to update. It must exists already.")
fs.BoolVar(&encrypt, "encrypt", false, "Encrypt file contents")
_ = cmd.RegisterFlagCompletionFunc("pipeline", completer.CompletePipelines)
_ = cmd.MarkFlagRequired("file")
_ = cmd.MarkFlagRequired("pipeline") // TODO: use default pipeline key from config cmd.
return cmd
}