Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
36 changes: 36 additions & 0 deletions e2e/15_set_ssh_config_path.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Test that we can set ssh_config path and it will survive
# application run in normal mode and overriding state file.

Set Shell bash

# Create ssh_config
Type "touch /tmp/delete_me_goto_ssh_config_test_file"
Enter
Sleep 100ms
Type "gg -f temp --set-ssh-config-path /tmp/delete_me_goto_ssh_config_test_file"
Enter
Sleep 100ms

# Run the app
Type "gg -f temp -l debug"
Enter
Sleep 100ms

# Create a random host
Type "n"
Sleep 100ms
Type "network host"
Down
Ctrl+u
Type "127.0.0.1"
Down
Type "network host description"
# Place the host to "manual hosts" group
Down
Type "manual hosts"
Ctrl+S
# Should always give some time when save a host
Sleep 100ms

Type "q"
Sleep 100ms
5 changes: 5 additions & 0 deletions e2e/expected/15_set_ssh_config_path_hosts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- host:
address: 127.0.0.1
description: network host description
group: manual hosts
title: network host
5 changes: 5 additions & 0 deletions e2e/expected/15_set_ssh_config_path_state.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
screen_layout: description
ssh_config_path: /tmp/delete_me_goto_ssh_config_test_file
selected: 1
enable_ssh_config: true
theme: default
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
151 changes: 83 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,130 @@ 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,
}, {
name: "Set ssh config path",
args: []string{"--set-ssh-config-path", "/tmp/custom_config2"},
wantConfig: &Configuration{
AppHome: "/tmp/home",
AppMode: "HANDLE_PARAM",
DisableFeature: "",
EnableFeature: "",
LogLevel: "info",
SSHConfigPath: "/tmp/custom_config", // this comes from env config, see above
SetSSHConfigPath: "/tmp/custom_config2",
SetTheme: "",
},
wantError: false,
},
Expand All @@ -207,7 +221,8 @@ 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.SetSSHConfigPath, cfg.SetSSHConfigPath)
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
Loading
Loading