Skip to content

Commit

Permalink
allow editor arguments (#9)
Browse files Browse the repository at this point in the history
* allow editor arguments

* use strings.Fields
  • Loading branch information
rajatjindal authored Apr 15, 2020
1 parent 14601be commit 8a04d0f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.7
k8s.io/api v0.17.0
k8s.io/apimachinery v0.17.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand All @@ -146,10 +147,12 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
16 changes: 15 additions & 1 deletion pkg/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package editor
import (
"os"
"os/exec"
"strings"
)

const defaultEditor = "vi"
Expand All @@ -14,8 +15,21 @@ func Edit(file string) error {
editorFromEnv = defaultEditor
}

cmd := exec.Command(editorFromEnv, file)
command, args := getCommandAndArgs(editorFromEnv, file)

cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
}

func getCommandAndArgs(editorFromEnv, file string) (string, []string) {
carray := strings.Fields(editorFromEnv)
command := carray[0]
if len(carray) > 1 {
var args = append(carray[1:len(carray)], file)
return command, args
}

return command, []string{file}
}
54 changes: 54 additions & 0 deletions pkg/editor/editor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package editor

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetCommandAndArgs(t *testing.T) {
testcases := []struct {
name string
editor string
file string
expectedCommand string
expectedArgs []string
}{
{
name: "single word editor vim",
editor: "vim",
file: "some-file.txt",
expectedCommand: "vim",
expectedArgs: []string{"some-file.txt"},
},
{
name: "single word editor code",
editor: "code",
file: "some-file.txt",
expectedCommand: "code",
expectedArgs: []string{"some-file.txt"},
},
{
name: "code with arguments",
editor: "code --wait",
file: "some-file.txt",
expectedCommand: "code",
expectedArgs: []string{"--wait", "some-file.txt"},
},
{
name: "code with arguments and inconsistent spacing",
editor: "code --wait",
file: "some-file.txt",
expectedCommand: "code",
expectedArgs: []string{"--wait", "some-file.txt"},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
command, args := getCommandAndArgs(tc.editor, tc.file)
assert.Equal(t, tc.expectedCommand, command)
assert.Equal(t, tc.expectedArgs, args)
})
}
}

0 comments on commit 8a04d0f

Please sign in to comment.