"strings"
urlhelper "github.com/hashicorp/go-getter/helper/url"
+ "github.com/hashicorp/go-safetemp"
"github.com/hashicorp/go-version"
)
// GetFile for Git doesn't support updating at this time. It will download
// the file every time.
func (g *GitGetter) GetFile(dst string, u *url.URL) error {
- td, err := ioutil.TempDir("", "getter-git")
+ td, tdcloser, err := safetemp.Dir("", "getter")
if err != nil {
return err
}
- if err := os.RemoveAll(td); err != nil {
- return err
- }
+ defer tdcloser.Close()
// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
// setupGitEnv sets up the environment for the given command. This is used to
// pass configuration data to git and ssh and enables advanced cloning methods.
func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
- var sshOpts []string
+ const gitSSHCommand = "GIT_SSH_COMMAND="
+ var sshCmd []string
+
+ // If we have an existing GIT_SSH_COMMAND, we need to append our options.
+ // We will also remove our old entry to make sure the behavior is the same
+ // with versions of Go < 1.9.
+ env := os.Environ()
+ for i, v := range env {
+ if strings.HasPrefix(v, gitSSHCommand) {
+ sshCmd = []string{v}
+
+ env[i], env[len(env)-1] = env[len(env)-1], env[i]
+ env = env[:len(env)-1]
+ break
+ }
+ }
+
+ if len(sshCmd) == 0 {
+ sshCmd = []string{gitSSHCommand + "ssh"}
+ }
if sshKeyFile != "" {
// We have an SSH key temp file configured, tell ssh about this.
- sshOpts = append(sshOpts, "-i", sshKeyFile)
+ sshCmd = append(sshCmd, "-i", sshKeyFile)
}
- cmd.Env = append(os.Environ(),
- // Set the ssh command to use for clones.
- "GIT_SSH_COMMAND=ssh "+strings.Join(sshOpts, " "),
- )
+ env = append(env, strings.Join(sshCmd, " "))
+ cmd.Env = env
}
// checkGitVersion is used to check the version of git installed on the system