/
app.go
121 lines (93 loc) · 2.35 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"log"
"os"
"github.com/didil/kubexcloud/kxc-cli/client"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
func buildAppsCmd() *cobra.Command {
var appsCmd = &cobra.Command{
Use: "apps",
Short: "KubeXCloud Apps",
}
var projectName string
appsCmd.PersistentFlags().StringVarP(&projectName, "project", "p", "", "project")
appsListCmd := buildAppsListCmd()
appsCmd.AddCommand(appsListCmd)
appRestartCmd := buildAppRestartCmd()
appsCmd.AddCommand(appRestartCmd)
return appsCmd
}
func buildAppsListCmd() *cobra.Command {
var appsListCmd = &cobra.Command{
Use: "list",
Short: "KubeXCloud Apps List",
RunE: func(cmd *cobra.Command, args []string) error {
projectName, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
if projectName == "" {
return fmt.Errorf("project name required")
}
err = listAppsRun(projectName)
if err != nil {
log.Fatalf("run: %v", err)
}
return nil
},
}
return appsListCmd
}
func listAppsRun(projectName string) error {
cl := client.NewClient()
fmt.Printf("Fetching Apps for project %v ...\n", projectName)
appsList, err := cl.ListApps(projectName)
if err != nil {
return fmt.Errorf("list apps: %v", err)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name"})
for _, proj := range appsList.Apps {
table.Append([]string{proj.Name})
}
table.Render()
return nil
}
func buildAppRestartCmd() *cobra.Command {
var appsListCmd = &cobra.Command{
Use: "restart <app>",
Short: "KubeXCloud Apps List",
RunE: func(cmd *cobra.Command, args []string) error {
projectName, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
if projectName == "" {
return fmt.Errorf("project name required")
}
if len(args) == 0 {
return fmt.Errorf("app name required")
}
appName := args[0]
err = restartAppRun(projectName, appName)
if err != nil {
log.Fatalf("run: %v", err)
}
return nil
},
}
return appsListCmd
}
func restartAppRun(projectName, appName string) error {
cl := client.NewClient()
fmt.Printf("Restarting App %s [Project %v] ...\n", appName, projectName)
err := cl.RestartApp(projectName, appName)
if err != nil {
return fmt.Errorf("restart app: %v", err)
}
fmt.Printf("App restart triggered successfully")
return nil
}