forked from dojo-brno/dojo-brno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
63 lines (57 loc) · 1.34 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// +build ignore
// This is a helper "script" to create a directory structure for a new dojo
// session with today's date. Run it as:
//
// go run new.go PACKAGE_NAME [EXERCISE_URL]
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
const usage = `go run new.go PACKAGE_NAME [EXERCISE_URL]`
func createDatedPackage(name string) (path string, err error) {
date := time.Now().Format("2006/2006-01-02")
path = filepath.Join(date, name)
if _, err := os.Stat(path); err == nil {
return path, fmt.Errorf("path already exists, aborting: %s", path)
}
return path, os.MkdirAll(path, 0775)
}
func createCommonFiles(path string, url string) error {
f, err := os.Create(filepath.Join(path, "AUTHORS"))
if err != nil {
return err
}
f.Close()
f, err = os.Create(filepath.Join(path, "README.md"))
if err != nil {
return err
}
defer f.Close()
fmt.Fprintf(f, "# Exercise\n\n%s\n", url)
return nil
}
func main() {
if l := len(os.Args); l < 2 || l > 3 {
fmt.Println("usage:", usage)
os.Exit(1)
}
name := strings.Replace(os.Args[1], " ", "_", -1)
var url string
if len(os.Args) > 2 {
url = os.Args[2]
}
path, err := createDatedPackage(name)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
if err := createCommonFiles(path, url); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
fmt.Printf("cd %q\n", path)
}