/
delete_pipeline_file.go
73 lines (58 loc) · 1.76 KB
/
delete_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
package pipeline
import (
"fmt"
"strings"
"github.com/spf13/cobra"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
)
func NewCmdDeletePipelineFile(config *cfg.Config) *cobra.Command {
var confirmed bool
var pipelineKey string
var name string
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "pipeline_file",
Short: "Delete a single file from a pipeline by its name",
RunE: func(cmd *cobra.Command, args []string) error {
if !confirmed {
cmd.Printf("Are you sure you want to delete %q? (y/N) ", pipelineKey)
var answer string
_, err := fmt.Scanln(&answer)
if err != nil && err.Error() == "unexpected newline" {
err = nil
}
if err != nil {
return fmt.Errorf("could not to read answer: %v", err)
}
answer = strings.TrimSpace(strings.ToLower(answer))
if answer != "y" && answer != "yes" {
return nil
}
}
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.DeletePipelineFile(config.Ctx, f.ID)
}
}
return nil
},
}
fs := cmd.Flags()
fs.BoolVarP(&confirmed, "yes", "y", false, "Confirm deletion")
fs.StringVar(&pipelineKey, "pipeline", "", "Parent pipeline ID or name")
fs.StringVar(&name, "name", "", "File name you want to delete")
_ = cmd.RegisterFlagCompletionFunc("pipeline", completer.CompletePipelines)
_ = cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("pipeline") // TODO: use default pipeline key from config cmd.
return cmd
}