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

147 lines
3.1 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"
"path/filepath"
"strings"
"text/template"
)
2017-04-29 10:02:02 +00:00
var projectPath string
2015-10-28 16:51:48 +00:00
2017-04-29 10:02:02 +00:00
var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}
var goPaths, srcPaths []string
2015-10-28 16:51:48 +00:00
2017-04-29 10:02:02 +00:00
func init() {
// Initialize goPaths and srcPaths
envGoPath := os.Getenv("GOPATH")
if envGoPath == "" {
er("$GOPATH is not set")
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
goPaths = filepath.SplitList(envGoPath)
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-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
}
if fi.IsDir() {
f, err := os.Open(path)
if err != nil {
2017-04-29 10:02:02 +00:00
er(err)
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
defer f.Close()
dirs, err := f.Readdirnames(1)
if err != nil && err != io.EOF {
er(err)
}
return len(dirs) == 0
2015-10-28 16:51:48 +00:00
}
2017-04-29 10:02:02 +00:00
return fi.Size() == 0
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 13:54:32 +00:00
var tmpl = template.New("").Funcs(template.FuncMap{"comment": commentifyString})
2017-04-29 10:02:02 +00:00
func executeTemplate(tmplStr string, data interface{}) (string, error) {
2017-04-29 13:54:32 +00:00
var err error
tmpl, err = tmpl.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")
}