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_application.go
91 lines (80 loc) · 2.66 KB
/
get_application.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
///////////////////////////////////////////////////////////////////////
// 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/application-manager/gen/client/application"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
getApplicationsLong = i18n.T(`Get applications.`)
// TODO: add examples
getApplicationsExample = i18n.T(``)
)
// NewCmdGetApplication creates command responsible for getting applications.
func NewCmdGetApplication(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "application [APPLICATION]",
Short: i18n.T("Get applications"),
Long: getApplicationsLong,
Example: getApplicationsExample,
Args: cobra.MaximumNArgs(1),
Aliases: []string{"app", "apps", "applications"},
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
err = getApplication(out, errOut, cmd, args)
} else {
err = getApplications(out, errOut, cmd)
}
CheckErr(err)
},
}
return cmd
}
func getApplication(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
client := applicationManagerClient()
params := &application.GetAppParams{
Context: context.Background(),
Application: args[0],
XDispatchOrg: getOrgFromConfig(),
}
resp, err := client.Application.GetApp(params, GetAuthInfoWriter())
if err != nil {
return err
}
return formatApplicationOutput(out, false, []*v1.Application{resp.Payload})
}
func getApplications(out, errOut io.Writer, cmd *cobra.Command) error {
client := applicationManagerClient()
params := &application.GetAppsParams{
Context: context.Background(),
XDispatchOrg: getOrgFromConfig(),
}
resp, err := client.Application.GetApps(params, GetAuthInfoWriter())
if err != nil {
return err
}
return formatApplicationOutput(out, true, resp.Payload)
}
func formatApplicationOutput(out io.Writer, list bool, applications []*v1.Application) error {
if w, err := formatOutput(out, list, applications); w {
return err
}
table := tablewriter.NewWriter(out)
table.SetHeader([]string{"Name", "Status", "Created Date"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetCenterSeparator("")
for _, app := range applications {
table.Append([]string{*app.Name, string(app.Status), time.Unix(app.CreatedTime, 0).Local().Format(time.UnixDate)})
}
table.Render()
return nil
}