Skip to content

Commit df054bc

Browse files
committed
feat: microcks#160 Adds support of ClienOpts in all remaining commads
Signed-off-by: Harsh4902 <harshparmar4902@gmail.com>
1 parent c032959 commit df054bc

6 files changed

Lines changed: 63 additions & 117 deletions

File tree

cmd/cmd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func NewCommad() *cobra.Command {
4040

4141
command.AddCommand(NewImportCommand(&clientOpts))
4242
command.AddCommand(NewVersionCommand())
43-
command.AddCommand(NewTestCommand())
44-
command.AddCommand(NewImportURLCommand())
45-
command.AddCommand(NewStartCommand())
46-
command.AddCommand(NewStopCommand())
47-
command.AddCommand(NewContextCommand())
43+
command.AddCommand(NewTestCommand(&clientOpts))
44+
command.AddCommand(NewImportURLCommand(&clientOpts))
45+
command.AddCommand(NewStartCommand(&clientOpts))
46+
command.AddCommand(NewStopCommand(&clientOpts))
47+
command.AddCommand(NewContextCommand(&clientOpts))
4848
command.AddCommand(NewLoginCommand(&clientOpts))
4949
command.AddCommand(NewLogoutCommand(&clientOpts))
5050

cmd/context.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"text/tabwriter"
99

1010
"github.com/microcks/microcks-cli/pkg/config"
11+
"github.com/microcks/microcks-cli/pkg/connectors"
1112
"github.com/microcks/microcks-cli/pkg/errors"
1213
"github.com/spf13/cobra"
1314
)
1415

15-
func NewContextCommand() *cobra.Command {
16+
func NewContextCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
1617
var delete bool
1718
ctxCmd := &cobra.Command{
1819
Use: "context [CONTEXT]",
@@ -27,24 +28,21 @@ microcks context httP://localhost:8080
2728
# Delete Microcks context
2829
microcks context httP://localhost:8080 --delete`,
2930
Run: func(cmd *cobra.Command, args []string) {
30-
var cfgFile string
31-
configPath, err := config.DefaultLocalConfigPath()
32-
errors.CheckError(err)
33-
cfgFile = configPath
34-
localCfg, err := config.ReadLocalConfig(cfgFile)
31+
configPath := globalClientOpts.ConfigPath
32+
localCfg, err := config.ReadLocalConfig(configPath)
3533
errors.CheckError(err)
3634
if delete {
3735
if len(args) == 0 {
3836
cmd.HelpFunc()(cmd, args)
3937
os.Exit(1)
4038
}
41-
err := deleteContext(args[0], cfgFile)
39+
err := deleteContext(args[0], configPath)
4240
errors.CheckError(err)
4341
return
4442
}
4543

4644
if len(args) == 0 {
47-
printMicrocksContexts(cfgFile)
45+
printMicrocksContexts(configPath)
4846
return
4947
}
5048

cmd/importURL.go

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ import (
2727
"github.com/spf13/cobra"
2828
)
2929

30-
func NewImportURLCommand() *cobra.Command {
31-
var (
32-
microcksURL string
33-
keycloakClientID string
34-
keycloakClientSecret string
35-
insecureTLS bool
36-
caCertPaths string
37-
verbose bool
38-
)
30+
func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
3931
var importURLCmd = &cobra.Command{
4032
Use: "import-url",
4133
Short: "import API artifacts from URL on Microcks server",
@@ -49,40 +41,30 @@ func NewImportURLCommand() *cobra.Command {
4941

5042
specificationFiles := args[0]
5143

52-
// Collect optional HTTPS transport flags.
53-
if insecureTLS {
54-
config.InsecureTLS = true
55-
}
56-
if len(caCertPaths) > 0 {
57-
config.CaCertPaths = caCertPaths
58-
}
59-
if verbose {
60-
config.Verbose = true
61-
}
44+
config.InsecureTLS = globalClientOpts.InsecureTLS
45+
config.CaCertPaths = globalClientOpts.CaCertPaths
46+
config.Verbose = globalClientOpts.Verbose
6247

63-
// Now we seems to be good ...
64-
// First - retrieve the Keycloak URL from Microcks configuration.
65-
mc := connectors.NewMicrocksClient(microcksURL)
66-
keycloakURL, err := mc.GetKeycloakURL()
48+
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
6749
if err != nil {
68-
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
69-
os.Exit(1)
50+
fmt.Println(err)
51+
return
7052
}
7153

72-
var oauthToken string = "unauthentifed-token"
73-
if keycloakURL != "null" {
74-
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
75-
kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret)
54+
if localConfig == nil {
55+
fmt.Println("Please login to perform opertion...")
56+
return
57+
}
7658

77-
oauthToken, err = kc.ConnectAndGetToken()
78-
if err != nil {
79-
fmt.Printf("Got error when invoking Keycloack client: %s", err)
80-
os.Exit(1)
81-
}
59+
if globalClientOpts.Context == "" {
60+
globalClientOpts.Context = localConfig.CurrentContext
8261
}
8362

84-
// Then - for each specificationFile, upload the artifact on Microcks Server.
85-
mc.SetOAuthToken(oauthToken)
63+
mc, err := connectors.NewClient(*globalClientOpts)
64+
if err != nil {
65+
fmt.Printf("error %v", err)
66+
return
67+
}
8668

8769
sepSpecificationFiles := strings.Split(specificationFiles, ",")
8870
for _, f := range sepSpecificationFiles {
@@ -116,17 +98,6 @@ func NewImportURLCommand() *cobra.Command {
11698
}
11799
},
118100
}
119-
importURLCmd.Flags().StringVar(&microcksURL, "microcksURL", "", "Microcks API URL")
120-
importURLCmd.Flags().StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
121-
importURLCmd.Flags().StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
122-
importURLCmd.Flags().BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection")
123-
importURLCmd.Flags().StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
124-
importURLCmd.Flags().BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges")
125-
126-
//Marking flags 'required'
127-
importURLCmd.MarkFlagRequired("microcksURL")
128-
importURLCmd.MarkFlagRequired("keycloakClientId")
129-
importURLCmd.MarkFlagRequired("keycloakClientSecret")
130101

131102
return importURLCmd
132103
}

cmd/start.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func NewStartCommand() *cobra.Command {
13+
func NewStartCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
1414
var (
1515
name string
1616
hostPort string
@@ -34,8 +34,7 @@ microcks start --driver [driver you wnat either 'docker' or 'podman']
3434
microcks start --name [name of you container/instance]`,
3535
Run: func(cmd *cobra.Command, args []string) {
3636

37-
configFile, err := config.DefaultLocalConfigPath()
38-
errors.CheckError(err)
37+
configFile := globalClientOpts.ConfigPath
3938
localConfig, err := config.ReadLocalConfig(configFile)
4039
errors.CheckError(err)
4140

cmd/stop.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func NewStopCommand() *cobra.Command {
13+
func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
1414

1515
var stopCmd = &cobra.Command{
1616
Use: "stop",
1717
Short: "stop microcks instance",
1818
Long: "stop microcks instance",
1919
Run: func(cmd *cobra.Command, args []string) {
2020

21-
configFile, err := config.DefaultLocalConfigPath()
22-
errors.CheckError(err)
21+
configFile := globalClientOpts.ConfigPath
2322
localConfig, err := config.ReadLocalConfig(configFile)
2423
errors.CheckError(err)
2524

cmd/test.go

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,21 @@ import (
2424

2525
"github.com/microcks/microcks-cli/pkg/config"
2626
"github.com/microcks/microcks-cli/pkg/connectors"
27+
"github.com/microcks/microcks-cli/pkg/errors"
2728
"github.com/spf13/cobra"
2829
)
2930

3031
var (
3132
runnerChoices = map[string]bool{"HTTP": true, "SOAP_HTTP": true, "SOAP_UI": true, "POSTMAN": true, "OPEN_API_SCHEMA": true, "ASYNC_API_SCHEMA": true, "GRPC_PROTOBUF": true, "GRAPHQL_SCHEMA": true}
3233
)
3334

34-
func NewTestCommand() *cobra.Command {
35+
func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
3536
var (
36-
microcksURL string
37-
keycloakClientID string
38-
keycloakClientSecret string
39-
waitFor string
40-
secretName string
41-
filteredOperations string
42-
operationsHeaders string
43-
oAuth2Context string
44-
insecureTLS bool
45-
caCertPaths string
46-
verbose bool
37+
waitFor string
38+
secretName string
39+
filteredOperations string
40+
operationsHeaders string
41+
oAuth2Context string
4742
)
4843
var testCmd = &cobra.Command{
4944

@@ -85,15 +80,9 @@ func NewTestCommand() *cobra.Command {
8580
}
8681

8782
// Collect optional HTTPS transport flags.
88-
if insecureTLS {
89-
config.InsecureTLS = true
90-
}
91-
if len(caCertPaths) > 0 {
92-
config.CaCertPaths = caCertPaths
93-
}
94-
if verbose {
95-
config.Verbose = true
96-
}
83+
config.InsecureTLS = globalClientOpts.InsecureTLS
84+
config.CaCertPaths = globalClientOpts.CaCertPaths
85+
config.Verbose = globalClientOpts.Verbose
9786

9887
// Compute time to wait in milliseconds.
9988
var waitForMilliseconds int64 = 5000
@@ -107,30 +96,31 @@ func NewTestCommand() *cobra.Command {
10796
waitForMilliseconds = waitForMilliseconds * 60 * 1000
10897
}
10998

110-
// Now we seems to be good ...
111-
// First - retrieve the Keycloak URL from Microcks configuration.
112-
mc := connectors.NewMicrocksClient(microcksURL)
113-
keycloakURL, err := mc.GetKeycloakURL()
99+
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
114100
if err != nil {
115-
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
116-
os.Exit(1)
101+
fmt.Println(err)
102+
return
117103
}
118104

119-
var oauthToken string = "unauthentifed-token"
120-
if keycloakURL != "null" {
121-
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
122-
kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret)
105+
if localConfig == nil {
106+
fmt.Println("Please login to perform opertion...")
107+
return
108+
}
123109

124-
oauthToken, err = kc.ConnectAndGetToken()
125-
if err != nil {
126-
fmt.Printf("Got error when invoking Keycloack client: %s", err)
127-
os.Exit(1)
128-
}
129-
//fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
110+
if globalClientOpts.Context == "" {
111+
globalClientOpts.Context = localConfig.CurrentContext
112+
}
113+
114+
mc, err := connectors.NewClient(*globalClientOpts)
115+
if err != nil {
116+
fmt.Printf("error %v", err)
117+
return
130118
}
131119

132-
// Then - launch the test on Microcks Server.
133-
mc.SetOAuthToken(oauthToken)
120+
ctx, err := localConfig.ResolveContext(globalClientOpts.Context)
121+
errors.CheckError(err)
122+
123+
serverAddr := ctx.Server.Server
134124

135125
var testResultID string
136126
testResultID, err = mc.CreateTestResult(serviceRef, testEndpoint, runnerType, secretName, waitForMilliseconds, filteredOperations, operationsHeaders, oAuth2Context)
@@ -166,30 +156,19 @@ func NewTestCommand() *cobra.Command {
166156
time.Sleep(2 * time.Second)
167157
}
168158

169-
fmt.Printf("Full TestResult details are available here: %s/#/tests/%s \n", strings.Split(microcksURL, "/api")[0], testResultID)
159+
fmt.Printf("Full TestResult details are available here: %s/#/tests/%s \n", serverAddr, testResultID)
170160

171161
if !success {
172162
os.Exit(1)
173163
}
174164
},
175165
}
176166

177-
testCmd.Flags().StringVar(&microcksURL, "microcksURL", "", "Microcks API URL")
178-
testCmd.Flags().StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
179-
testCmd.Flags().StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
180167
testCmd.Flags().StringVar(&waitFor, "waitFor", "5sec", "Time to wait for test to finish")
181168
testCmd.Flags().StringVar(&secretName, "secretName", "", "Secret to use for connecting test endpoint")
182169
testCmd.Flags().StringVar(&filteredOperations, "filteredOperations", "", "List of operations to launch a test for")
183170
testCmd.Flags().StringVar(&operationsHeaders, "operationsHeaders", "", "Override of operations headers as JSON string")
184171
testCmd.Flags().StringVar(&oAuth2Context, "oAuth2Context", "", "Spec of an OAuth2 client context as JSON string")
185-
testCmd.Flags().BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection")
186-
testCmd.Flags().StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
187-
testCmd.Flags().BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges")
188-
189-
//Marking flags 'required'
190-
testCmd.MarkFlagRequired("microcksURL")
191-
testCmd.MarkFlagRequired("keycloakClientId")
192-
testCmd.MarkFlagRequired("keycloakClientSecret")
193172

194173
return testCmd
195174
}

0 commit comments

Comments
 (0)