From 16b359bee2735aed43653c5e7db0f68c1b549ea2 Mon Sep 17 00:00:00 2001 From: windvalley Date: Mon, 14 Feb 2022 10:32:33 +0800 Subject: [PATCH] refactor: optimize subcommand 'config' --- configs/gossh.yaml | 7 ++++--- internal/cmd/config.go | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/configs/gossh.yaml b/configs/gossh.yaml index 0621c93..82a9eb9 100644 --- a/configs/gossh.yaml +++ b/configs/gossh.yaml @@ -1,7 +1,7 @@ auth: # Default login user. # Default: $USER - user: "xg" + user: "" # Default password of the login user. # Default: "" @@ -47,9 +47,10 @@ run: # Default: root as-user: root - # Export systems environment variables LANG/LC_ALL/LANGUAGE + # Export systems environment variables LANG/LC_ALL/LANGUAGE # as this value when executing command/script. - # Default: original variables values of the target hosts + # Available vaules: zh_CN.UTF-8, en_US.UTF-8, etc. + # Default: "" (null means do not export) lang: "" # Number of concurrent connections. diff --git a/internal/cmd/config.go b/internal/cmd/config.go index 442a8e0..2070c26 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -24,6 +24,7 @@ package cmd import ( "fmt" + "os" "github.com/spf13/cobra" @@ -80,10 +81,10 @@ run: # Default: root as-user: %s - # Export systems environment variables LANG/LC_ALL/LANGUAGE + # Export systems environment variables LANG/LC_ALL/LANGUAGE # as this value when executing command/script. # Available vaules: zh_CN.UTF-8, en_US.UTF-8, etc. - # Default: "" (do not export) + # Default: "" (null means do not export) lang: %q # Number of concurrent connections. @@ -167,9 +168,14 @@ and $PWD/.gossh.yaml has higher priority than $HOME/.gossh.yaml`, Run: func(cmd *cobra.Command, args []string) { config := configflags.Config + user := config.Auth.User + if user == os.Getenv("USER") { + user = "" + } + fmt.Printf( configTemplate, - config.Auth.User, config.Auth.Password, config.Auth.AskPass, + user, config.Auth.Password, config.Auth.AskPass, config.Auth.PassFile, config.Auth.Passphrase, config.Auth.VaultPassFile, config.Hosts.Inventory, config.Hosts.Port, config.Run.Sudo, config.Run.AsUser, config.Run.Lang, config.Run.Concurrency,