From c18da07a11a542610c0f5e6116a9b53ad8975072 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Sat, 14 Sep 2019 12:15:14 +0200 Subject: [PATCH] Added runtime internal component and MONDAY_EDITOR to specify editor --- cmd/edit.go | 5 ++-- cmd/init.go | 5 ++-- cmd/main.go | 16 ++--------- internal/runtime/runtime.go | 48 ++++++++++++++++++++++++++++++++ internal/runtime/runtime_test.go | 43 ++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 internal/runtime/runtime.go create mode 100644 internal/runtime/runtime_test.go diff --git a/cmd/edit.go b/cmd/edit.go index 0f85f8d5..7e5a97fe 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -4,6 +4,7 @@ import ( "fmt" "os/exec" + "github.com/eko/monday/internal/runtime" "github.com/eko/monday/pkg/config" "github.com/spf13/cobra" ) @@ -28,10 +29,10 @@ in the source code repository.`, files = []string{config.Filepath} } - command := exec.Command(openerCommand, files...) + command := exec.Command(runtime.EditorCommand, files...) if err := command.Start(); err != nil { - fmt.Printf("❌ Cannot run the '%s' command to edit config file: %v\n", openerCommand, err) + fmt.Printf("❌ Cannot run the '%s' command to edit config file: %v\n", runtime.EditorCommand, err) return } }, diff --git a/cmd/init.go b/cmd/init.go index d453da9f..1f44b60a 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" + "github.com/eko/monday/internal/runtime" "github.com/eko/monday/pkg/config" "github.com/spf13/cobra" ) @@ -23,10 +24,10 @@ in the source code repository.`, return } - command := exec.Command(openerCommand, config.Filepath) + command := exec.Command(runtime.EditorCommand, config.Filepath) if err := command.Start(); err != nil { - fmt.Printf("❌ Cannot run the '%s' command to edit config file: %v\n", openerCommand, err) + fmt.Printf("❌ Cannot run the '%s' command to edit config file: %v\n", runtime.EditorCommand, err) return } } else { diff --git a/cmd/main.go b/cmd/main.go index 44ed563f..a57ea7f7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,10 +4,10 @@ import ( "fmt" "os" "os/signal" - "runtime" "strconv" "syscall" + "github.com/eko/monday/internal/runtime" "github.com/eko/monday/pkg/config" "github.com/eko/monday/pkg/forwarder" "github.com/eko/monday/pkg/hostfile" @@ -33,13 +33,11 @@ var ( runnerComponent *runner.Runner watcherComponent *watcher.Watcher - openerCommand string - uiEnabled = len(os.Getenv("MONDAY_ENABLE_UI")) > 0 ) func main() { - initRuntimeEnvironment() + runtime.InitRuntimeEnvironment() rootCmd := &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { @@ -77,16 +75,6 @@ func main() { } } -func initRuntimeEnvironment() { - switch runtime.GOOS { - case "darwin": - openerCommand = "open" - - default: - openerCommand = "gedit" - } -} - func selectProject(conf *config.Config) string { prompt := promptui.Select{ Label: "Which project do you want to work on?", diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go new file mode 100644 index 00000000..0ed0a4bc --- /dev/null +++ b/internal/runtime/runtime.go @@ -0,0 +1,48 @@ +package runtime + +import ( + "fmt" + "os" + "os/exec" + goRuntime "runtime" +) + +var ( + // EditorCommand specified the editor that will be used to open configuration files + EditorCommand string +) + +// InitRuntimeEnvironment initializes runtime depending on user's environment +// such as OS and architecture +func InitRuntimeEnvironment() { + initRuntimeEditor() +} + +// initRuntimeEditor initializes the editor that will be used +// to manage configuration files +func initRuntimeEditor() { + if value := os.Getenv("MONDAY_EDITOR"); value != "" { + // In case environment variable is set, do not guess an editor + EditorCommand = value + return + } + + switch goRuntime.GOOS { + case "darwin": + EditorCommand = "open" + + case "linux": + editors := []string{"gedit", "xed"} + + for _, editor := range editors { + _, err := exec.LookPath(editor) + if err == nil { + EditorCommand = editor + break + } + } + + default: + panic(fmt.Sprintf("Your operating (%s) system does not seems compatible. Sorry about that", goRuntime.GOOS)) + } +} diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go new file mode 100644 index 00000000..1a25ffce --- /dev/null +++ b/internal/runtime/runtime_test.go @@ -0,0 +1,43 @@ +package runtime + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestInitRuntimeEditorWhenMethodNotCalled(t *testing.T) { + // Given + os.Setenv("MONDAY_EDITOR", "") + + // Then + assert.Equal(t, "", EditorCommand) +} + +func TestInitRuntimeEditorWhenGuessByEnvironment(t *testing.T) { + // Then + os.Setenv("MONDAY_EDITOR", "") + + // When + initRuntimeEditor() + + // Then + var editorIsFound = false + if EditorCommand == "open" || EditorCommand == "gedit" || EditorCommand == "xed" { + editorIsFound = true + } + + assert.True(t, editorIsFound) +} + +func TestInitRuntimeEditorWhenEnvironmentVariableIsSet(t *testing.T) { + // Given + os.Setenv("MONDAY_EDITOR", "code") + + // When + initRuntimeEditor() + + // Then + assert.Equal(t, "code", EditorCommand) +}