forked from mutagen-io/mutagen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
54 lines (47 loc) · 1.79 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ssh
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
)
const (
// PrompterEnvironmentVariable is the environment variable in which the
// Mutagen prompter identifier is stored.
PrompterEnvironmentVariable = "MUTAGEN_SSH_PROMPTER"
)
// setPrompterVariables sets up environment variables for prompting based on the
// provided prompter identifier. If an empty identifier is provided, then any
// potentially conflicting environment variables (that might cause alternative
// prompting) are removed.
func setPrompterVariables(environment []string, prompter string) ([]string, error) {
// Handle based on whether or not there's a prompter.
if prompter == "" {
// If there is no prompter, then enforce that SSH_ASKPASS is not set,
// because some systems (e.g. systems with a Cygwin SSH binary) will
// include default SSH_ASKPASS values that throw up GUIs without any
// message or context and we don't want that.
filteredEnvironment := environment[:0]
for _, e := range environment {
if !strings.HasPrefix(e, "SSH_ASKPASS=") {
filteredEnvironment = append(filteredEnvironment, e)
}
}
environment = filteredEnvironment
} else {
// Compute the path to the current (mutagen) executable and set it in
// the SSH_ASKPASS variable.
if mutagenPath, err := os.Executable(); err != nil {
return nil, errors.Wrap(err, "unable to determine executable path")
} else {
environment = append(environment, fmt.Sprintf("SSH_ASKPASS=%s", mutagenPath))
}
// Set the DISPLAY variable to Mutagen.
environment = append(environment, "DISPLAY=mutagen")
// Add environment variables to make Mutagen recognize an SSH prompting
// invocation.
environment = append(environment, fmt.Sprintf("%s=%s", PrompterEnvironmentVariable, prompter))
}
// Done.
return environment, nil
}