Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ const (

// Configuration structs contains user-definable parameters.
type Configuration struct {
AppMode constant.AppMode
AppName string
DisableFeature FeatureFlag
EnableFeature FeatureFlag
SetTheme string
AppHome string `env:"GG_HOME"`
LogLevel constant.LogLevel `env:"GG_LOG_LEVEL" envDefault:"info"`
SSHConfigFilePath string `env:"GG_SSH_CONFIG_FILE_PATH"`
AppMode constant.AppMode
AppName string
DisableFeature FeatureFlag
EnableFeature FeatureFlag
SetTheme string
AppHome string `env:"GG_HOME"`
LogLevel constant.LogLevel `env:"GG_LOG_LEVEL" envDefault:"info"`
SSHConfigPath string `env:"GG_SSH_CONFIG_FILE_PATH"`
// SetSSHConfigPath is not the same as SSHConfigPath, as when this is set, we must
// write the value to state file and exit. When SSHConfigPath is set, we just use it
// as the path to ssh config within the current application run.
SetSSHConfigPath string
}

func Initialize() (*Configuration, error) {
Expand Down Expand Up @@ -81,9 +85,9 @@ func parseCommandLineFlags(envConfig *Configuration, args []string, exitOnError
fs.StringVar(&cmdConfig.AppHome, "f", envConfig.AppHome, "Application home folder")
fs.StringVar(&cmdConfig.LogLevel, "l", envConfig.LogLevel, "Log verbosity level: debug, info")
fs.StringVar(
&cmdConfig.SSHConfigFilePath,
&cmdConfig.SSHConfigPath,
"s",
envConfig.SSHConfigFilePath,
envConfig.SSHConfigPath,
"Specifies an alternative per-user SSH configuration file path",
)
fs.Var(
Expand All @@ -97,6 +101,7 @@ func parseCommandLineFlags(envConfig *Configuration, args []string, exitOnError
fmt.Sprintf("Disable feature. Supported values: %s", strings.Join(SupportedFeatures, "|")),
)
fs.StringVar(&cmdConfig.SetTheme, "set-theme", "", "Set application theme")
fs.StringVar(&cmdConfig.SetSSHConfigPath, "set-ssh-config-path", "", "Set SSH configuration file path or URL.")

err := fs.Parse(args[1:]) // args should not include program name, see docs
if err != nil {
Expand All @@ -115,6 +120,9 @@ func parseCommandLineFlags(envConfig *Configuration, args []string, exitOnError
case cmdConfig.SetTheme != "":
fmt.Printf("[CONFIG] Set theme to %q\n", cmdConfig.SetTheme)
cmdConfig.AppMode = constant.AppModeType.HandleParam
case cmdConfig.SetSSHConfigPath != "":
fmt.Printf("[CONFIG] Set SSH config file path to %q\n", cmdConfig.SetSSHConfigPath)
cmdConfig.AppMode = constant.AppModeType.HandleParam
}

return &cmdConfig, nil
Expand Down
136 changes: 68 additions & 68 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_parseEnvironmentConfig(t *testing.T) {
require.Empty(t, envConfig.DisableFeature)
require.Empty(t, envConfig.EnableFeature)
require.Empty(t, envConfig.SetTheme)
require.Empty(t, envConfig.SSHConfigFilePath)
require.Empty(t, envConfig.SSHConfigPath)
require.Equal(t, "info", envConfig.LogLevel)
})

Expand All @@ -52,16 +52,16 @@ func Test_parseEnvironmentConfig(t *testing.T) {
require.Empty(t, envConfig.DisableFeature)
require.Empty(t, envConfig.EnableFeature)
require.Empty(t, envConfig.SetTheme)
require.Equal(t, "/tmp/custom_config", envConfig.SSHConfigFilePath)
require.Equal(t, "/tmp/custom_config", envConfig.SSHConfigPath)
require.Equal(t, "debug", envConfig.LogLevel)
})
}

func Test_parseCommandLineFlags(t *testing.T) {
envConfig := &Configuration{
AppHome: "/tmp/home",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
AppHome: "/tmp/home",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
}

tests := []struct {
Expand All @@ -74,116 +74,116 @@ func Test_parseCommandLineFlags(t *testing.T) {
name: "No command line flags",
args: []string{},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
},
wantError: false,
}, {
name: "Display help",
args: []string{"-h"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: true, // returns flag.ErrHelp, read the os.flag documentation for details
}, {
name: "Display version",
args: []string{"-v"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "DISPLAY_INFO",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "DISPLAY_INFO",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: false,
}, {
name: "Set home app folder",
args: []string{"-f", "/tmp/home2"},
wantConfig: &Configuration{
AppHome: "/tmp/home2",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home2",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: false,
}, {
name: "Set log level",
args: []string{"-l", "debug"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "debug",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "debug",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: false,
}, {
name: "Set SSH config file path",
args: []string{"-s", "/tmp/custom_config2"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config2",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config2",
SetTheme: "",
},
wantError: false,
}, {
name: "Set theme",
args: []string{"--set-theme", "dark"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "dark",
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "dark",
},
wantError: false,
}, {
name: "Enable feature",
args: []string{"-e", "ssh_config"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "",
EnableFeature: "ssh_config",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "",
EnableFeature: "ssh_config",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: false,
}, {
name: "Disable feature",
args: []string{"-d", "ssh_config"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "ssh_config",
EnableFeature: "",
LogLevel: "info",
SSHConfigFilePath: "/tmp/custom_config",
SetTheme: "",
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "ssh_config",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config",
SetTheme: "",
},
wantError: false,
},
Expand All @@ -207,7 +207,7 @@ func Test_parseCommandLineFlags(t *testing.T) {
require.Equal(t, tt.wantConfig.DisableFeature, cfg.DisableFeature)
require.Equal(t, tt.wantConfig.EnableFeature, cfg.EnableFeature)
require.Equal(t, tt.wantConfig.LogLevel, cfg.LogLevel)
require.Equal(t, tt.wantConfig.SSHConfigFilePath, cfg.SSHConfigFilePath)
require.Equal(t, tt.wantConfig.SSHConfigPath, cfg.SSHConfigPath)
require.Equal(t, tt.wantConfig.SetTheme, cfg.SetTheme)
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/model/sshcommand/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ConnectCommand(options ...Option) string {
}

if sshconfig.IsUserDefinedPath() {
addOption(&sb, OptionConfigFilePath{Value: sshconfig.GetFilePath()})
addOption(&sb, OptionConfigFilePath{Value: sshconfig.Path()})
}

return sb.String()
Expand All @@ -34,7 +34,7 @@ func LoadConfigCommand(options ...Option) string {
}

if sshconfig.IsUserDefinedPath() {
addOption(&sb, OptionConfigFilePath{Value: sshconfig.GetFilePath()})
addOption(&sb, OptionConfigFilePath{Value: sshconfig.Path()})
}

return sb.String()
Expand Down
4 changes: 2 additions & 2 deletions internal/model/sshcommand/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Test_ConnectCommand(t *testing.T) {

// Check that the command uses custom SSH config file path if defined
state.Initialize(context.TODO(),
&config.Configuration{SSHConfigFilePath: "~/.ssh/custom_config"},
&config.Configuration{SSHConfigPath: "~/.ssh/custom_config"},
&mocklogger.Logger{})
actual := ConnectCommand(OptionAddress{Value: "example.com"})
require.Contains(t, actual, `ssh example.com -F "~/.ssh/custom_config"`)
Expand Down Expand Up @@ -161,7 +161,7 @@ func Test_LoadConfigCommand(t *testing.T) {

// Repeat the first test with a custom SSH config file path
mockLogger := mocklogger.Logger{}
state.Initialize(context.TODO(), &config.Configuration{SSHConfigFilePath: "~/.ssh/custom_config"}, &mockLogger)
state.Initialize(context.TODO(), &config.Configuration{SSHConfigPath: "~/.ssh/custom_config"}, &mockLogger)
actual := LoadConfigCommand(tests[0].option)
// Should use contains because on Windows version the command starts from 'cmd /c ...'
require.Contains(t, actual, `ssh -G example.com -F "~/.ssh/custom_config"`)
Expand Down
18 changes: 15 additions & 3 deletions internal/model/sshconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,19 @@ func IsUserDefinedPath() bool {
return state.Get().IsUserDefinedSSHConfigPath
}

// GetFilePath - returns SSH config file path which is defined in application configuration.
func GetFilePath() string {
return state.Get().SSHConfigFilePath
var sshConfigPath *string

// SetPath - set SSH config file path.
func SetPath(path string) {
sshConfigPath = &path
}

// Path - returns SSH config file path which is defined in application configuration.
func Path() string {
if sshConfigPath != nil {
return *sshConfigPath
}

// Fallback to application state.
return state.Get().SSHConfigPath
}
Loading
Loading