This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
delete_function.go
83 lines (71 loc) · 2.18 KB
/
delete_function.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"fmt"
"io"
"golang.org/x/net/context"
"github.com/spf13/cobra"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/client"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
deleteFunctionLong = i18n.T(`Delete functions.`)
// TODO: add examples
deleteFunctionExample = i18n.T(``)
)
// NewCmdDeleteFunction creates command responsible for deleting functions.
func NewCmdDeleteFunction(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "function FUNCTION_NAME",
Short: i18n.T("Delete function"),
Long: deleteFunctionLong,
Example: deleteFunctionExample,
Args: cobra.ExactArgs(1),
Aliases: []string{"functions"},
Run: func(cmd *cobra.Command, args []string) {
c := functionManagerClient()
err := deleteFunction(out, errOut, cmd, args, c)
CheckErr(err)
},
}
cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "filter by application")
return cmd
}
// CallDeleteFunction makes the API call to delete a function
func CallDeleteFunction(c client.FunctionsClient) ModelAction {
return func(i interface{}) error {
functionModel := i.(*v1.Function)
deleted, err := c.DeleteFunction(context.Background(), "", *functionModel.Name)
if err != nil {
return err
}
*functionModel = *deleted
return nil
}
}
func deleteFunction(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.FunctionsClient) error {
functionModel := v1.Function{
Name: &args[0],
}
err := CallDeleteFunction(c)(&functionModel)
if err != nil {
return err
}
return formatDeleteFunctionOutput(out, false, []*v1.Function{&functionModel})
}
func formatDeleteFunctionOutput(out io.Writer, list bool, functions []*v1.Function) error {
if w, err := formatOutput(out, list, functions); w {
return err
}
for _, f := range functions {
_, err := fmt.Fprintf(out, "Deleted function: %s\n", *f.Name)
if err != nil {
return err
}
}
return nil
}