/
describe_spark_application.go
128 lines (102 loc) · 2.82 KB
/
describe_spark_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
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
package wave
import (
"context"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spotinst/spotctl/internal/errors"
"github.com/spotinst/spotctl/internal/flags"
"github.com/spotinst/spotctl/internal/spot"
"github.com/spotinst/spotctl/internal/writer/writers/json"
)
type (
CmdDescribeSparkApplication struct {
cmd *cobra.Command
opts CmdDescribeSparkApplicationOptions
}
CmdDescribeSparkApplicationOptions struct {
*CmdDescribeOptions
ID string
}
)
func NewCmdDescribeSparkApplication(opts *CmdDescribeOptions) *cobra.Command {
return newCmdDescribeSparkApplication(opts).cmd
}
func newCmdDescribeSparkApplication(opts *CmdDescribeOptions) *CmdDescribeSparkApplication {
var cmd CmdDescribeSparkApplication
cmd.cmd = &cobra.Command{
Use: "sparkapplication",
Short: "Describe a Wave Spark application",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(*cobra.Command, []string) error {
return cmd.Run(context.Background())
},
}
cmd.opts.Init(cmd.cmd.PersistentFlags(), opts)
return &cmd
}
func (x *CmdDescribeSparkApplicationOptions) Init(fs *pflag.FlagSet, opts *CmdDescribeOptions) {
x.initDefaults(opts)
x.initFlags(fs)
}
func (x *CmdDescribeSparkApplicationOptions) initDefaults(opts *CmdDescribeOptions) {
x.CmdDescribeOptions = opts
}
func (x *CmdDescribeSparkApplicationOptions) initFlags(fs *pflag.FlagSet) {
fs.StringVar(&x.ID, flags.FlagWaveSparkApplicationEntityID, x.ID, "id of the spark application")
}
func (x *CmdDescribeSparkApplication) survey(ctx context.Context) error {
if x.opts.Noninteractive {
return nil
}
return nil
}
func (x *CmdDescribeSparkApplicationOptions) Validate() error {
if x.ID == "" {
return errors.Required(flags.FlagWaveSparkApplicationEntityID)
}
return x.CmdDescribeOptions.Validate()
}
func (x *CmdDescribeSparkApplication) Run(ctx context.Context) error {
steps := []func(context.Context) error{
x.survey,
x.log,
x.validate,
x.run,
}
for _, step := range steps {
if err := step(ctx); err != nil {
return err
}
}
return nil
}
func (x *CmdDescribeSparkApplication) log(ctx context.Context) error {
flags.Log(x.cmd)
return nil
}
func (x *CmdDescribeSparkApplication) validate(ctx context.Context) error {
return x.opts.Validate()
}
func (x *CmdDescribeSparkApplication) run(ctx context.Context) error {
spotClientOpts := []spot.ClientOption{
spot.WithCredentialsProfile(x.opts.Profile),
}
spotClient, err := x.opts.Clientset.NewSpotClient(spotClientOpts...)
if err != nil {
return err
}
waveClient, err := spotClient.Services().Wave()
if err != nil {
return err
}
sparkApplication, err := waveClient.GetSparkApplication(ctx, x.opts.ID)
if err != nil {
return err
}
w, err := x.opts.Clientset.NewWriter(json.WriterFormat)
if err != nil {
return err
}
return w.Write(sparkApplication.Obj)
}