@@ -24,6 +24,7 @@ import (
2424 "github.com/microcks/microcks-cli/pkg/config"
2525 "github.com/microcks/microcks-cli/pkg/connectors"
2626 "github.com/microcks/microcks-cli/pkg/errors"
27+ "github.com/microcks/microcks-cli/pkg/watcher"
2728 "github.com/spf13/cobra"
2829)
2930
@@ -34,6 +35,7 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
3435 Use : "import" ,
3536 Short : "import API artifacts on Microcks server" ,
3637 Long : `import API artifacts on Microcks server` ,
38+ Args : cobra .MaximumNArgs (1 ),
3739 Run : func (cmd * cobra.Command , args []string ) {
3840 // Parse subcommand args first.
3941 if len (args ) == 0 {
@@ -44,14 +46,23 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
4446
4547 specificationFiles := args [0 ]
4648
49+ // Initialize config from command options.
4750 config .InsecureTLS = globalClientOpts .InsecureTLS
4851 config .CaCertPaths = globalClientOpts .CaCertPaths
4952 config .Verbose = globalClientOpts .Verbose
5053
54+ // Read local config file in case we need some context info.
55+ localConfig , err := config .ReadLocalConfig (globalClientOpts .ConfigPath )
56+ if err != nil {
57+ fmt .Println (err )
58+ return
59+ }
60+
61+ // Prepare Microcks client.
5162 var mc connectors.MicrocksClient
5263
5364 if globalClientOpts .ServerAddr != "" && globalClientOpts .ClientId != "" && globalClientOpts .ClientSecret != "" {
54- // create client with server address
65+ // Create client with server address.
5566 mc = connectors .NewMicrocksClient (globalClientOpts .ServerAddr )
5667
5768 keycloakURL , err := mc .GetKeycloakURL ()
@@ -60,7 +71,7 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
6071 os .Exit (1 )
6172 }
6273
63- var oauthToken string = "unauthentifed -token"
74+ var oauthToken string = "unauthenticated -token"
6475 if keycloakURL != "null" {
6576 // If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
6677 kc := connectors .NewKeycloakClient (keycloakURL , globalClientOpts .ClientId , globalClientOpts .ClientSecret )
@@ -73,18 +84,23 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
7384 //fmt.Printf("Retrieve OAuthToken: %s", oauthToken)
7485 }
7586
76- //Set Auth token
87+ // Set Auth token.
7788 mc .SetOAuthToken (oauthToken )
78- } else {
7989
80- localConfig , err := config .ReadLocalConfig (globalClientOpts .ConfigPath )
81- if err != nil {
82- fmt .Println (err )
83- return
90+ // If no context provided use current one from config file or client server address.
91+ // So that watch config can be updated properly, referencing the right context.
92+ if globalClientOpts .Context == "" {
93+ if (localConfig != nil ) && (localConfig .CurrentContext != "" ) {
94+ globalClientOpts .Context = localConfig .CurrentContext
95+ } else {
96+ globalClientOpts .Context = globalClientOpts .ServerAddr
97+ }
8498 }
8599
100+ } else {
101+ // Create client from config file and using the current or provided context.
86102 if localConfig == nil {
87- fmt .Println ("Please login to perform opertion ..." )
103+ fmt .Println ("Please login to perform operation ..." )
88104 return
89105 }
90106
@@ -98,10 +114,13 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
98114 return
99115 }
100116 }
117+
118+ // Handle multiple specification files separated by comma.
101119 sepSpecificationFiles := strings .Split (specificationFiles , "," )
102120 for _ , f := range sepSpecificationFiles {
103121 mainArtifact := true
104122 var err error
123+
105124 // Check if mainArtifact flag is provided.
106125 if strings .Contains (f , ":" ) {
107126 pathAndMainArtifact := strings .Split (f , ":" )
@@ -118,31 +137,55 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
118137 fmt .Printf ("Got error when invoking Microcks client importing Artifact: %s" , err )
119138 os .Exit (1 )
120139 }
121- fmt .Printf ("Microcks has discovered '%s'\n " , msg )
140+ action := "discovered"
141+ if ! mainArtifact {
142+ action = "completed"
143+ }
144+ fmt .Printf ("Microcks has %s '%s'\n " , action , msg )
122145
146+ // If watch flag is provided, update watch config.
123147 if watch {
124148 watchFile , err := config .DefaultLocalWatchPath ()
125149 errors .CheckError (err )
150+
126151 watchCfg , err := config .ReadLocalWatchConfig (watchFile )
127152 errors .CheckError (err )
128153 if watchCfg == nil {
129154 watchCfg = & config.WatchConfig {}
130155 }
131156
157+ // Normalize file path to match the watcher fsnotify events format.
158+ if strings .HasPrefix (f , "./" ) {
159+ f = strings .TrimPrefix (f , "./" )
160+ }
161+
162+ // Upsert entry.
132163 watchCfg .UpsertEntry (config.WatchEntry {
133164 FilePath : f ,
134165 Context : []string {globalClientOpts .Context },
135166 MainArtifact : mainArtifact ,
136167 })
137- //write watch file
168+
169+ // Write watch file.
138170 err = config .WriteLocalWatchConfig (* watchCfg , watchFile )
139171 errors .CheckError (err )
140172 }
141173 }
142174
175+ // Start watcher if --watch flag is provided.
176+ if watch {
177+ watchFile , err := config .DefaultLocalWatchPath ()
178+ errors .CheckError (err )
179+
180+ wm , err := watcher .NewWatchManger (watchFile )
181+ errors .CheckError (err )
182+
183+ fmt .Println ("Watch mode enabled - microcks-watcher started..." )
184+ wm .Run ()
185+ }
143186 },
144187 }
145188
146- importCmd .Flags ().BoolVar (& watch , "watch" , false , "Keep watch on file changes and re-import it on change " )
189+ importCmd .Flags ().BoolVar (& watch , "watch" , false , "Keep watch on file changes and re-import it on change" )
147190 return importCmd
148191}
0 commit comments