@@ -16,25 +16,20 @@ import (
16
16
)
17
17
18
18
const (
19
- githubTarballURL = "https://github.com/dispatchrun/ %s/tarball/main"
20
- githubAPIURL = "https://api.github.com/repos/dispatchrun/ %s/branches/main"
21
- repo = "dispatch-examples"
19
+ githubTarballURL = "https://github.com/%s/tarball/main"
20
+ githubAPIURL = "https://api.github.com/repos/%s/branches/main"
21
+ repo = "dispatchrun/ dispatch-examples"
22
22
dispatchUserDir = "dispatch"
23
23
)
24
24
25
- // TODO: versioning for different SDKs?
26
-
27
25
func directoryExists (path string ) (bool , error ) {
28
26
info , err := os .Stat (path )
29
27
if os .IsNotExist (err ) {
30
- // The directory does not exist
31
28
return false , nil
32
29
}
33
30
if err != nil {
34
- // Some other error occurred
35
31
return false , err
36
32
}
37
- // Check if the path is a directory
38
33
return info .IsDir (), nil
39
34
}
40
35
@@ -96,6 +91,8 @@ func extractTarball(r io.Reader, destDir string) error {
96
91
continue
97
92
}
98
93
94
+ // We need to strip the top-level directory from the file paths
95
+ // It contains the repository name and the commit SHA which we don't need
99
96
// Get the top-level directory name
100
97
if topLevelDir == "" {
101
98
parts := strings .Split (header .Name , "/" )
@@ -286,18 +283,15 @@ func initCommand() *cobra.Command {
286
283
cmd := & cobra.Command {
287
284
Use : "init <template> [path]" ,
288
285
Short : "Initialize a new Dispatch project" ,
289
- Long : "Initialize a new Dispatch project" ,
290
- Args : cobra .MinimumNArgs (1 ),
286
+ // Args: cobra.MinimumNArgs(1),
291
287
RunE : func (cmd * cobra.Command , args []string ) error {
292
- var directory string
293
- var exists = true
294
-
288
+ // get or create the Dispatch templates directory
295
289
dispatchUserDirPath , err := getAppDataDir (dispatchUserDir )
296
290
if err != nil {
297
- cmd .SilenceUsage = true
298
- return fmt .Errorf ("failed to get Dispatch templates directory: %w" , err )
291
+ fmt .Printf ("failed to get Dispatch templates directory: %s" , err )
299
292
}
300
293
294
+ // well-known paths for Dispatch templates
301
295
dispatchTemplatesDirPath := filepath .Join (dispatchUserDirPath , "templates" )
302
296
dispatchTemplatesHashPath := filepath .Join (dispatchUserDirPath , "templates.sha" )
303
297
@@ -306,15 +300,17 @@ func initCommand() *cobra.Command {
306
300
if err != nil {
307
301
if ! os .IsNotExist (err ) {
308
302
cmd .SilenceUsage = true
309
- return fmt . Errorf ("failed to read templates SHA: %w " , err )
303
+ cmd . PrintErrf ("failed to read templates SHA: %s " , err )
310
304
}
311
305
}
312
306
307
+ // get the latest commit SHA from the templates repository
313
308
remoteSHA , err := getLatestCommitSHA (repo )
314
309
if err != nil {
315
310
cmd .Printf ("failed to get latest commit SHA: %v" , err )
316
311
}
317
312
313
+ // update the templates if the latest commit SHA is different
318
314
if remoteSHA != "" && string (sha ) != remoteSHA {
319
315
cmd .Println ("Templates update available. Do you want to download the latest templates? [y/N]" )
320
316
@@ -327,7 +323,7 @@ func initCommand() *cobra.Command {
327
323
if err != nil {
328
324
cmd .Printf ("failed to download and extract templates: %v" , err )
329
325
} else {
330
- cmd .Print ("Templates have been updated\n " )
326
+ cmd .Print ("Templates have been updated\n \n " )
331
327
// TODO: possible improvement:
332
328
// find which templates have been added/removed/modified
333
329
// and/or
@@ -342,18 +338,42 @@ func initCommand() *cobra.Command {
342
338
}
343
339
}
344
340
341
+ // read the available templates
345
342
templates , err := readDirectories (dispatchTemplatesDirPath )
343
+
346
344
if err != nil {
347
345
cmd .SilenceUsage = true
348
346
if os .IsNotExist (err ) {
349
- return fmt . Errorf ("templates directory does not exist in %s. Please run `dispatch init` to download the templates" , dispatchTemplatesDirPath )
347
+ cmd . PrintErrf ("templates directory does not exist in %s. Please run `dispatch init` to download the templates" , dispatchTemplatesDirPath )
350
348
}
351
- return fmt . Errorf ("failed to read templates directory. : %w " , err )
349
+ cmd . PrintErrf ("failed to read templates directory. : %s " , err )
352
350
}
353
351
352
+ if len (templates ) == 0 {
353
+ cmd .SilenceUsage = true
354
+ return fmt .Errorf ("templates directory %s is corrupted. Please clean it and try again" , dispatchTemplatesDirPath )
355
+ }
356
+
357
+ var templatesList string = ""
358
+
359
+ for _ , template := range templates {
360
+ templatesList += " " + template + "\n "
361
+ }
362
+ cmd .SetUsageTemplate (cmd .UsageTemplate () + "\n Available templates:\n " + templatesList )
363
+
364
+ // if no arguments are provided (user wants to download/update templates only), print the usage
365
+ if len (args ) == 0 {
366
+ cmd .Print (cmd .UsageString ())
367
+ return nil
368
+ }
369
+
370
+ var directory string
371
+ var exists = true
372
+
354
373
wantedTemplate := args [0 ]
355
374
isTemplateFound := false
356
375
376
+ // find template in the available templates
357
377
for _ , template := range templates {
358
378
if template == wantedTemplate {
359
379
isTemplateFound = true
@@ -363,14 +383,11 @@ func initCommand() *cobra.Command {
363
383
364
384
if ! isTemplateFound {
365
385
cmd .SilenceUsage = true
366
- cmd .Printf ("Template %v is not supported.\n Available templates:\n " , wantedTemplate )
367
- for _ , template := range templates {
368
- cmd .Println (" " + template )
369
- }
370
- cmd .Println ()
386
+ cmd .Printf ("Template %s is not supported.\n \n Available templates:\n %s" , wantedTemplate , templatesList )
371
387
return nil
372
388
}
373
389
390
+ // check if a directory is provided
374
391
if len (args ) > 1 {
375
392
directory = args [1 ]
376
393
flag , err := directoryExists (directory )
@@ -394,6 +411,7 @@ func initCommand() *cobra.Command {
394
411
directory = "."
395
412
}
396
413
414
+ // check if the if directory exists and is empty
397
415
if exists {
398
416
isEmpty , err := isDirectoryEmpty (directory )
399
417
if err != nil {
@@ -435,5 +453,6 @@ func initCommand() *cobra.Command {
435
453
return nil
436
454
},
437
455
}
456
+
438
457
return cmd
439
458
}
0 commit comments