@@ -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}
0 commit comments