/
delete.go
77 lines (65 loc) · 2.58 KB
/
delete.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
package cmd
import (
"github.com/ory/viper"
"github.com/spf13/cobra"
"github.com/boson-project/faas"
"github.com/boson-project/faas/knative"
"github.com/boson-project/faas/prompt"
)
func init() {
root.AddCommand(deleteCmd)
deleteCmd.Flags().BoolP("confirm", "c", false, "Prompt to confirm all configuration options - $FAAS_CONFIRM")
deleteCmd.Flags().StringP("path", "p", cwd(), "Path to the project which should be deleted - $FAAS_PATH")
deleteCmd.Flags().StringP("namespace", "n", "", "Override namespace in which to search for Functions. Default is to use currently active underlying platform setting - $FAAS_NAMESPACE")
}
var deleteCmd = &cobra.Command{
Use: "delete <name>",
Short: "Delete a Function deployment",
Long: `Removes the deployed Function by name, by explicit path, or by default for the current directory. No local files are deleted.`,
SuggestFor: []string{"remove", "rm", "del"},
ValidArgsFunction: CompleteFunctionList,
PreRunE: bindEnv("path", "confirm", "namespace"),
RunE: runDelete,
}
func runDelete(cmd *cobra.Command, args []string) (err error) {
config := newDeleteConfig(args).Prompt()
remover := knative.NewRemover(config.Namespace)
remover.Verbose = config.Verbose
function := faas.Function{Root: config.Path, Name: config.Name}
client := faas.New(
faas.WithVerbose(config.Verbose),
faas.WithRemover(remover))
return client.Remove(function)
}
type deleteConfig struct {
Name string
Namespace string
Path string
Verbose bool
}
// newDeleteConfig returns a config populated from the current execution context
// (args, flags and environment variables)
func newDeleteConfig(args []string) deleteConfig {
var name string
if len(args) > 0 {
name = args[0]
}
return deleteConfig{
Path: viper.GetString("path"),
Namespace: viper.GetString("namespace"),
Name: deriveName(name, viper.GetString("path")), // args[0] or derived
Verbose: viper.GetBool("verbose"), // defined on root
}
}
// Prompt the user with value of config members, allowing for interaractive changes.
// Skipped if not in an interactive terminal (non-TTY), or if --yes (agree to
// all prompts) was explicitly set.
func (c deleteConfig) Prompt() deleteConfig {
if !interactiveTerminal() || !viper.GetBool("confirm") {
return c
}
return deleteConfig{
// TODO: Path should be prompted for and set prior to name attempting path derivation. Test/fix this if necessary.
Name: prompt.ForString("Function to remove", deriveName(c.Name, c.Path), prompt.WithRequired(true)),
}
}