1 // Copyright © 2015 Steve Francia <spf@spf13.com>.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
27 var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
31 // Initialize srcPaths.
32 envGoPath := os.Getenv("GOPATH")
33 goPaths := filepath.SplitList(envGoPath)
34 if len(goPaths) == 0 {
35 // Adapted from https://github.com/Masterminds/glide/pull/798/files.
36 // As of Go 1.8 the GOPATH is no longer required to be set. Instead there
37 // is a default value. If there is no GOPATH check for the default value.
38 // Note, checking the GOPATH first to avoid invoking the go toolchain if
41 goExecutable := os.Getenv("COBRA_GO_EXECUTABLE")
42 if len(goExecutable) <= 0 {
46 out, err := exec.Command(goExecutable, "env", "GOPATH").Output()
51 toolchainGoPath := strings.TrimSpace(string(out))
52 goPaths = filepath.SplitList(toolchainGoPath)
53 if len(goPaths) == 0 {
54 er("$GOPATH is not set")
57 srcPaths = make([]string, 0, len(goPaths))
58 for _, goPath := range goPaths {
59 srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
63 func er(msg interface{}) {
64 fmt.Println("Error:", msg)
68 // isEmpty checks if a given path is empty.
69 // Hidden files in path are ignored.
70 func isEmpty(path string) bool {
71 fi, err := os.Stat(path)
80 f, err := os.Open(path)
86 names, err := f.Readdirnames(-1)
87 if err != nil && err != io.EOF {
91 for _, name := range names {
92 if len(name) > 0 && name[0] != '.' {
99 // exists checks if a file or directory exists.
100 func exists(path string) bool {
104 _, err := os.Stat(path)
108 if !os.IsNotExist(err) {
114 func executeTemplate(tmplStr string, data interface{}) (string, error) {
115 tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr)
120 buf := new(bytes.Buffer)
121 err = tmpl.Execute(buf, data)
122 return buf.String(), err
125 func writeStringToFile(path string, s string) error {
126 return writeToFile(path, strings.NewReader(s))
129 // writeToFile writes r to file with path only
130 // if file/directory on given path doesn't exist.
131 // If file/directory exists on given path, then
132 // it terminates app and prints an appropriate error.
133 func writeToFile(path string, r io.Reader) error {
135 return fmt.Errorf("%v already exists", path)
138 dir := filepath.Dir(path)
140 if err := os.MkdirAll(dir, 0777); err != nil {
145 file, err := os.Create(path)
151 _, err = io.Copy(file, r)
155 // commentfyString comments every line of in.
156 func commentifyString(in string) string {
157 var newlines []string
158 lines := strings.Split(in, "\n")
159 for _, line := range lines {
160 if strings.HasPrefix(line, "//") {
161 newlines = append(newlines, line)
164 newlines = append(newlines, "//")
166 newlines = append(newlines, "// "+line)
170 return strings.Join(newlines, "\n")