Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #22 from eko/added-runtime
Added runtime internal component and MONDAY_EDITOR to specify editor
  • Loading branch information
eko committed Sep 14, 2019
2 parents 4135865 + c18da07 commit 43f1ceb
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -127,8 +127,9 @@ The following environment variables can be used to tweak your Monday configurati
| Environment variable | Description |
|:----------------------------:|-------------------------------------------------------------------------------------------|
| MONDAY_CONFIG_PATH | Specify the configuration path where your YAML files can be found |
| MONDAY_KUBE_CONFIG | Specify the location of your Kubernetes config file (if not in your home directory) |
| MONDAY_EDITOR | Specify which editor you want to use in order to edit configuration files |
| MONDAY_ENABLE_UI | Specify that you want to use the terminal UI instead of simply logging to stdout |
| MONDAY_KUBE_CONFIG | Specify the location of your Kubernetes config file (if not in your home directory) |

## Configuration

Expand Down
5 changes: 3 additions & 2 deletions cmd/edit.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os/exec"

"github.com/eko/monday/internal/runtime"
"github.com/eko/monday/pkg/config"
"github.com/spf13/cobra"
)
Expand All @@ -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
}
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/init.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"

"github.com/eko/monday/internal/runtime"
"github.com/eko/monday/pkg/config"
"github.com/spf13/cobra"
)
Expand All @@ -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 {
Expand Down
16 changes: 2 additions & 14 deletions cmd/main.go
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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?",
Expand Down
48 changes: 48 additions & 0 deletions 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))
}
}
43 changes: 43 additions & 0 deletions 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)
}

0 comments on commit 43f1ceb

Please sign in to comment.