diff --git a/cmd/init.go b/cmd/init.go index 102ff900..8b463298 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -17,7 +17,9 @@ package cmd import ( "fmt" + "io/ioutil" "os" + "path" "runtime" "strings" @@ -79,6 +81,25 @@ func initEnvironment(cmd *cobra.Command, args []string) error { fmt.Printf("\n%s\n", globals.DashLine) if common.DirExists(sandboxBinary) { fmt.Printf("Directory %s ($SANDBOX_BINARY) already exists\n", sandboxBinary) + files, err := ioutil.ReadDir(sandboxBinary) + if err != nil { + return fmt.Errorf("error reading sandbox binary directory %s: %s", sandboxBinary, err) + } + // Sandbox binary directory exists. + // We now check whether there is any expanded tarball directory + numSandboxes := 0 + for _, f := range files { + if f.IsDir() { + bin := path.Join(sandboxBinary, f.Name(), "bin") + if common.DirExists(bin) { + numSandboxes++ + } + } + } + if numSandboxes == 0 { + needDownload = true + } + } else { if !dryRun { diff --git a/common/strutils.go b/common/strutils.go index 9d45144f..dd10cb1d 100644 --- a/common/strutils.go +++ b/common/strutils.go @@ -64,7 +64,19 @@ func ReplaceLiteralHome(path string) string { // for example, if "$HOME" resolves to "/home/goofy" the string "/home/goofy/some/path" would become "$HOME/some/path" func ReplaceLiteralEnvVar(name string, envVar string) string { value := os.Getenv(envVar) - re := regexp.MustCompile(value) + // If the environment variable is empty, we return the initial name + if value == "" { + return name + } + // If the current location is at the top of the directory tree, we don't want to do any replacement + if value == "/" { + return name + } + // If there is already a variable in the name, no further replacement is needed + if strings.Contains(name, "$") { + return name + } + re := regexp.MustCompile(`^` + value) return re.ReplaceAllString(name, "$$"+envVar) } @@ -72,6 +84,9 @@ func ReplaceLiteralEnvVar(name string, envVar string) string { // for example, if "$HOME" resolves to "/home/goofy" the string "$HOME/some/path" would become "/home/goofy/some/path" func ReplaceEnvVar(name string, envVar string) string { value := os.Getenv(envVar) + if value == "" || value == "/" { + return name + } re := regexp.MustCompile(`\$` + envVar + `\b`) return re.ReplaceAllString(name, value) } diff --git a/common/strutils_test.go b/common/strutils_test.go index a6e80adb..c8f49716 100644 --- a/common/strutils_test.go +++ b/common/strutils_test.go @@ -25,6 +25,8 @@ import ( ) type pathInfo struct { + home string + pwd string value string envVar string expected string @@ -33,16 +35,21 @@ type pathInfo struct { func TestReplaceLiteralHome(t *testing.T) { saveHome := os.Getenv("HOME") savePWD := os.Getenv("PWD") - os.Setenv("HOME", "/home/Groucho") - os.Setenv("PWD", "/var/lib/MarxBrothers") + groucho := "/home/Groucho" + brothers := "/var/lib/MarxBrothers" var paths = []pathInfo{ - {"/home/Groucho/", "HOME", "$HOME/"}, - {"/home/Groucho/path1/path2", "HOME", "$HOME/path1/path2"}, - {"/home/Harpo/path1/path2", "HOME", "/home/Harpo/path1/path2"}, - {"/var/lib/MarxBrothers/path1/path2", "PWD", "$PWD/path1/path2"}, - {"/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"}, + {groucho, brothers, "/home/Groucho/", "HOME", "$HOME/"}, + {groucho, brothers, "/home/Groucho/path1/path2", "HOME", "$HOME/path1/path2"}, + {groucho, brothers, "/home/Harpo/path1/path2", "HOME", "/home/Harpo/path1/path2"}, + {groucho, brothers, "/var/lib/MarxBrothers/path1/path2", "PWD", "$PWD/path1/path2"}, + {groucho, brothers, "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"}, + {groucho, "/", "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"}, + {groucho, "", "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"}, + {groucho, brothers, "$PWD/home/Groucho/path2", "HOME", "$PWD/home/Groucho/path2"}, } for _, p := range paths { + os.Setenv("HOME", p.home) + os.Setenv("PWD", p.pwd) value := p.value envVar := p.envVar expected := p.expected @@ -50,11 +57,13 @@ func TestReplaceLiteralHome(t *testing.T) { if expected == canary { t.Logf("ok %-35s %-10s =--> %-25s\n", value, "("+envVar+")", expected) } else { - t.Logf("NOT OK %-35s %-10s =--> %-25s\n", value, "("+envVar+")", expected) + t.Logf("NOT OK - got %-35s %-10s =--> want %-25s\n", value, "("+envVar+")", expected) t.Fail() } } for _, p := range paths { + os.Setenv("HOME", p.home) + os.Setenv("PWD", p.pwd) value := p.expected envVar := p.envVar expected := p.value @@ -62,7 +71,7 @@ func TestReplaceLiteralHome(t *testing.T) { if expected == canary { t.Logf("ok %-35s %-10s --=> %-25s\n", value, "("+envVar+")", expected) } else { - t.Logf("NOT OK %-35s %-10s --=> %-25s\n", value, "("+envVar+")", expected) + t.Logf("NOT OK - got %-35s %-10s --=> want %-25s\n", value, "("+envVar+")", expected) t.Fail() } } diff --git a/common/version.go b/common/version.go index b25c6e8d..524c5d23 100644 --- a/common/version.go +++ b/common/version.go @@ -16,11 +16,11 @@ package common // This file was generated during build. Do not edit. -// Build time: 2020-07-26 19:40 +// Build time: 2020-08-07 10:52 -var VersionDef string = "1.53.1" // 2020-07-26 +var VersionDef string = "1.53.2" // 2020-08-07 // Compatible version is the version used to mark compatible archives (templates, configuration). // It is usually major.minor.0, except when we are at version 0.x, when // every revision may bring incompatibility -var CompatibleVersion string = "1.53.0" // 2020-07-26 +var CompatibleVersion string = "1.53.0" // 2020-08-07