spf13--cobra/cobra/cmd/helpers.go

172 lines
3.8 KiB
Go
Raw Normal View History

2015-10-28 16:51:48 +00:00
// Copyright © 2015 Steve Francia <spf@spf13.com>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
2015-10-28 16:51:48 +00:00
"path/filepath"
"strings"
"text/template"
)
2017-04-29 10:02:02 +00:00
var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
2017-05-05 08:06:10 +00:00
var srcPaths []string
2015-10-28 16:51:48 +00:00
2017-04-29 10:02:02 +00:00
func init() {
// Initialize srcPaths.
2017-04-29 10:02:02 +00:00
envGoPath := os.Getenv("GOPATH")
goPaths := filepath.SplitList(envGoPath)
if len(goPaths) == 0 {
// Adapted from https://github.com/Masterminds/glide/pull/798/files.
// As of Go 1.8 the GOPATH is no longer required to be set. Instead there
// is a default value. If there is no GOPATH check for the default value.
// Note, checking the GOPATH first to avoid invoking the go toolchain if
// possible.
goExecutable := os.Getenv("COBRA_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOPATH").Output()
if err != nil {
er(err)
}
toolchainGoPath := strings.TrimSpace(string(out))
goPaths = filepath.SplitList(toolchainGoPath)
if len(goPaths) == 0 {
er("$GOPATH is not set")
}
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
srcPaths = make([]string, 0, len(goPaths))
for _, goPath := range goPaths {
srcPaths = append(srcPaths, filepath.Join(goPath, "src"))
2015-10-28 16:51:48 +00:00
}
}
2017-04-29 10:02:02 +00:00
func er(msg interface{}) {
fmt.Println("Error:", msg)
os.Exit(1)
2015-10-28 16:51:48 +00:00
}
// isEmpty checks if a given path is empty.
2017-07-18 16:22:37 +00:00
// Hidden files in path are ignored.
2017-04-29 10:02:02 +00:00
func isEmpty(path string) bool {
2015-10-28 16:51:48 +00:00
fi, err := os.Stat(path)
if err != nil {
2017-04-29 10:02:02 +00:00
er(err)
2015-10-28 16:51:48 +00:00
}
2017-07-18 16:22:37 +00:00
if !fi.IsDir() {
return fi.Size() == 0
}
f, err := os.Open(path)
if err != nil {
er(err)
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil && err != io.EOF {
er(err)
}
for _, name := range names {
if len(name) > 0 && name[0] != '.' {
return false
2017-04-29 10:02:02 +00:00
}
2015-10-28 16:51:48 +00:00
}
2017-07-18 16:22:37 +00:00
return true
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
// exists checks if a file or directory exists.
func exists(path string) bool {
if path == "" {
return false
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
_, err := os.Stat(path)
if err == nil {
return true
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
if !os.IsNotExist(err) {
er(err)
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
return false
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
func executeTemplate(tmplStr string, data interface{}) (string, error) {
tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr)
2015-10-28 16:51:48 +00:00
if err != nil {
2017-04-29 10:02:02 +00:00
return "", err
2015-10-28 16:51:48 +00:00
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, data)
2017-04-29 10:02:02 +00:00
return buf.String(), err
}
2015-10-28 16:51:48 +00:00
2017-04-29 10:02:02 +00:00
func writeStringToFile(path string, s string) error {
2017-04-29 12:53:52 +00:00
return writeToFile(path, strings.NewReader(s))
2015-10-28 16:51:48 +00:00
}
2017-04-29 12:53:52 +00:00
// writeToFile writes r to file with path only
// if file/directory on given path doesn't exist.
// If file/directory exists on given path, then
// it terminates app and prints an appropriate error.
func writeToFile(path string, r io.Reader) error {
if exists(path) {
return fmt.Errorf("%v already exists", path)
2015-10-28 16:51:48 +00:00
}
2017-04-29 12:53:52 +00:00
dir := filepath.Dir(path)
if dir != "" {
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
2017-04-29 10:02:02 +00:00
}
2015-10-28 16:51:48 +00:00
2017-04-29 12:53:52 +00:00
file, err := os.Create(path)
2015-10-28 16:51:48 +00:00
if err != nil {
2017-04-29 12:53:52 +00:00
return err
2015-10-28 16:51:48 +00:00
}
defer file.Close()
_, err = io.Copy(file, r)
2017-04-29 12:53:52 +00:00
return err
2015-10-28 16:51:48 +00:00
}
2017-04-29 12:53:52 +00:00
// commentfyString comments every line of in.
2015-10-28 16:51:48 +00:00
func commentifyString(in string) string {
var newlines []string
lines := strings.Split(in, "\n")
2017-04-29 10:02:02 +00:00
for _, line := range lines {
if strings.HasPrefix(line, "//") {
newlines = append(newlines, line)
} else {
if line == "" {
newlines = append(newlines, "//")
2017-04-29 10:02:02 +00:00
} else {
newlines = append(newlines, "// "+line)
}
2015-10-28 16:51:48 +00:00
}
}
return strings.Join(newlines, "\n")
}