Skip to content

Commit fc3dcd6

Browse files
add gozer new command
1 parent 1240bb9 commit fc3dcd6

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Sample websites using Gozer:
1212
- [Simplest possible example](example/)
1313
- [My personal website](https://github.com/dannyvankooten/www.dannyvankooten.com)
1414

15-
1615
## Directory structure
1716

1817
Gozer expects a certain directory structure in order to build your site correctly.
@@ -36,14 +35,16 @@ Gozer - a fast & simple static site generator
3635
Usage: gozer [OPTIONS] <COMMAND>
3736
3837
Commands:
39-
build Deletes the output directory if there is one and builds the site
40-
serve Builds the site and starts an HTTP server on http://localhost:8080
38+
build Deletes the output directory if there is one and builds the site
39+
serve Builds the site and starts an HTTP server on http://localhost:8080
40+
new Creates a new site structure in the given directory
4141
4242
Options:
43-
-r, --root <ROOT> Directory to use as root of project (default: .)
44-
-c, --config <CONFIG> Path to confiruation file (default: config.xml)
43+
-r, --root <ROOT> Directory to use as root of project (default: .)
44+
-c, --config <CONFIG> Path to confiruation file (default: config.xml)
4545
```
4646

47+
Run `gozer new` in an empty directory to quickly create the directory structure for a basic site.
4748

4849
## Content files
4950

main.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,15 @@ func main() {
385385
flag.Parse()
386386

387387
command := os.Args[len(os.Args)-1]
388-
if command != "build" && command != "serve" {
388+
if command != "build" && command != "serve" && command != "new" {
389389
fmt.Printf(`Gozer - a fast & simple static site generator
390390
391391
Usage: gozer [OPTIONS] <COMMAND>
392392
393393
Commands:
394394
build Deletes the output directory if there is one and builds the site
395395
serve Builds the site and starts an HTTP server on http://localhost:8080
396+
new Creates a new site structure in the given directory
396397
397398
Options:
398399
-r, --root <ROOT> Directory to use as root of project (default: .)
@@ -401,10 +402,18 @@ Options:
401402
return
402403
}
403404

405+
// ensure rootPath has a trailing slash
404406
if rootPath != "" {
405407
rootPath = strings.TrimSuffix(rootPath, "/") + "/"
406408
}
407409

410+
if command == "new" {
411+
if err := createDirectoryStructure(rootPath); err != nil {
412+
log.Fatal("Error creating site structure: ", err)
413+
}
414+
return
415+
}
416+
408417
buildSite(rootPath, configFile)
409418

410419
if command == "serve" {
@@ -413,6 +422,44 @@ Options:
413422
}
414423
}
415424

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("+++\ntitle = \"Gozer!\"\n+++\n\nWelcome to my website.\n")
458+
fh.Close()
459+
460+
return nil
461+
}
462+
416463
func buildSite(rootPath string, configFile string) {
417464
var err error
418465
timeStart := time.Now()

0 commit comments

Comments
 (0)