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
/
get_function.go
84 lines (72 loc) · 2.49 KB
/
get_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
84
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"io"
"time"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/client"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
getFunctionLong = i18n.T(`Get function(s).`)
// TODO: add examples
getFunctionExample = i18n.T(``)
)
// NewCmdGetFunction creates command responsible for getting functions.
func NewCmdGetFunction(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "function [FUNCTION_NAME]",
Short: i18n.T("Get function(s)"),
Long: getFunctionLong,
Example: getFunctionExample,
Args: cobra.RangeArgs(0, 1),
Aliases: []string{"functions"},
Run: func(cmd *cobra.Command, args []string) {
c := functionManagerClient()
var err error
if len(args) > 0 {
err = getFunction(out, errOut, cmd, args, c)
} else {
err = getFunctions(out, errOut, cmd, c)
}
CheckErr(err)
},
}
cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "filter by application")
return cmd
}
func getFunction(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.FunctionsClient) error {
functionName := args[0]
resp, err := c.GetFunction(context.TODO(), dispatchConfig.Organization, functionName)
if err != nil {
return err
}
return formatFunctionOutput(out, false, []v1.Function{*resp})
}
func getFunctions(out, errOut io.Writer, cmd *cobra.Command, c client.FunctionsClient) error {
resp, err := c.ListFunctions(context.TODO(), dispatchConfig.Organization)
if err != nil {
return err
}
return formatFunctionOutput(out, true, resp)
}
func formatFunctionOutput(out io.Writer, list bool, functions []v1.Function) error {
if w, err := formatOutput(out, list, functions); w {
return err
}
table := tablewriter.NewWriter(out)
table.SetHeader([]string{"Name", "FunctionImage", "Status", "Created Date"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetCenterSeparator("")
for _, function := range functions {
table.Append([]string{*function.Name, function.FunctionImageURL, string(function.Status), time.Unix(function.CreatedTime, 0).Local().Format(time.UnixDate)})
}
table.Render()
return nil
}