-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
113 lines (96 loc) · 1.97 KB
/
Copy pathcli.go
File metadata and controls
113 lines (96 loc) · 1.97 KB
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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
// CLI is the command line object
type CLI struct {
stdout io.Writer
stderr io.Writer
fatalLog *log.Logger
cfg *Config
}
func newCLI(stdout, stderr io.Writer, args []string) (*CLI, error) {
cfg, err := loadConfig(stdout, stderr, args)
if err != nil {
return nil, err
}
return &CLI{
stdout: stdout,
stderr: stderr,
fatalLog: newFatalLogger(stderr),
cfg: cfg,
}, nil
}
func newFatalLogger(stderr io.Writer) *log.Logger {
return log.New(stderr, "fatal: ", 0)
}
// main process
func (c *CLI) run() int {
if c.cfg.showList {
return c.outputList()
}
// copy boilerplate-name to project-name
err := c.copyDir()
if err != nil {
c.fatalLog.Println(err)
return exitCodeError
}
return exitCodeOK
}
func (c *CLI) outputList() int {
blist, err := c.cfg.blplList()
if err != nil {
c.fatalLog.Println(err)
return exitCodeError
}
// print
for _, name := range blist {
fmt.Fprintln(c.stdout, name)
}
return exitCodeOK
}
func (c *CLI) copyDir() error {
dst := c.cfg.projectPath
src := filepath.Join(c.cfg.root, c.cfg.boilerplateName)
if !fileExists(src) {
c.fatalLog.Printf("Not exists directory '%s'", src)
}
os.Mkdir(dst, 0755)
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
// e.g.
// src = /home/foo
// path = /home/foo/bar
// /bar
path = strings.TrimPrefix(path, src)
// skip src root dir
if path == "" {
return nil
}
if info.IsDir() { // make dest dir
dstDir := filepath.Join(dst, path)
err := os.Mkdir(dstDir, info.Mode())
if err != nil {
return err
}
} else { // copy file
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(filepath.Join(dst, path))
if err != nil {
return err
}
defer dstFile.Close()
// TODO: hook to find template
io.Copy(dstFile, srcFile)
}
return nil
})
}