1// Copyright © 2015 Steve Francia <spf@spf13.com>.
2//
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
7//
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.
13
14package cmd
15
16import (
17	"bytes"
18	"fmt"
19	"io"
20	"os"
21	"os/exec"
22	"path/filepath"
23	"strings"
24	"text/template"
25)
26
27var srcPaths []string
28
29func init() {
30	// Initialize srcPaths.
31	envGoPath := os.Getenv("GOPATH")
32	goPaths := filepath.SplitList(envGoPath)
33	if len(goPaths) == 0 {
34		// Adapted from https://github.com/Masterminds/glide/pull/798/files.
35		// As of Go 1.8 the GOPATH is no longer required to be set. Instead there
36		// is a default value. If there is no GOPATH check for the default value.
37		// Note, checking the GOPATH first to avoid invoking the go toolchain if
38		// possible.
39
40		goExecutable := os.Getenv("COBRA_GO_EXECUTABLE")
41		if len(goExecutable) <= 0 {
42			goExecutable = "go"
43		}
44
45		out, err := exec.Command(goExecutable, "env", "GOPATH").Output()
46		if err != nil {
47			er(err)
48		}
49
50		toolchainGoPath := strings.TrimSpace(string(out))
51		goPaths = filepath.SplitList(toolchainGoPath)
52		if len(goPaths) == 0 {
53			er("$GOPATH is not set")
54		}
55	}
56	srcPaths = make([]string, 0, len(goPaths))
57	for _, goPath := range goPaths {
58		srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
59	}
60}
61
62func er(msg interface{}) {
63	fmt.Println("Error:", msg)
64	os.Exit(1)
65}
66
67// isEmpty checks if a given path is empty.
68// Hidden files in path are ignored.
69func isEmpty(path string) bool {
70	fi, err := os.Stat(path)
71	if err != nil {
72		er(err)
73	}
74
75	if !fi.IsDir() {
76		return fi.Size() == 0
77	}
78
79	f, err := os.Open(path)
80	if err != nil {
81		er(err)
82	}
83	defer f.Close()
84
85	names, err := f.Readdirnames(-1)
86	if err != nil && err != io.EOF {
87		er(err)
88	}
89
90	for _, name := range names {
91		if len(name) > 0 && name[0] != '.' {
92			return false
93		}
94	}
95	return true
96}
97
98// exists checks if a file or directory exists.
99func exists(path string) bool {
100	if path == "" {
101		return false
102	}
103	_, err := os.Stat(path)
104	if err == nil {
105		return true
106	}
107	if !os.IsNotExist(err) {
108		er(err)
109	}
110	return false
111}
112
113func executeTemplate(tmplStr string, data interface{}) (string, error) {
114	tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr)
115	if err != nil {
116		return "", err
117	}
118
119	buf := new(bytes.Buffer)
120	err = tmpl.Execute(buf, data)
121	return buf.String(), err
122}
123
124func writeStringToFile(path string, s string) error {
125	return writeToFile(path, strings.NewReader(s))
126}
127
128// writeToFile writes r to file with path only
129// if file/directory on given path doesn't exist.
130func writeToFile(path string, r io.Reader) error {
131	if exists(path) {
132		return fmt.Errorf("%v already exists", path)
133	}
134
135	dir := filepath.Dir(path)
136	if dir != "" {
137		if err := os.MkdirAll(dir, 0777); err != nil {
138			return err
139		}
140	}
141
142	file, err := os.Create(path)
143	if err != nil {
144		return err
145	}
146	defer file.Close()
147
148	_, err = io.Copy(file, r)
149	return err
150}
151
152// commentfyString comments every line of in.
153func commentifyString(in string) string {
154	var newlines []string
155	lines := strings.Split(in, "\n")
156	for _, line := range lines {
157		if strings.HasPrefix(line, "//") {
158			newlines = append(newlines, line)
159		} else {
160			if line == "" {
161				newlines = append(newlines, "//")
162			} else {
163				newlines = append(newlines, "// "+line)
164			}
165		}
166	}
167	return strings.Join(newlines, "\n")
168}
169