1- package cmd
1+ package config
22
33import (
44 "archive/tar"
@@ -15,7 +15,7 @@ import (
1515 "time"
1616
1717 "github.com/go-logr/logr"
18- "github.com/konveyor/tackle2-hub/api "
18+ "github.com/konveyor-ecosystem/kantra/pkg/profile "
1919 "github.com/spf13/cobra"
2020 "gopkg.in/yaml.v2"
2121)
@@ -59,7 +59,7 @@ func NewConfigCmd(log logr.Logger) *cobra.Command {
5959 return nil
6060 },
6161 RunE : func (cmd * cobra.Command , args []string ) error {
62- if val , err := cmd .Flags ().GetUint32 (logLevelFlag ); err == nil {
62+ if val , err := cmd .Flags ().GetUint32 ("log-level" ); err == nil {
6363 configCmd .logLevel = & val
6464 }
6565
@@ -93,7 +93,7 @@ func (c *configCommand) Validate(ctx context.Context) error {
9393 if ! stat .IsDir () {
9494 return fmt .Errorf ("application path for profile %s is not a directory" , c .listProfiles )
9595 }
96- profilesDir := filepath .Join (c .listProfiles , Profiles )
96+ profilesDir := filepath .Join (c .listProfiles , profile . Profiles )
9797 if _ , err := os .Stat (profilesDir ); os .IsNotExist (err ) {
9898 return err
9999 }
@@ -108,7 +108,7 @@ func (c *configCommand) ListProfiles() error {
108108 c .log .Info ("application path not provided" )
109109 return nil
110110 }
111- profilesDir := filepath .Join (c .listProfiles , Profiles )
111+ profilesDir := filepath .Join (c .listProfiles , profile . Profiles )
112112 entries , err := os .ReadDir (profilesDir )
113113 if err != nil {
114114 return err
@@ -148,7 +148,7 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
148148 return nil
149149 },
150150 RunE : func (cmd * cobra.Command , args []string ) error {
151- if val , err := cmd .Flags ().GetUint32 (logLevelFlag ); err == nil {
151+ if val , err := cmd .Flags ().GetUint32 ("log-level" ); err == nil {
152152 syncCmd .logLevel = & val
153153 }
154154 if insecure , err := cmd .Parent ().PersistentFlags ().GetBool ("insecure" ); err == nil {
@@ -159,7 +159,7 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
159159 log .Error (err , "failed to get applications from Hub" )
160160 return err
161161 }
162- profiles , err := syncCmd .getProfilesFromHubApplicaton (int (application . Resource .ID ))
162+ profiles , err := syncCmd .getProfilesFromHubApplication (int (application .ID ))
163163 if err != nil {
164164 return err
165165 }
@@ -177,15 +177,18 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
177177
178178 syncCommand .Flags ().StringVar (& syncCmd .url , "url" , "" , "url of the remote application repository" )
179179 syncCommand .MarkFlagRequired ("url" )
180- syncCommand .Flags ().StringVar (& syncCmd .applicationPath , "application-path" , "" , "path to the local application to download Hub profiles" )
181- syncCommand .MarkFlagRequired ("application-path" )
180+ syncCommand .Flags ().StringVar (& syncCmd .applicationPath , "application-path" , "" , "path to the local application for Hub profiles. Default is the current directory" )
182181
183182 return syncCommand
184183}
185184
186185func (s * syncCommand ) Validate (ctx context.Context ) error {
187186 if s .applicationPath == "" {
188- return fmt .Errorf ("application path is required" )
187+ defaultPath , err := s .setDefaultApplicationPath ()
188+ if err != nil {
189+ return err
190+ }
191+ s .applicationPath = defaultPath
189192 }
190193 stat , err := os .Stat (s .applicationPath )
191194 if err != nil {
@@ -198,6 +201,14 @@ func (s *syncCommand) Validate(ctx context.Context) error {
198201 return nil
199202}
200203
204+ func (s * syncCommand ) setDefaultApplicationPath () (string , error ) {
205+ currentDir , err := os .Getwd ()
206+ if err != nil {
207+ return "" , err
208+ }
209+ return currentDir , nil
210+ }
211+
201212func (s * syncCommand ) getHubClient () (* hubClient , error ) {
202213 var err error
203214 if s .hubClient == nil {
@@ -311,46 +322,46 @@ func (s *syncCommand) checkAuthentication() error {
311322 return nil
312323}
313324
314- func (s * syncCommand ) getApplicationFromHub (urlRepo string ) (api .Application , error ) {
325+ func (s * syncCommand ) getApplicationFromHub (urlRepo string ) (profile .Application , error ) {
315326 if err := s .checkAuthentication (); err != nil {
316- return api .Application {}, err
327+ return profile .Application {}, err
317328 }
318329 hubClient , err := s .getHubClient ()
319330 if err != nil {
320- return api .Application {}, err
331+ return profile .Application {}, err
321332 }
322333 path := fmt .Sprintf ("/applications?filter=repository.url='%s'" , urlRepo )
323334
324335 resp , err := hubClient .doRequest (path , "application/json" , s .log )
325336 if err != nil {
326- return api .Application {}, err
337+ return profile .Application {}, err
327338 }
328339 body , err := hubClient .readResponseBody (resp )
329340 if err != nil {
330- return api .Application {}, err
341+ return profile .Application {}, err
331342 }
332343
333344 apps , err := parseApplicationsFromHub (string (body ))
334345 if err != nil {
335- return api .Application {}, err
346+ return profile .Application {}, err
336347 }
337348 if len (apps ) == 0 {
338- return api .Application {}, fmt .Errorf ("no applications found in Hub for URL: %s" , urlRepo )
349+ return profile .Application {}, fmt .Errorf ("no applications found in Hub for URL: %s" , urlRepo )
339350
340351 // TODO handle multiple applications later
341352 } else if len (apps ) > 1 {
342- return api .Application {}, fmt .Errorf ("multiple applications found in Hub for URL: %s" , urlRepo )
353+ return profile .Application {}, fmt .Errorf ("multiple applications found in Hub for URL: %s" , urlRepo )
343354 }
344- var application api .Application
345- if apps [0 ].Repository .URL == s .url {
355+ var application profile .Application
356+ if apps [0 ].Repository != nil && apps [ 0 ]. Repository .URL == s .url {
346357 application = apps [0 ]
347358 }
348359
349360 return application , nil
350361}
351362
352- func parseApplicationsFromHub (jsonData string ) ([]api .Application , error ) {
353- var apps []api .Application
363+ func parseApplicationsFromHub (jsonData string ) ([]profile .Application , error ) {
364+ var apps []profile .Application
354365
355366 err := json .Unmarshal ([]byte (jsonData ), & apps )
356367 if err != nil {
@@ -360,7 +371,7 @@ func parseApplicationsFromHub(jsonData string) ([]api.Application, error) {
360371 return apps , nil
361372}
362373
363- func (s * syncCommand ) getProfilesFromHubApplicaton (appID int ) ([]api .AnalysisProfile , error ) {
374+ func (s * syncCommand ) getProfilesFromHubApplication (appID int ) ([]profile .AnalysisProfile , error ) {
364375 hubClient , err := s .getHubClient ()
365376 if err != nil {
366377 return nil , err
@@ -374,7 +385,7 @@ func (s *syncCommand) getProfilesFromHubApplicaton(appID int) ([]api.AnalysisPro
374385 if err != nil {
375386 return nil , err
376387 }
377- profiles := []api .AnalysisProfile {}
388+ profiles := []profile .AnalysisProfile {}
378389 if err := yaml .Unmarshal (body , & profiles ); err != nil {
379390 return nil , err
380391 }
@@ -388,8 +399,8 @@ func (s *syncCommand) downloadProfileBundle(profileID int) error {
388399 return err
389400 }
390401 filename := fmt .Sprintf ("profile-%d.tar" , profileID )
391- downloadPath := filepath .Join (s .applicationPath , Profiles , filename )
392- err = os .MkdirAll (filepath .Join (s .applicationPath , Profiles ), 0755 )
402+ downloadPath := filepath .Join (s .applicationPath , profile . Profiles , filename )
403+ err = os .MkdirAll (filepath .Join (s .applicationPath , profile . Profiles ), 0755 )
393404 if err != nil {
394405 return err
395406 }
0 commit comments