@@ -385,14 +385,15 @@ func main() {
385
385
flag .Parse ()
386
386
387
387
command := os .Args [len (os .Args )- 1 ]
388
- if command != "build" && command != "serve" {
388
+ if command != "build" && command != "serve" && command != "new" {
389
389
fmt .Printf (`Gozer - a fast & simple static site generator
390
390
391
391
Usage: gozer [OPTIONS] <COMMAND>
392
392
393
393
Commands:
394
394
build Deletes the output directory if there is one and builds the site
395
395
serve Builds the site and starts an HTTP server on http://localhost:8080
396
+ new Creates a new site structure in the given directory
396
397
397
398
Options:
398
399
-r, --root <ROOT> Directory to use as root of project (default: .)
@@ -401,10 +402,18 @@ Options:
401
402
return
402
403
}
403
404
405
+ // ensure rootPath has a trailing slash
404
406
if rootPath != "" {
405
407
rootPath = strings .TrimSuffix (rootPath , "/" ) + "/"
406
408
}
407
409
410
+ if command == "new" {
411
+ if err := createDirectoryStructure (rootPath ); err != nil {
412
+ log .Fatal ("Error creating site structure: " , err )
413
+ }
414
+ return
415
+ }
416
+
408
417
buildSite (rootPath , configFile )
409
418
410
419
if command == "serve" {
@@ -413,6 +422,44 @@ Options:
413
422
}
414
423
}
415
424
425
+ func createDirectoryStructure (rootPath string ) error {
426
+ if err := os .Mkdir (rootPath + "content" , 0755 ); err != nil {
427
+ return err
428
+ }
429
+ if err := os .Mkdir (rootPath + "templates" , 0755 ); err != nil {
430
+ return err
431
+ }
432
+ if err := os .Mkdir (rootPath + "public" , 0755 ); err != nil {
433
+ return err
434
+ }
435
+
436
+ // create config.xml
437
+ fh , err := os .Create (rootPath + "config.xml" )
438
+ if err != nil {
439
+ return err
440
+ }
441
+ _ , _ = fh .WriteString ("<config>\n \t <site_url>http://localhost:8080</site_url>\n </config>" )
442
+ fh .Close ()
443
+
444
+ // create default template
445
+ fh , err = os .Create (rootPath + "templates/default.html" )
446
+ if err != nil {
447
+ return err
448
+ }
449
+ _ , _ = fh .WriteString ("<!DOCTYPE html>\n <head>\n \t <title>{{ .Title }}</title>\n </head>\n <body>\n {{ .Content }}\n </body>\n </html>" )
450
+ fh .Close ()
451
+
452
+ // create homepage
453
+ fh , err = os .Create (rootPath + "content/index.html" )
454
+ if err != nil {
455
+ return err
456
+ }
457
+ _ , _ = fh .WriteString ("+++\n title = \" Gozer!\" \n +++\n \n Welcome to my website.\n " )
458
+ fh .Close ()
459
+
460
+ return nil
461
+ }
462
+
416
463
func buildSite (rootPath string , configFile string ) {
417
464
var err error
418
465
timeStart := time .Now ()
0 commit comments