forked from remogatto/mandala-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (98 loc) · 2.51 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var (
verbose *bool
rootDir = "template"
)
func verboseLog(format string, message ...interface{}) {
if *verbose {
log.Printf(format, message...)
}
}
func copyFile(srcFile, dstPath, dstFile string) error {
// Get the full path of destination file
fullDstPath := filepath.Join(dstPath, dstFile)
// Read source file
srcData, err := ioutil.ReadFile(srcFile)
if err != nil {
return err
}
// Get source FileMode
srcFileInfo, err := os.Stat(srcFile)
// Create the destination subdirectories
dir := filepath.Dir(fullDstPath)
err = os.MkdirAll(dir, 0777)
if err != nil {
return err
}
err = ioutil.WriteFile(fullDstPath, srcData, srcFileInfo.Mode())
if err != nil {
return err
}
verboseLog("%s copied in %s\n", srcFile, fullDstPath)
return nil
}
func main() {
defaultInstallPath := filepath.Join(os.Getenv("GOPATH"), "src/github.com/remogatto/mandala-template/")
installPath := flag.String("install-path", defaultInstallPath, "Package installation directory")
help := flag.Bool("help", false, "Show usage")
verbose = flag.Bool("verbose", false, "Be verbose")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "mandala-template - Create a template for a basic Mandala application\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\n")
fmt.Fprintf(os.Stderr, "\tmandala-template [options] dirname\n\n")
fmt.Fprintf(os.Stderr, "Options are:\n\n")
flag.PrintDefaults()
}
flag.Parse()
if *help == true {
flag.Usage()
return
}
if len(flag.Args()) != 1 {
flag.Usage()
}
dstPath := flag.Arg(0)
if _, err := os.Stat(dstPath); err == nil {
panic(fmt.Errorf("Directory %s already exists\n", dstPath))
}
templatePath := filepath.Join(*installPath, rootDir)
err := filepath.Walk(
templatePath,
func(src string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
splits := strings.Split(src, "/")
dstParts := make([]string, len(splits))
for i := 0; i < len(splits); i++ {
index := len(splits) - i - 1
if splits[index] != rootDir {
continue
}
copy(dstParts, splits[index+1:len(splits)])
break
}
err := copyFile(src, dstPath, filepath.Join(dstParts...))
if err != nil {
return err
}
}
return nil
},
)
if err != nil {
panic(err)
}
fmt.Printf("A new Mandala template was successful created in %s\n", dstPath)
fmt.Printf("Now:\n\n\tcd %s\n\tgotask init\n\tgotask run xorg # or\n\tgotask run android\n\n", dstPath)
}