Skip to content

Commit 694792e

Browse files
committed
添加环境变量参数读取
1 parent 3c20460 commit 694792e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

main.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ var (
3535

3636
func init() {
3737
flag.IntVar(&timeout, "t", 120, "ssh连接超时时间(min)")
38-
flag.BoolVar(&savePass, "s", false, "保存ssh密码")
39-
envVal, ok := os.LookupEnv("savePass")
40-
if ok {
41-
b, err := strconv.ParseBool(envVal)
42-
if err != nil {
43-
savePass = false
44-
} else {
38+
flag.BoolVar(&savePass, "s", true, "保存ssh密码")
39+
if envVal, ok := os.LookupEnv("savePass"); ok {
40+
if b, err := strconv.ParseBool(envVal); err == nil {
4541
savePass = b
4642
}
4743
}
44+
if envVal, ok := os.LookupEnv("authInfo"); ok {
45+
*authInfo = envVal
46+
}
47+
if envVal, ok := os.LookupEnv("port"); ok {
48+
if b, err := strconv.Atoi(envVal); err == nil {
49+
*port = b
50+
}
51+
}
4852
flag.Parse()
4953
if *v {
5054
fmt.Printf("Version: %s\n\n", version)
@@ -56,7 +60,7 @@ func init() {
5660
if *authInfo != "" {
5761
accountInfo := strings.Split(*authInfo, ":")
5862
if len(accountInfo) != 2 || accountInfo[0] == "" || accountInfo[1] == "" {
59-
fmt.Println("请按'-a user:pass'的格式来传参, 且账号密码都不能为空!")
63+
fmt.Println("请按'user:pass'的格式来传参或设置环境变量, 且账号密码都不能为空!")
6064
os.Exit(0)
6165
}
6266
username, password = accountInfo[0], accountInfo[1]

0 commit comments

Comments
 (0)