/
show.go
174 lines (156 loc) · 4.42 KB
/
show.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cmd
import (
"fmt"
"os"
"strings"
"github.com/cloud-native-application/rudrx/api/types"
"github.com/cloud-native-application/rudrx/pkg/application"
cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "show <APPLICATION-NAME>",
Short: "get details of your app",
Long: "get details of your app, including its workload and trait",
Example: `vela app show <APPLICATION-NAME>`,
RunE: func(cmd *cobra.Command, args []string) error {
argsLength := len(args)
if argsLength == 0 {
ioStreams.Errorf("Hint: please specify the application name\n")
os.Exit(1)
}
appName := args[0]
env, err := GetEnv(cmd)
if err != nil {
ioStreams.Errorf("Error: failed to get Env: %s", err)
return err
}
return showApplication(cmd, env, appName)
},
Annotations: map[string]string{
types.TagCommandType: types.TypeApp,
},
}
cmd.SetOut(ioStreams.Out)
return cmd
}
type Unkown struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Spec interface{} `json:"spec"`
Status interface{} `json:"status"`
}
func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) error {
app, err := application.Load(env.Name, appName)
if err != nil {
return err
}
cmd.Printf("About:\n\n")
table := uitable.New()
table.AddRow(" Name:", appName)
table.AddRow(" Created at:", app.CreateTime.String())
table.AddRow(" Updated at:", app.UpdateTime.String())
cmd.Printf("%s\n\n", table.String())
cmd.Println()
cmd.Printf("Environment:\n\n")
cmd.Printf(" Namespace:\t%s\n", env.Namespace)
cmd.Println()
table = uitable.New()
cmd.Printf("Components:\n\n")
table.AddRow(" Name", "Type", "Traits")
for compName := range app.Components {
wtype, _ := app.GetWorkload(compName)
var outPutTraits []string
traits, _ := app.GetTraits(compName)
for k := range traits {
outPutTraits = append(outPutTraits, k)
}
table.AddRow(" "+compName, wtype, strings.Join(outPutTraits, ","))
}
cmd.Println(table.String())
cmd.Println()
return nil
}
func NewCompShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "show <COMPONENT-NAME>",
Short: "get component detail",
Long: "get component detail, including arguments of workload and trait",
Example: `vela comp show <COMPONENT-NAME>`,
RunE: func(cmd *cobra.Command, args []string) error {
argsLength := len(args)
if argsLength == 0 {
ioStreams.Errorf("Hint: please specify the application name\n")
os.Exit(1)
}
compName := args[0]
env, err := GetEnv(cmd)
if err != nil {
ioStreams.Errorf("Error: failed to get Env: %s", err)
return err
}
appName, err := cmd.Flags().GetString(App)
if err != nil {
return err
}
return showComponent(cmd, env, compName, appName)
},
Annotations: map[string]string{
types.TagCommandType: types.TypeApp,
},
}
cmd.SetOut(ioStreams.Out)
return cmd
}
func showComponent(cmd *cobra.Command, env *types.EnvMeta, compName, appName string) error {
var app *application.Application
var err error
if appName != "" {
app, err = application.Load(env.Name, appName)
} else {
app, err = application.MatchAppByComp(env.Name, compName)
}
if err != nil {
return err
}
for cname := range app.Components {
if cname != compName {
continue
}
cmd.Printf("About:\n\n")
wtype, data := app.GetWorkload(compName)
table := uitable.New()
table.AddRow(" Name:", compName)
table.AddRow(" WorkloadType:", wtype)
table.AddRow(" Application:", app.Name)
cmd.Printf("%s\n\n", table.String())
cmd.Printf("Environment:\n\n")
cmd.Printf(" Namespace:\t%s\n\n", env.Namespace)
cmd.Printf("Arguments:\n\n")
table = uitable.New()
for k, v := range data {
table.AddRow(fmt.Sprintf(" %s:", k), v)
}
cmd.Printf("%s\n\n", table.String())
traits, err := app.GetTraits(compName)
if err != nil {
cmd.PrintErr(err)
continue
}
cmd.Println()
cmd.Printf("Traits:\n\n")
for k, v := range traits {
cmd.Printf(" %s:\n", k)
table = uitable.New()
for kk, vv := range v {
table.AddRow(fmt.Sprintf(" %s:", kk), vv)
}
cmd.Printf("%s\n\n", table.String())
}
cmd.Println()
}
return nil
}