summaryrefslogtreecommitdiff
path: root/internal/cli/authkeyflags.go
blob: 4c945d325a1bdf14d87baf658d04b4650c7d6681 (plain)
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
package cli

import (
	"flag"

	"github.com/mimecast/dtail/internal/config"
)

const authKeyPathHelpText = "Path to auth key/private key (defaults to ~/.ssh/id_rsa via config)"

// BindAuthKeyFlags registers the legacy and current auth-key flags.
func BindAuthKeyFlags(fs *flag.FlagSet, legacyKey *string, args *config.Args) {
	fs.StringVar(legacyKey, "key", "", "Deprecated alias for -auth-key-path")
	fs.StringVar(&args.SSHPrivateKeyFilePath, "auth-key-path", "", authKeyPathHelpText)
}

// FlagWasSet reports whether the named flag was explicitly set on the command line.
func FlagWasSet(name string) bool {
	var wasSet bool
	flag.Visit(func(f *flag.Flag) {
		if f.Name == name {
			wasSet = true
		}
	})
	return wasSet
}

// ApplyAuthKeyPathCompatibility copies the deprecated legacy key into args when
// the new flag was not explicitly set. If both were set, the new flag wins and
// a warning message is returned.
func ApplyAuthKeyPathCompatibility(args *config.Args, legacyKey string, authKeyPathSet bool) string {
	if authKeyPathSet {
		if legacyKey != "" {
			return "WARN: -key is deprecated; ignoring it because -auth-key-path was also set"
		}
		return ""
	}

	if legacyKey != "" {
		args.SSHPrivateKeyFilePath = legacyKey
	}
	return ""
}