Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added runtime internal component and MONDAY_EDITOR to specify editor
- Loading branch information
Showing
5 changed files
with
99 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |