/
delete_core_instance_file.go
109 lines (89 loc) · 2.62 KB
/
delete_core_instance_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
package coreinstance
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"golang.org/x/term"
"gopkg.in/yaml.v2"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/confirm"
"github.com/calyptia/cli/formatters"
)
func NewCmdDeleteCoreInstanceFile(config *cfg.Config) *cobra.Command {
loader := completer.Completer{Config: config}
var confirmed bool
var instanceKey string
var name string
cmd := &cobra.Command{
Use: "core_instance_file", // delete
Short: "Delete core instance file",
Long: "Delete a file within a core instance",
RunE: func(cmd *cobra.Command, args []string) error {
name := filepath.Base(name)
name = strings.TrimSuffix(name, filepath.Ext(name))
if !confirmed {
cmd.Printf("Are you sure you want to delete file %q? (y/N) ", name)
confirmed, err := confirm.Read(cmd.InOrStdin())
if err != nil {
return err
}
if !confirmed {
cmd.Println("Aborted")
return nil
}
}
instanceID, err := loader.LoadCoreInstanceID(instanceKey, "")
if err != nil {
return err
}
ctx := cmd.Context()
files, err := config.Cloud.CoreInstanceFiles(ctx, types.ListCoreInstanceFiles{
CoreInstanceID: instanceID,
})
if err != nil {
return err
}
var fileID string
for _, f := range files.Items {
if f.Name == name {
fileID = f.ID
break
}
}
if fileID == "" {
cmd.Println("File not found.")
return nil
}
out, err := config.Cloud.DeleteCoreInstanceFile(ctx, fileID)
if err != nil {
return err
}
fs := cmd.Flags()
outputFormat := formatters.OutputFormatFromFlags(fs)
if fn, ok := formatters.ShouldApplyTemplating(outputFormat); ok {
return fn(cmd.OutOrStdout(), formatters.TemplateFromFlags(fs), out)
}
switch outputFormat {
case formatters.OutputFormatJSON:
return json.NewEncoder(cmd.OutOrStdout()).Encode(out)
case formatters.OutputFormatYAML:
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(out)
default:
return formatters.RenderDeleted(cmd.OutOrStdout(), out)
}
},
}
isNonInteractive := os.Stdin == nil || !term.IsTerminal(int(os.Stdin.Fd()))
fs := cmd.Flags()
fs.BoolVarP(&confirmed, "yes", "y", isNonInteractive, "Confirm deletion")
fs.StringVar(&instanceKey, "core-instance", "", "Parent core instance ID or name")
fs.StringVar(&name, "name", "", "Name of the file to delete")
_ = cmd.RegisterFlagCompletionFunc("core-instance", loader.CompleteCoreInstances)
_ = cmd.MarkFlagRequired("core-instance")
_ = cmd.MarkFlagRequired("name")
return cmd
}