Skip to content

Commit cfb301c

Browse files
authored
Feat: Adds implementation of ClienOpts in all remaining commads (#177)
* feat: #160 Adds support of ClienOpts in all remaining commads Signed-off-by: Harsh4902 <[email protected]> * Adds microcksURL, keycloakClientId and keycloakClientSecret as global flags to perform operations like import, test without doing login Signed-off-by: Harsh4902 <[email protected]> --------- Signed-off-by: Harsh4902 <[email protected]>
1 parent c032959 commit cfb301c

File tree

8 files changed

+187
-143
lines changed

8 files changed

+187
-143
lines changed

cmd/cmd.go

Lines changed: 10 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

@@ -55,5 +55,10 @@ func NewCommad() *cobra.Command {
5555
command.PersistentFlags().BoolVar(&clientOpts.Verbose, "verbose", false, "Produce dumps of HTTP exchanges")
5656
command.PersistentFlags().BoolVar(&clientOpts.InsecureTLS, "insecure-tls", false, "Whether to accept insecure HTTPS connection")
5757
command.PersistentFlags().StringVar(&clientOpts.CaCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
58+
command.PersistentFlags().StringVar(&clientOpts.ClientId, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
59+
command.PersistentFlags().StringVar(&clientOpts.ClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
60+
command.PersistentFlags().StringVar(&clientOpts.ServerAddr, "microcksURL", "", "Microcks API URL")
61+
command.MarkFlagsRequiredTogether("keycloakClientId", "microcksURL", "keycloakClientSecret")
62+
5863
return command
5964
}

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/import.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,60 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
4848
config.CaCertPaths = globalClientOpts.CaCertPaths
4949
config.Verbose = globalClientOpts.Verbose
5050

51-
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
52-
if err != nil {
53-
fmt.Println(err)
54-
return
55-
}
51+
var mc connectors.MicrocksClient
5652

57-
if localConfig == nil {
58-
fmt.Println("Please login to perform opertion...")
59-
return
60-
}
53+
if globalClientOpts.ServerAddr != "" && globalClientOpts.ClientId != "" && globalClientOpts.ClientSecret != "" {
54+
// create client with server address
55+
mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr)
6156

62-
if globalClientOpts.Context == "" {
63-
globalClientOpts.Context = localConfig.CurrentContext
64-
}
57+
keycloakURL, err := mc.GetKeycloakURL()
58+
if err != nil {
59+
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
60+
os.Exit(1)
61+
}
6562

66-
mc, err := connectors.NewClient(*globalClientOpts)
67-
if err != nil {
68-
fmt.Printf("error %v", err)
69-
return
70-
}
63+
var oauthToken string = "unauthentifed-token"
64+
if keycloakURL != "null" {
65+
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
66+
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)
67+
68+
oauthToken, err = kc.ConnectAndGetToken()
69+
if err != nil {
70+
fmt.Printf("Got error when invoking Keycloack client: %s", err)
71+
os.Exit(1)
72+
}
73+
//fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
74+
}
75+
76+
//Set Auth token
77+
mc.SetOAuthToken(oauthToken)
78+
} else {
7179

80+
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
81+
if err != nil {
82+
fmt.Println(err)
83+
return
84+
}
85+
86+
if localConfig == nil {
87+
fmt.Println("Please login to perform opertion...")
88+
return
89+
}
90+
91+
if globalClientOpts.Context == "" {
92+
globalClientOpts.Context = localConfig.CurrentContext
93+
}
94+
95+
mc, err = connectors.NewClient(*globalClientOpts)
96+
if err != nil {
97+
fmt.Printf("error %v", err)
98+
return
99+
}
100+
}
72101
sepSpecificationFiles := strings.Split(specificationFiles, ",")
73102
for _, f := range sepSpecificationFiles {
74103
mainArtifact := true
75-
104+
var err error
76105
// Check if mainArtifact flag is provided.
77106
if strings.Contains(f, ":") {
78107
pathAndMainArtifact := strings.Split(f, ":")

cmd/importURL.go

Lines changed: 47 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,41 +41,60 @@ 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()
67-
if err != nil {
68-
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
69-
os.Exit(1)
70-
}
48+
var mc connectors.MicrocksClient
7149

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)
50+
if globalClientOpts.ServerAddr != "" && globalClientOpts.ClientId != "" && globalClientOpts.ClientSecret != "" {
51+
// create client with server address
52+
mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr)
7653

77-
oauthToken, err = kc.ConnectAndGetToken()
54+
keycloakURL, err := mc.GetKeycloakURL()
7855
if err != nil {
79-
fmt.Printf("Got error when invoking Keycloack client: %s", err)
56+
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
8057
os.Exit(1)
8158
}
82-
}
8359

84-
// Then - for each specificationFile, upload the artifact on Microcks Server.
85-
mc.SetOAuthToken(oauthToken)
60+
var oauthToken string = "unauthentifed-token"
61+
if keycloakURL != "null" {
62+
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
63+
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)
64+
65+
oauthToken, err = kc.ConnectAndGetToken()
66+
if err != nil {
67+
fmt.Printf("Got error when invoking Keycloack client: %s", err)
68+
os.Exit(1)
69+
}
70+
//fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
71+
}
72+
73+
//Set Auth token
74+
mc.SetOAuthToken(oauthToken)
75+
} else {
8676

77+
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
78+
if err != nil {
79+
fmt.Println(err)
80+
return
81+
}
82+
83+
if localConfig == nil {
84+
fmt.Println("Please login to perform opertion...")
85+
return
86+
}
87+
88+
if globalClientOpts.Context == "" {
89+
globalClientOpts.Context = localConfig.CurrentContext
90+
}
91+
92+
mc, err = connectors.NewClient(*globalClientOpts)
93+
if err != nil {
94+
fmt.Printf("error %v", err)
95+
return
96+
}
97+
}
8798
sepSpecificationFiles := strings.Split(specificationFiles, ",")
8899
for _, f := range sepSpecificationFiles {
89100
mainArtifact := true
@@ -116,17 +127,6 @@ func NewImportURLCommand() *cobra.Command {
116127
}
117128
},
118129
}
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")
130130

131131
return importURLCmd
132132
}

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

0 commit comments

Comments
 (0)