Skip to content

Commit 9f2c49a

Browse files
authored
Move profile flag to base command (opendistro-for-elasticsearch#19)
* Move profile flag to base command Since, profile feature will be common to all plugins, move profile as persistent flag to odfe-cli. This will help other plugins to use value instead of declaring it exclusively.
1 parent 3c138ea commit 9f2c49a

9 files changed

+82
-26
lines changed

commands/ad.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package commands
1717

1818
import (
19-
"fmt"
2019
"odfe-cli/client"
2120
adctrl "odfe-cli/controller/ad"
2221
esctrl "odfe-cli/controller/es"
@@ -29,8 +28,7 @@ import (
2928
)
3029

3130
const (
32-
adCommandName = "ad"
33-
flagProfileName = "profile"
31+
adCommandName = "ad"
3432
)
3533

3634
//adCommand is base command for Anomaly Detection plugin.
@@ -41,7 +39,6 @@ var adCommand = &cobra.Command{
4139
}
4240

4341
func init() {
44-
adCommand.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
4542
adCommand.Flags().BoolP("help", "h", false, "Help for Anomaly Detection")
4643
GetRoot().AddCommand(adCommand)
4744
}
@@ -58,23 +55,12 @@ func GetADHandler() (*handler.Handler, error) {
5855
if err != nil {
5956
return nil, err
6057
}
61-
p, err := GetProfileController()
58+
profile, err := GetProfile()
6259
if err != nil {
6360
return nil, err
6461
}
65-
profileFlagValue, err := GetADCommand().Flags().GetString(flagProfileName)
66-
if err != nil {
67-
return nil, err
68-
}
69-
profile, ok, err := p.GetProfileForExecution(profileFlagValue)
70-
if err != nil {
71-
return nil, err
72-
}
73-
if !ok {
74-
return nil, fmt.Errorf("No profile found for execution. Try %s %s --help for more information.", RootCommandName, ProfileCommandName)
75-
}
76-
g := adgateway.New(c, &profile)
77-
esg := esgateway.New(c, &profile)
62+
g := adgateway.New(c, profile)
63+
esg := esgateway.New(c, profile)
7864
esc := esctrl.New(esg)
7965
ctr := adctrl.New(os.Stdin, esc, g)
8066
return handler.New(ctr), nil

commands/ad_create.go

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func generateTemplate() {
5858

5959
func init() {
6060
GetADCommand().AddCommand(createCmd)
61-
createCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
6261
createCmd.Flags().BoolP(generate, "g", false, "Output sample detector configuration")
6362
createCmd.Flags().BoolP("help", "h", false, "Help for "+createDetectorsCommandName)
6463

commands/ad_delete.go

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func init() {
5656
GetADCommand().AddCommand(deleteDetectorsCmd)
5757
deleteDetectorsCmd.Flags().BoolP(detectorForceDeletionFlagName, "f", false, "Delete the detector even if it is running")
5858
deleteDetectorsCmd.Flags().BoolP(deleteDetectorIDFlagName, "", false, "Input is detector ID")
59-
deleteDetectorsCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
6059
deleteDetectorsCmd.Flags().BoolP("help", "h", false, "Help for "+deleteDetectorsCommandName)
6160
}
6261

commands/ad_get.go

-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,5 @@ func Println(cmd *cobra.Command, d *entity.DetectorOutput) error {
129129
func init() {
130130
GetADCommand().AddCommand(getDetectorsCmd)
131131
getDetectorsCmd.Flags().BoolP(getDetectorIDFlagName, "", false, "Input is detector ID")
132-
getDetectorsCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
133132
getDetectorsCmd.Flags().BoolP("help", "h", false, "Help for "+getDetectorsCommandName)
134133
}

commands/ad_start_stop.go

-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ var stopDetectorsCmd = &cobra.Command{
7777

7878
func init() {
7979
startDetectorsCmd.Flags().BoolP(idFlagName, "", false, "Input is detector ID")
80-
startDetectorsCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
8180
startDetectorsCmd.Flags().BoolP("help", "h", false, "Help for "+startDetectorsCommandName)
8281
GetADCommand().AddCommand(startDetectorsCmd)
8382
stopDetectorsCmd.Flags().BoolP(idFlagName, "", false, "Input is detector ID")
84-
stopDetectorsCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
8583
stopDetectorsCmd.Flags().BoolP("help", "h", false, "Help for "+stopDetectorsCommandName)
8684
GetADCommand().AddCommand(stopDetectorsCmd)
8785
}

commands/ad_update.go

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func init() {
5656
GetADCommand().AddCommand(updateDetectorsCmd)
5757
updateDetectorsCmd.Flags().BoolP(forceFlagName, "f", false, "Stop detector and update forcefully")
5858
updateDetectorsCmd.Flags().BoolP(startFlagName, "s", false, "Start detector if update is successful")
59-
updateDetectorsCmd.Flags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
6059
updateDetectorsCmd.Flags().BoolP("help", "h", false, "Help for "+updateDetectorsCommandName)
6160
}
6261

commands/root.go

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package commands
1717

1818
import (
1919
"fmt"
20+
"odfe-cli/entity"
2021
"os"
2122
"path/filepath"
2223

@@ -27,6 +28,7 @@ const (
2728
configFileType = "yaml"
2829
defaultConfigFileName = "config"
2930
flagConfig = "config"
31+
flagProfileName = "profile"
3032
folderPermission = 0755 // only owner can write, while everyone can read and execute
3133
odfeConfigEnvVarName = "ODFE_CLI_CONFIG"
3234
RootCommandName = "odfe-cli"
@@ -71,6 +73,7 @@ func init() {
7173
cobra.OnInitialize()
7274
configFilePath := GetDefaultConfigFilePath()
7375
rootCommand.PersistentFlags().StringP(flagConfig, "c", "", fmt.Sprintf("Configuration file for odfe-cli, default is %s", configFilePath))
76+
rootCommand.PersistentFlags().StringP(flagProfileName, "p", "", "Use a specific profile from your configuration file")
7477
rootCommand.Flags().BoolP("version", "v", false, "Version for odfe-cli")
7578
rootCommand.Flags().BoolP("help", "h", false, "Help for odfe-cli")
7679
}
@@ -127,3 +130,23 @@ func DisplayError(err error, cmdName string) {
127130
fmt.Println("Reason:", err)
128131
}
129132
}
133+
134+
// GetProfile gets profile details for current execution
135+
func GetProfile() (*entity.Profile, error) {
136+
p, err := GetProfileController()
137+
if err != nil {
138+
return nil, err
139+
}
140+
profileFlagValue, err := rootCommand.PersistentFlags().GetString(flagProfileName)
141+
if err != nil {
142+
return nil, err
143+
}
144+
profile, ok, err := p.GetProfileForExecution(profileFlagValue)
145+
if err != nil {
146+
return nil, err
147+
}
148+
if !ok {
149+
return nil, fmt.Errorf("No profile found for execution. Try %s %s --help for more information.", RootCommandName, ProfileCommandName)
150+
}
151+
return &profile, nil
152+
}

commands/root_test.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package commands
1717

1818
import (
19+
"odfe-cli/entity"
1920
"os"
2021
"testing"
2122

@@ -45,8 +46,51 @@ func TestGetRoot(t *testing.T) {
4546
root.SetArgs([]string{"--config", "test/config.yml"})
4647
cmd, err := root.ExecuteC()
4748
assert.NoError(t, err)
48-
expected, err := cmd.Flags().GetString(flagConfig)
49+
actual, err := cmd.Flags().GetString(flagConfig)
4950
assert.NoError(t, err)
50-
assert.EqualValues(t, expected, "test/config.yml")
51+
assert.EqualValues(t, "test/config.yml", actual)
52+
})
53+
}
54+
55+
func TestGetProfile(t *testing.T) {
56+
t.Run("get default profile", func(t *testing.T) {
57+
root := GetRoot()
58+
assert.NotNil(t, root)
59+
root.SetArgs([]string{"--config", "testdata/config.yaml"})
60+
_, err := root.ExecuteC()
61+
assert.NoError(t, err)
62+
actual, err := GetProfile()
63+
assert.NoError(t, err)
64+
expectedProfile := entity.Profile{Name: "default", Endpoint: "http://localhost:9200", UserName: "default", Password: "admin"}
65+
assert.EqualValues(t, expectedProfile, *actual)
66+
})
67+
t.Run("test get profile", func(t *testing.T) {
68+
root := GetRoot()
69+
assert.NotNil(t, root)
70+
root.SetArgs([]string{"--config", "testdata/config.yaml", "--profile", "test"})
71+
_, err := root.ExecuteC()
72+
assert.NoError(t, err)
73+
actual, err := GetProfile()
74+
assert.NoError(t, err)
75+
expectedProfile := entity.Profile{Name: "test", Endpoint: "https://localhost:9200", UserName: "admin", Password: "admin"}
76+
assert.EqualValues(t, expectedProfile, *actual)
77+
})
78+
t.Run("Profile mismatch", func(t *testing.T) {
79+
root := GetRoot()
80+
assert.NotNil(t, root)
81+
root.SetArgs([]string{"--config", "testdata/config.yaml", "--profile", "test1"})
82+
_, err := root.ExecuteC()
83+
assert.NoError(t, err)
84+
_, err = GetProfile()
85+
assert.EqualError(t, err, "No profile found for execution. Try odfe-cli profile --help for more information.")
86+
})
87+
t.Run("no config file found", func(t *testing.T) {
88+
root := GetRoot()
89+
assert.NotNil(t, root)
90+
root.SetArgs([]string{"--config", "testdata/config1.yaml", "--profile", "test1"})
91+
_, err := root.ExecuteC()
92+
assert.NoError(t, err)
93+
_, err = GetProfile()
94+
assert.EqualError(t, err, "open testdata/config1.yaml: no such file or directory")
5195
})
5296
}

commands/testdata/config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
profiles:
2+
- name: test
3+
endpoint: https://localhost:9200
4+
user: admin
5+
password: admin
6+
- name: default
7+
endpoint: http://localhost:9200
8+
user: default
9+
password: admin

0 commit comments

Comments
 (0)