Skip to content

Commit

Permalink
Disable history file on CI environments, fix home dir perms, more log…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
pnegahdar committed Mar 19, 2018
1 parent fce3e64 commit 937cf20
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
38 changes: 25 additions & 13 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,20 @@ func makeScriptCommand(manager *venvy.ProjectManager, script *foundScript) func(
}
}

func isCIEnv() bool {
_, isCI := os.LookupEnv("CI")
_, isJenkins := os.LookupEnv("JENKINS_URL")
return isCI || isJenkins
}

func LoadConfigCommands() ([]*cobra.Command, error) {
cmds := []*cobra.Command{}
useHistory := true
if isCIEnv(){
logger.Debug("Not using config history, CI environment detected.")
}
if os.Getenv(disableHistoryEnvVar) != "" {
logger.Debugf("Not using config history because envar %s is set", disableHistoryEnvVar)
useHistory = false
}
foundConfigs := LoadConfigs(true, useHistory)
Expand Down Expand Up @@ -301,7 +311,7 @@ func handleCliInit() {
func main() {
var err error
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose logging")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, fmt.Sprintf("disable all logging", venvy.ProjectName))
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "disable all logging")

logger.SetOutput(os.Stderr) // default but explicit

Expand All @@ -316,21 +326,23 @@ func main() {
rootCmd.AddCommand(evalCmd)
cobra.OnInitialize(handleCliInit)

subCommand := ""
if len(os.Args) > 1 {
subCommand = os.Args[1]
}

if subCommand != "version" && subCommand != "shell-init" {
configCmds, err := LoadConfigCommands()
if err != nil {
errExit(err)
// Set debug early on so
for _, arg := range os.Args {
if arg == "-v" || arg == "--verbose"{
logger.SetLevel(logger.DebugLevel)
}

for _, cmd := range configCmds {
rootCmd.AddCommand(cmd)
if arg == "--"{
break
}
}
configCmds, err := LoadConfigCommands()
if err != nil {
errExit(err)
}

for _, cmd := range configCmds {
rootCmd.AddCommand(cmd)
}
err = rootCmd.Execute()
errExit(err)
}
24 changes: 19 additions & 5 deletions cli_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func globalPath(elem ...string) string {
if err != nil {
panic(err)
}
return path.Join(append([]string{dotDir(homeDir)}, elem...)...)
venvyDir := dotDir(homeDir)
os.MkdirAll(venvyDir, 0700)
return path.Join(append([]string{venvyDir}, elem...)...)
}

type foundScript struct {
Expand Down Expand Up @@ -215,6 +217,7 @@ func (f *foundConfig) Scripts() map[string][]*foundScript {

func configPathsFromGit() []*foundConfig {
paths := []*foundConfig{}
seenPaths := map[string]bool{}
gitRoot, err := util.FindPathInAncestors("", ".git")
storageDir := dotDir(gitRoot)
if err != nil {
Expand All @@ -225,21 +228,30 @@ func configPathsFromGit() []*foundConfig {

addedFilesArgs := []string{"ls-files", "--others", "--exclude-standard", "--full-name"}
lsSubmodulesArgs := []string{"ls-files", "--recurse-submodules", "--full-name"}
// TODO: older versions of git don't have --recurse submodules, perhaps detect instead of trying again
lsNoSubmodules := []string{"ls-files", "--full-name"}

for _, args := range [][]string{addedFilesArgs, lsSubmodulesArgs} {
cmd := exec.Command("git", append(baseArgs, args...)...)
for _, args := range [][]string{addedFilesArgs, lsSubmodulesArgs, lsNoSubmodules} {
runArgs := append(baseArgs, args...)
logger.Debugf("Running command: git %s", strings.Join(runArgs, " "))
cmd := exec.Command("git", runArgs...)
data, err := cmd.Output()
if err != nil {
logger.Debugf("ran into err %s doing git ls-files", err)
} else {
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
for _, line := range lines {
if strings.HasSuffix(line, defaultFileName) {
paths = append(paths, &foundConfig{Path: path.Join(gitRoot, line), StorageDir: storageDir})
fullPath := path.Join(gitRoot, line)
if _, ok := seenPaths[fullPath]; !ok {
paths = append(paths, &foundConfig{Path: fullPath, StorageDir: storageDir})
seenPaths[fullPath] = true
}
}
}
}
}
logger.Debugf("Found %d configs in git dir.", len(paths))
return paths
}

Expand All @@ -255,6 +267,7 @@ func configPathsFromHistory() []*foundConfig {
logger.Debugf("ran into err unmarshaling seen configs %s", err)
return nil
}
logger.Debugf("Found %d configs in history.", len(foundConfigs))
return foundConfigs
}

Expand All @@ -267,6 +280,7 @@ func configsPathsFromPwd() []*foundConfig {
inDirConfig := path.Join(workDir, defaultFileName)
_, err = os.Stat(inDirConfig)
if err != nil {
logger.Debugf("no config found in current directory")
return nil
}
return []*foundConfig{{Path: inDirConfig, StorageDir: dotDir(workDir)}}
Expand Down Expand Up @@ -301,7 +315,7 @@ func LoadConfigs(prefetch bool, useHistory bool) []*foundConfig {
if err != nil {
logger.Debugf("unable to marshall history file with err %s", err)
}
os.MkdirAll(filepath.Dir(seenConfigsPath), 0600)
os.MkdirAll(filepath.Dir(seenConfigsPath), 0700)
err = ioutil.WriteFile(seenConfigsPath, data, 0600)
if err != nil {
logger.Debugf("Unable to save to history file at %s with err %s", seenConfigsPath, err)
Expand Down

0 comments on commit 937cf20

Please sign in to comment.