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

57 lines
1.7 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 (
"os"
"os/exec"
2015-10-28 16:51:48 +00:00
"path/filepath"
"strings"
"github.com/spf13/cobra"
2015-10-28 16:51:48 +00:00
)
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()
cobra.CheckErr(err)
toolchainGoPath := strings.TrimSpace(string(out))
goPaths = filepath.SplitList(toolchainGoPath)
if len(goPaths) == 0 {
cobra.CheckErr("$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
}
}