From ce686d09106fac7444b82feac4f03689a6a12c2a Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 24 May 2023 10:13:47 +0200 Subject: [PATCH 1/8] fix: setup cmd check supported OSX versions Partially address https://github.com/cristianoliveira/ergo/issues/123 --- commands/setup/osx_configurator.go | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index 9dc66be..043b69b 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -1,14 +1,73 @@ package setup +import ( + "fmt" + "os/exec" + "strconv" + "strings" +) + // OSXConfigurator implements Configurator for windows type OSXConfigurator struct{} +const SUPPORTED_OSX_VERSION = 10 // up to Catalina + +func checkSupportedVersion() (bool, error) { + cmd := `sw_vers -productVersion` + output, err := exec.Command("/bin/sh", "-c", cmd).Output() + if err != nil { + return false, err + } + + var majorVersionNumber int + outputString := string(output) + if strings.Contains(outputString, ".") { + majorVersion := strings.Split(string(output), ".")[0] + majorVersionNumber, err = strconv.Atoi(majorVersion) + } else { + majorVersionNumber, err = strconv.Atoi(outputString) + } + + if err != nil { + return false, err + } + + if majorVersionNumber >= SUPPORTED_OSX_VERSION { + fmt.Println("The ergo setup is not supported for the current osx version.") + return false, nil + } + + return true, nil +} + // SetUp is responsible for setting up the ergo as proxy func (c *OSXConfigurator) SetUp(proxyURL string) error { + isSupported, err := checkSupportedVersion() + if err != nil { + fmt.Println("Error while checking the current osx version: ", err) + return err + } + + if !isSupported { + fmt.Println("The ergo setup is not supported for the current osx version.") + return nil + } + return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`) } // SetDown is responsible for remove the ergo as proxy func (c *OSXConfigurator) SetDown() error { + isSupported, err := checkSupportedVersion() + if err != nil { + fmt.Println("Error while checking the current osx version: ", err) + return err + } + + if !isSupported { + fmt.Println("The ergo setup is not supported for the current osx version.") + return nil + } + return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`) } From 8e71989bf612077ffb5858502e3b0af4870bef9a Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 24 May 2023 10:18:39 +0200 Subject: [PATCH 2/8] Adds instructions to the error message --- commands/setup/osx_configurator.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index 043b69b..c715264 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -21,6 +21,10 @@ func checkSupportedVersion() (bool, error) { var majorVersionNumber int outputString := string(output) + if outputString == "" { + return false, fmt.Errorf("Checking the current osx version failed.") + } + if strings.Contains(outputString, ".") { majorVersion := strings.Split(string(output), ".")[0] majorVersionNumber, err = strconv.Atoi(majorVersion) @@ -34,6 +38,8 @@ func checkSupportedVersion() (bool, error) { if majorVersionNumber >= SUPPORTED_OSX_VERSION { fmt.Println("The ergo setup is not supported for the current osx version.") + fmt.Println("Supported versions Catalina or below.") + fmt.Println("Please, consider to setting up ergo as proxy manually.") return false, nil } From e5e6fc6cb018eaab90dea339e83c7ff0e6348eb4 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 24 May 2023 10:25:23 +0200 Subject: [PATCH 3/8] better error messages --- commands/setup/osx_configurator.go | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index c715264..2a3908e 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -12,17 +12,17 @@ type OSXConfigurator struct{} const SUPPORTED_OSX_VERSION = 10 // up to Catalina -func checkSupportedVersion() (bool, error) { +func checkSupportedVersion() error { cmd := `sw_vers -productVersion` output, err := exec.Command("/bin/sh", "-c", cmd).Output() if err != nil { - return false, err + return err } var majorVersionNumber int outputString := string(output) if outputString == "" { - return false, fmt.Errorf("Checking the current osx version failed.") + return fmt.Errorf("checking the current osx version failed") } if strings.Contains(outputString, ".") { @@ -33,47 +33,35 @@ func checkSupportedVersion() (bool, error) { } if err != nil { - return false, err + return err } if majorVersionNumber >= SUPPORTED_OSX_VERSION { fmt.Println("The ergo setup is not supported for the current osx version.") fmt.Println("Supported versions Catalina or below.") - fmt.Println("Please, consider to setting up ergo as proxy manually.") - return false, nil + fmt.Println("Please, consider setting up ergo as proxy manually.") + return fmt.Errorf("unsupported osx version") } - return true, nil + return nil } // SetUp is responsible for setting up the ergo as proxy func (c *OSXConfigurator) SetUp(proxyURL string) error { - isSupported, err := checkSupportedVersion() + err := checkSupportedVersion() if err != nil { - fmt.Println("Error while checking the current osx version: ", err) return err } - if !isSupported { - fmt.Println("The ergo setup is not supported for the current osx version.") - return nil - } - return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`) } // SetDown is responsible for remove the ergo as proxy func (c *OSXConfigurator) SetDown() error { - isSupported, err := checkSupportedVersion() + err := checkSupportedVersion() if err != nil { - fmt.Println("Error while checking the current osx version: ", err) return err } - if !isSupported { - fmt.Println("The ergo setup is not supported for the current osx version.") - return nil - } - return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`) } From d955580fccd790bbe524ab8dd18a7135cd1b2189 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 24 May 2023 10:27:32 +0200 Subject: [PATCH 4/8] print error and show standard messagegp --- commands/setup/osx_configurator.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index 2a3908e..317b972 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -16,7 +16,8 @@ func checkSupportedVersion() error { cmd := `sw_vers -productVersion` output, err := exec.Command("/bin/sh", "-c", cmd).Output() if err != nil { - return err + fmt.Println(err) + return fmt.Errorf("checking the current osx version failed") } var majorVersionNumber int @@ -33,7 +34,8 @@ func checkSupportedVersion() error { } if err != nil { - return err + fmt.Println(err) + return fmt.Errorf("checking the current osx version failed") } if majorVersionNumber >= SUPPORTED_OSX_VERSION { From ff18f38e7d85442e4f17bd380c5710da9a1b09f9 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 24 May 2023 10:28:11 +0200 Subject: [PATCH 5/8] allow catalina --- commands/setup/osx_configurator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index 317b972..a6a3002 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -38,7 +38,7 @@ func checkSupportedVersion() error { return fmt.Errorf("checking the current osx version failed") } - if majorVersionNumber >= SUPPORTED_OSX_VERSION { + if majorVersionNumber > SUPPORTED_OSX_VERSION { fmt.Println("The ergo setup is not supported for the current osx version.") fmt.Println("Supported versions Catalina or below.") fmt.Println("Please, consider setting up ergo as proxy manually.") From 2bbc74575e4ef5625b50e7ab3f6f30e8ab4127b4 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Sat, 3 Jun 2023 14:38:20 +0200 Subject: [PATCH 6/8] test: adapt tests --- commands/setup/linux_configurator.go | 10 ++-- commands/setup/osx_configurator.go | 9 ++-- commands/setup/runner.go | 6 +-- commands/setup/windows_configurator.go | 4 +- commands/setup_test.go | 73 +++++++++++++++++++++----- main_test.go | 37 ++++++++++++- 6 files changed, 112 insertions(+), 27 deletions(-) diff --git a/commands/setup/linux_configurator.go b/commands/setup/linux_configurator.go index 362b95f..c6da0fb 100644 --- a/commands/setup/linux_configurator.go +++ b/commands/setup/linux_configurator.go @@ -5,22 +5,24 @@ type LinuxConfigurator struct{} // SetUp is responsible for setting up the ergo as proxy func (c *LinuxConfigurator) SetUp(proxyURL string) error { - err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'auto'") + _, err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'auto'") if err != nil { return err } - return RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", `'`+proxyURL+`'`) + _, err = RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", `'`+proxyURL+`'`) + return err } // SetDown is responsible for remove the ergo as proxy func (c *LinuxConfigurator) SetDown() error { - err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'none'") + _, err := RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "mode", "'none'") if err != nil { return err } - return RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", "''") + _, err = RunnerDefault.Run("gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", "''") + return err } diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index a6a3002..2fce0a6 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -2,7 +2,6 @@ package setup import ( "fmt" - "os/exec" "strconv" "strings" ) @@ -14,7 +13,7 @@ const SUPPORTED_OSX_VERSION = 10 // up to Catalina func checkSupportedVersion() error { cmd := `sw_vers -productVersion` - output, err := exec.Command("/bin/sh", "-c", cmd).Output() + output, err := RunnerDefault.Run("/bin/sh", "-c", cmd) if err != nil { fmt.Println(err) return fmt.Errorf("checking the current osx version failed") @@ -55,7 +54,8 @@ func (c *OSXConfigurator) SetUp(proxyURL string) error { return err } - return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`) + _, err = RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" "`+proxyURL+`"`) + return err } // SetDown is responsible for remove the ergo as proxy @@ -65,5 +65,6 @@ func (c *OSXConfigurator) SetDown() error { return err } - return RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`) + _, err = RunnerDefault.Run(`networksetup`, `-setautoproxyurl "Wi-Fi" ""`) + return err } diff --git a/commands/setup/runner.go b/commands/setup/runner.go index 6c65de3..51c945d 100644 --- a/commands/setup/runner.go +++ b/commands/setup/runner.go @@ -7,19 +7,19 @@ import ( // Runner is a process runner type Runner interface { - Run(string, ...string) error + Run(string, ...string) ([]byte, error) } // DefaultRunner implements the default runner for ergo type DefaultRunner struct{} // Run a given command script -func (r DefaultRunner) Run(command string, args ...string) error { +func (r DefaultRunner) Run(command string, args ...string) ([]byte, error) { out, err := exec.Command(command, args...).Output() if len(out) != 0 { fmt.Printf("Command: %s\n", out) } - return err + return out, err } // RunnerDefault run given commands diff --git a/commands/setup/windows_configurator.go b/commands/setup/windows_configurator.go index 7ad6108..5353597 100644 --- a/commands/setup/windows_configurator.go +++ b/commands/setup/windows_configurator.go @@ -5,7 +5,7 @@ type WindowsConfigurator struct{} // SetUp is responsible for setting up the ergo as proxy func (c *WindowsConfigurator) SetUp(proxyURL string) error { - err := RunnerDefault.Run( + _, err := RunnerDefault.Run( `reg`, ` add HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings /v AutoConfigURL /t REG_SZ /d `+proxyURL+` /f`) InetRefresh() @@ -15,7 +15,7 @@ func (c *WindowsConfigurator) SetUp(proxyURL string) error { // SetDown is responsible for remove the ergo as proxy func (c *WindowsConfigurator) SetDown() error { - err := RunnerDefault.Run( + _, err := RunnerDefault.Run( `reg`, ` delete HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings /v AutoConfigURL /f`) InetRefresh() diff --git a/commands/setup_test.go b/commands/setup_test.go index edb7f2e..b09866e 100644 --- a/commands/setup_test.go +++ b/commands/setup_test.go @@ -29,10 +29,10 @@ type TestRunner struct { History string } -func (r *TestRunner) Run(command string, args ...string) error { +func (r *TestRunner) Run(command string, args ...string) ([]byte, error) { if r.ExpectToInclude == "" { fmt.Println("No expectation") - return nil + return []byte{}, nil } r.History = r.History + " > " + command + " " + strings.Join(args, " ") @@ -44,7 +44,31 @@ func (r *TestRunner) Run(command string, args ...string) error { ) } - return nil + return []byte{}, nil +} + +type TestRunnerWithOutput struct { + Test *testing.T + History string + MockedOutput map[string][]byte +} + +func (r *TestRunnerWithOutput) Mock(command string, output []byte) { + // parse command to a unique key + key := strings.Join(strings.Split(command, " "), "_") + r.MockedOutput[key] = output +} + +func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) { + command_with_args := command + " " + strings.Join(args, " ") + key := strings.Join(strings.Split(command_with_args, " "), "_") + mockedOutput, ok := r.MockedOutput[key] + if !ok { + r.Test.Fatalf("No more expectation for command %s", command_with_args) + return []byte{}, nil + } + + return mockedOutput, nil } func TestSetupLinuxGnome(t *testing.T) { @@ -99,7 +123,6 @@ func TestSetupLinuxGnome(t *testing.T) { } testRunner := &TestRunner{} - for _, c := range cases { t.Run(c.Title, func(tt *testing.T) { testRunner.Test = tt @@ -128,19 +151,24 @@ func TestSetupOSX(t *testing.T) { }{ { Title: "expect to set networking proxy pac url", - CommandExpectToInclude: `-setautoproxyurl "Wi-Fi" "` + config.GetProxyPacURL() + `"`, + CommandExpectToInclude: `networksetup -setautoproxyurl "Wi-Fi" "` + config.GetProxyPacURL() + `"`, }, } for _, c := range cases { t.Run(c.Title, func(tt *testing.T) { - setup.RunnerDefault = &TestRunner{ - Test: tt, - ExpectToInclude: c.CommandExpectToInclude, + mockedRunner := &TestRunnerWithOutput{ + Test: tt, + MockedOutput: map[string][]byte{}, } + mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6")) + mockedRunner.Mock(c.CommandExpectToInclude, []byte{}) + + setup.RunnerDefault = mockedRunner command := SetupCommand{System: "osx", Remove: false} _, err := command.Execute(config) + if err != nil { t.Fatalf(err.Error()) } @@ -155,17 +183,21 @@ func TestSetupOSX(t *testing.T) { }{ { Title: "expect to set networking wi-fi to none", - CommandExpectToInclude: `-setautoproxyurl "Wi-Fi" ""`, + CommandExpectToInclude: `networksetup -setautoproxyurl "Wi-Fi" ""`, }, } for _, c := range cases { t.Run(c.Title, func(tt *testing.T) { - setup.RunnerDefault = &TestRunner{ - Test: tt, - ExpectToInclude: c.CommandExpectToInclude, + mockedRunner := &TestRunnerWithOutput{ + Test: tt, + MockedOutput: map[string][]byte{}, } + mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6")) + mockedRunner.Mock(c.CommandExpectToInclude, []byte{}) + setup.RunnerDefault = mockedRunner + command := SetupCommand{System: "osx", Remove: true} _, err := command.Execute(config) if err != nil { @@ -174,6 +206,23 @@ func TestSetupOSX(t *testing.T) { }) } }) + + t.Run("it does not work after Catalina version * > 10", func(tt *testing.T) { + mockedRunner := &TestRunnerWithOutput{ + Test: tt, + MockedOutput: map[string][]byte{}, + } + + mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("11.11.6")) + setup.RunnerDefault = mockedRunner + + command := SetupCommand{System: "osx", Remove: true} + _, err := command.Execute(config) + // check if the error message contains Setup failed + if !strings.Contains(err.Error(), "Setup failed cause unsupported osx version") { + t.Fatalf(err.Error()) + } + }) } func TestSetupWindows(t *testing.T) { diff --git a/main_test.go b/main_test.go index cf93d5f..be35379 100644 --- a/main_test.go +++ b/main_test.go @@ -92,8 +92,8 @@ func TestShowUsage(t *testing.T) { type TestRunner struct{} -func (r *TestRunner) Run(command string, args ...string) error { - return nil +func (r *TestRunner) Run(command string, args ...string) ([]byte, error) { + return []byte(""), nil } func TestListCommand(t *testing.T) { @@ -152,6 +152,31 @@ func TestListNamesCommand(t *testing.T) { }) } +type TestRunnerWithOutput struct { + OriginalRunner setup.Runner + Test *testing.T + History string + MockedOutput map[string][]byte +} + +func (r *TestRunnerWithOutput) Mock(command string, output []byte) { + // parse command to a unique key + key := strings.Join(strings.Split(command, " "), "_") + r.MockedOutput[key] = output +} + +func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) { + command_with_args := command + " " + strings.Join(args, " ") + key := strings.Join(strings.Split(command_with_args, " "), "_") + + mockedOutput, ok := r.MockedOutput[key] + if !ok { + return r.OriginalRunner.Run(command, args...) + } + + return mockedOutput, nil +} + func TestSetupCommand(t *testing.T) { setup.RunnerDefault = &TestRunner{} @@ -200,6 +225,14 @@ func TestSetupCommand(t *testing.T) { t.Fatal("Expected result to not be nil") } + mockedRunner := &TestRunnerWithOutput{ + OriginalRunner: setup.RunnerDefault, + Test: tt, + MockedOutput: map[string][]byte{}, + } + mockedRunner.Mock("/bin/sh -c sw_vers -productVersion", []byte("10.11.6")) + setup.RunnerDefault = mockedRunner + setup := command.(commands.SetupCommand) output, err := setup.Execute(config) From 581dc8aec2894b39688364598ae1abea01e82788 Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Sat, 3 Jun 2023 14:55:38 +0200 Subject: [PATCH 7/8] fix linter complaints --- commands/setup/osx_configurator.go | 4 ++-- commands/setup_test.go | 6 +++--- main_test.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/commands/setup/osx_configurator.go b/commands/setup/osx_configurator.go index 2fce0a6..b3ea054 100644 --- a/commands/setup/osx_configurator.go +++ b/commands/setup/osx_configurator.go @@ -9,7 +9,7 @@ import ( // OSXConfigurator implements Configurator for windows type OSXConfigurator struct{} -const SUPPORTED_OSX_VERSION = 10 // up to Catalina +const osxVersionThatSupportsSetupCommand = 10 // up to Catalina func checkSupportedVersion() error { cmd := `sw_vers -productVersion` @@ -37,7 +37,7 @@ func checkSupportedVersion() error { return fmt.Errorf("checking the current osx version failed") } - if majorVersionNumber > SUPPORTED_OSX_VERSION { + if majorVersionNumber > osxVersionThatSupportsSetupCommand { fmt.Println("The ergo setup is not supported for the current osx version.") fmt.Println("Supported versions Catalina or below.") fmt.Println("Please, consider setting up ergo as proxy manually.") diff --git a/commands/setup_test.go b/commands/setup_test.go index b09866e..a9bbae0 100644 --- a/commands/setup_test.go +++ b/commands/setup_test.go @@ -60,11 +60,11 @@ func (r *TestRunnerWithOutput) Mock(command string, output []byte) { } func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) { - command_with_args := command + " " + strings.Join(args, " ") - key := strings.Join(strings.Split(command_with_args, " "), "_") + commandWithArgs := command + " " + strings.Join(args, " ") + key := strings.Join(strings.Split(commandWithArgs, " "), "_") mockedOutput, ok := r.MockedOutput[key] if !ok { - r.Test.Fatalf("No more expectation for command %s", command_with_args) + r.Test.Fatalf("No more expectation for command %s", commandWithArgs) return []byte{}, nil } diff --git a/main_test.go b/main_test.go index be35379..b79abd3 100644 --- a/main_test.go +++ b/main_test.go @@ -166,8 +166,8 @@ func (r *TestRunnerWithOutput) Mock(command string, output []byte) { } func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, error) { - command_with_args := command + " " + strings.Join(args, " ") - key := strings.Join(strings.Split(command_with_args, " "), "_") + commandWithArgs := command + " " + strings.Join(args, " ") + key := strings.Join(strings.Split(commandWithArgs, " "), "_") mockedOutput, ok := r.MockedOutput[key] if !ok { From 9195f3b85cf813aeb41bf9bf254402295938851c Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Sat, 3 Jun 2023 14:58:36 +0200 Subject: [PATCH 8/8] fixes makefile --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 251e611..b7a7128 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,6 @@ start: tools: @(go install golang.org/x/lint/golint) - # @(go get github.com/golang/lint) fmt: tools @(echo "${OK_COLOR}Running fmt ...${NO_COLOR}") @@ -66,7 +65,7 @@ vet: tools go vet ./... lint: tools - @(echo "${OK_COLOR}Running lint ...${NO_COLOR}") + @(echo "Running lint ...") @(export PATH=$$PATH:$$GOPATH/bin && [ $$(golint ./... | wc -l) != 0 ] && \ echo "${WARN_COLOR}Lint says the following files are not ok:${NO_COLOR}" && \ echo "${ERROR_COLOR}" && golint ./... && \