Skip to content

Commit b0998c8

Browse files
committed
chore: refactor mapt context
previously init functions for context had bunch of parameters which were hard to set with default values, this commit includes a struct to hold all possible params, also the contxt initalization has been moved from cmds to actions improving the design of the solution, as cmd code should have no logic at all and act only as wrapper to invoke action Signed-off-by: Adrian Riobo <[email protected]>
1 parent a1e8f08 commit b0998c8

File tree

27 files changed

+463
-380
lines changed

27 files changed

+463
-380
lines changed

cmd/mapt/cmd/aws/hosts/fedora.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func GetFedoraCmd() *cobra.Command {
3333
return nil
3434
},
3535
}
36+
37+
flagSet := pflag.NewFlagSet(cmdFedora, pflag.ExitOnError)
38+
params.AddCommonFlags(flagSet)
39+
c.PersistentFlags().AddFlagSet(flagSet)
40+
3641
c.AddCommand(getFedoraCreate(), getFedoraDestroy())
3742
return c
3843
}
@@ -46,16 +51,6 @@ func getFedoraCreate() *cobra.Command {
4651
return err
4752
}
4853

49-
// Initialize context
50-
maptContext.Init(
51-
viper.GetString(params.ProjectName),
52-
viper.GetString(params.BackedURL),
53-
viper.GetString(params.ConnectionDetailsOutput),
54-
viper.GetStringMapString(params.Tags),
55-
viper.IsSet(params.Debug),
56-
viper.GetUint(params.DebugLevel),
57-
false)
58-
5954
// Initialize gh actions runner if needed
6055
if viper.IsSet(params.InstallGHActionsRunner) {
6156
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
@@ -66,21 +61,29 @@ func getFedoraCreate() *cobra.Command {
6661
logging.Fatal(err)
6762
}
6863
}
69-
instanceRequest := &instancetypes.AwsInstanceRequest{
70-
CPUs: viper.GetInt32(params.CPUs),
71-
MemoryGib: viper.GetInt32(params.Memory),
72-
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64", instancetypes.Arm64, instancetypes.Amd64),
73-
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
74-
}
7564

7665
// Run create
7766
if err := fedora.Create(
67+
&maptContext.ContextArgs{
68+
ProjectName: viper.GetString(params.ProjectName),
69+
BackedURL: viper.GetString(params.BackedURL),
70+
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
71+
Debug: viper.IsSet(params.Debug),
72+
DebugLevel: viper.GetUint(params.DebugLevel),
73+
Tags: viper.GetStringMapString(params.Tags),
74+
},
7875
&fedora.Request{
79-
Prefix: "main",
80-
Version: viper.GetString(fedoraVersion),
81-
Arch: viper.GetString(params.LinuxArch),
82-
VMType: viper.GetStringSlice(vmTypes),
83-
InstanceRequest: instanceRequest,
76+
Prefix: "main",
77+
Version: viper.GetString(fedoraVersion),
78+
Arch: viper.GetString(params.LinuxArch),
79+
VMType: viper.GetStringSlice(vmTypes),
80+
InstanceRequest: &instancetypes.AwsInstanceRequest{
81+
CPUs: viper.GetInt32(params.CPUs),
82+
MemoryGib: viper.GetInt32(params.Memory),
83+
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64",
84+
instancetypes.Arm64, instancetypes.Amd64),
85+
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
86+
},
8487
Spot: viper.IsSet(spot),
8588
Timeout: viper.GetString(params.Timeout),
8689
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
@@ -114,16 +117,13 @@ func getFedoraDestroy() *cobra.Command {
114117
return err
115118
}
116119

117-
maptContext.InitBase(
118-
viper.GetString(params.ProjectName),
119-
viper.GetString(params.BackedURL),
120-
viper.IsSet(params.Debug),
121-
viper.GetUint(params.DebugLevel),
122-
viper.IsSet(params.Serverless))
123-
124-
logging.Debug("Run fedora destroy")
125-
126-
if err := fedora.Destroy(); err != nil {
120+
if err := fedora.Destroy(&maptContext.ContextArgs{
121+
ProjectName: viper.GetString(params.ProjectName),
122+
BackedURL: viper.GetString(params.BackedURL),
123+
Debug: viper.IsSet(params.Debug),
124+
DebugLevel: viper.GetUint(params.DebugLevel),
125+
Serverless: viper.IsSet(params.Serverless),
126+
}); err != nil {
127127
logging.Error(err)
128128
}
129129
return nil

cmd/mapt/cmd/aws/hosts/mac.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ func getMacRequest() *cobra.Command {
4141
return err
4242
}
4343

44-
// Initialize context
45-
maptContext.Init(
46-
viper.GetString(params.ProjectName),
47-
viper.GetString(params.BackedURL),
48-
viper.GetString(params.ConnectionDetailsOutput),
49-
viper.GetStringMapString(params.Tags),
50-
viper.IsSet(params.Debug),
51-
viper.GetUint(params.DebugLevel),
52-
false)
53-
5444
// Initialize gh actions runner if needed
5545
if viper.IsSet(params.InstallGHActionsRunner) {
5646
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
@@ -64,7 +54,15 @@ func getMacRequest() *cobra.Command {
6454

6555
// Run create
6656
if err := mac.Request(
67-
&mac.MacRequest{
57+
&maptContext.ContextArgs{
58+
ProjectName: viper.GetString(params.ProjectName),
59+
BackedURL: viper.GetString(params.BackedURL),
60+
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
61+
Debug: viper.IsSet(params.Debug),
62+
DebugLevel: viper.GetUint(params.DebugLevel),
63+
Tags: viper.GetStringMapString(params.Tags),
64+
},
65+
&mac.MacRequestArgs{
6866
Prefix: "main",
6967
Architecture: viper.GetString(awsParams.MACArch),
7068
Version: viper.GetString(awsParams.MACOSVersion),
@@ -77,6 +75,7 @@ func getMacRequest() *cobra.Command {
7775
},
7876
}
7977
flagSet := pflag.NewFlagSet(awsParams.MACRequestCmd, pflag.ExitOnError)
78+
params.AddCommonFlags(flagSet)
8079
flagSet.StringP(params.ConnectionDetailsOutput, "", "", params.ConnectionDetailsOutputDesc)
8180
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
8281
flagSet.StringP(awsParams.MACArch, "", awsParams.MACArchDefault, awsParams.MACArchDesc)
@@ -100,17 +99,17 @@ func getMacRelease() *cobra.Command {
10099

101100
// Run create
102101
if err := mac.Release(
103-
"main",
104-
viper.GetString(awsParams.MACDHID),
105-
viper.IsSet(params.Debug),
106-
viper.GetUint(params.DebugLevel)); err != nil {
102+
&maptContext.ContextArgs{
103+
Debug: viper.IsSet(params.Debug),
104+
DebugLevel: viper.GetUint(params.DebugLevel),
105+
},
106+
viper.GetString(awsParams.MACDHID)); err != nil {
107107
logging.Error(err)
108108
}
109109
return nil
110110
},
111111
}
112112
flagSet := pflag.NewFlagSet(awsParams.MACReleaseCmd, pflag.ExitOnError)
113-
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
114113
flagSet.StringP(awsParams.MACDHID, "", "", awsParams.MACDHIDDesc)
115114
c.PersistentFlags().AddFlagSet(flagSet)
116115
err := c.MarkPersistentFlagRequired(awsParams.MACDHID)
@@ -130,10 +129,11 @@ func getMacDestroy() *cobra.Command {
130129
}
131130

132131
if err := mac.Destroy(
133-
"main",
134-
viper.GetString(awsParams.MACDHID),
135-
viper.IsSet(params.Debug),
136-
viper.GetUint(params.DebugLevel)); err != nil {
132+
&maptContext.ContextArgs{
133+
Debug: viper.IsSet(params.Debug),
134+
DebugLevel: viper.GetUint(params.DebugLevel),
135+
},
136+
viper.GetString(awsParams.MACDHID)); err != nil {
137137
logging.Error(err)
138138
}
139139
return nil

cmd/mapt/cmd/aws/hosts/rhel.go

+30-29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func GetRHELCmd() *cobra.Command {
2929
return nil
3030
},
3131
}
32+
33+
flagSet := pflag.NewFlagSet(cmdRHEL, pflag.ExitOnError)
34+
params.AddCommonFlags(flagSet)
35+
c.PersistentFlags().AddFlagSet(flagSet)
36+
3237
c.AddCommand(getRHELCreate(), getRHELDestroy())
3338
return c
3439
}
@@ -42,16 +47,6 @@ func getRHELCreate() *cobra.Command {
4247
return err
4348
}
4449

45-
// Initialize context
46-
maptContext.Init(
47-
viper.GetString(params.ProjectName),
48-
viper.GetString(params.BackedURL),
49-
viper.GetString(params.ConnectionDetailsOutput),
50-
viper.GetStringMapString(params.Tags),
51-
viper.IsSet(params.Debug),
52-
viper.GetUint(params.DebugLevel),
53-
false)
54-
5550
// Initialize gh actions runner if needed
5651
if viper.IsSet(params.InstallGHActionsRunner) {
5752
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
@@ -63,20 +58,27 @@ func getRHELCreate() *cobra.Command {
6358
}
6459
}
6560

66-
instanceRequest := &instancetypes.AwsInstanceRequest{
67-
CPUs: viper.GetInt32(params.CPUs),
68-
MemoryGib: viper.GetInt32(params.Memory),
69-
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64", instancetypes.Arm64, instancetypes.Amd64),
70-
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
71-
}
72-
7361
// Run create
7462
if err := rhel.Create(
63+
&maptContext.ContextArgs{
64+
ProjectName: viper.GetString(params.ProjectName),
65+
BackedURL: viper.GetString(params.BackedURL),
66+
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
67+
Debug: viper.IsSet(params.Debug),
68+
DebugLevel: viper.GetUint(params.DebugLevel),
69+
Tags: viper.GetStringMapString(params.Tags),
70+
},
7571
&rhel.Request{
76-
Prefix: "main",
77-
Version: viper.GetString(params.RhelVersion),
78-
Arch: viper.GetString(params.LinuxArch),
79-
InstanceRequest: instanceRequest,
72+
Prefix: "main",
73+
Version: viper.GetString(params.RhelVersion),
74+
Arch: viper.GetString(params.LinuxArch),
75+
InstanceRequest: &instancetypes.AwsInstanceRequest{
76+
CPUs: viper.GetInt32(params.CPUs),
77+
MemoryGib: viper.GetInt32(params.Memory),
78+
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64",
79+
instancetypes.Arm64, instancetypes.Amd64),
80+
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
81+
},
8082
VMType: viper.GetStringSlice(vmTypes),
8183
SubsUsername: viper.GetString(params.SubsUsername),
8284
SubsUserpass: viper.GetString(params.SubsUserpass),
@@ -118,14 +120,13 @@ func getRHELDestroy() *cobra.Command {
118120
return err
119121
}
120122

121-
maptContext.InitBase(
122-
viper.GetString(params.ProjectName),
123-
viper.GetString(params.BackedURL),
124-
viper.IsSet(params.Debug),
125-
viper.GetUint(params.DebugLevel),
126-
viper.IsSet(params.Serverless))
127-
128-
if err := rhel.Destroy(); err != nil {
123+
if err := rhel.Destroy(&maptContext.ContextArgs{
124+
ProjectName: viper.GetString(params.ProjectName),
125+
BackedURL: viper.GetString(params.BackedURL),
126+
Debug: viper.IsSet(params.Debug),
127+
DebugLevel: viper.GetUint(params.DebugLevel),
128+
Serverless: viper.IsSet(params.Serverless),
129+
}); err != nil {
129130
logging.Error(err)
130131
}
131132
return nil

cmd/mapt/cmd/aws/hosts/windows.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func GetWindowsCmd() *cobra.Command {
4242
return nil
4343
},
4444
}
45+
flagSet := pflag.NewFlagSet(cmdWindows, pflag.ExitOnError)
46+
params.AddCommonFlags(flagSet)
47+
c.PersistentFlags().AddFlagSet(flagSet)
4548
c.AddCommand(getWindowsCreate(), getWindowsDestroy())
4649
return c
4750
}
@@ -55,16 +58,6 @@ func getWindowsCreate() *cobra.Command {
5558
return err
5659
}
5760

58-
// Initialize context
59-
maptContext.Init(
60-
viper.GetString(params.ProjectName),
61-
viper.GetString(params.BackedURL),
62-
viper.GetString(params.ConnectionDetailsOutput),
63-
viper.GetStringMapString(params.Tags),
64-
viper.IsSet(params.Debug),
65-
viper.GetUint(params.DebugLevel),
66-
false)
67-
6861
// Initialize gh actions runner if needed
6962
if viper.IsSet(params.InstallGHActionsRunner) {
7063
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
@@ -78,6 +71,14 @@ func getWindowsCreate() *cobra.Command {
7871

7972
// Run create
8073
if err := windows.Create(
74+
&maptContext.ContextArgs{
75+
ProjectName: viper.GetString(params.ProjectName),
76+
BackedURL: viper.GetString(params.BackedURL),
77+
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
78+
Debug: viper.IsSet(params.Debug),
79+
DebugLevel: viper.GetUint(params.DebugLevel),
80+
Tags: viper.GetStringMapString(params.Tags),
81+
},
8182
&windows.Request{
8283
Prefix: "main",
8384
AMIName: viper.GetString(amiName),
@@ -120,14 +121,13 @@ func getWindowsDestroy() *cobra.Command {
120121
return err
121122
}
122123

123-
maptContext.InitBase(
124-
viper.GetString(params.ProjectName),
125-
viper.GetString(params.BackedURL),
126-
viper.IsSet(params.Debug),
127-
viper.GetUint(params.DebugLevel),
128-
viper.IsSet(params.Serverless))
129-
130-
if err := windows.Destroy(); err != nil {
124+
if err := windows.Destroy(&maptContext.ContextArgs{
125+
ProjectName: viper.GetString(params.ProjectName),
126+
BackedURL: viper.GetString(params.BackedURL),
127+
Debug: viper.IsSet(params.Debug),
128+
DebugLevel: viper.GetUint(params.DebugLevel),
129+
Serverless: viper.IsSet(params.Serverless),
130+
}); err != nil {
131131
logging.Error(err)
132132
}
133133
return nil

0 commit comments

Comments
 (0)