Skip to content

Commit

Permalink
Merge pull request #60 from ivanilves/issue-57
Browse files Browse the repository at this point in the history
feat(issues/57): use ${EDITOR} variable to edit files
  • Loading branch information
ivanilves authored Oct 27, 2024
2 parents 3410b8a + 3df81ae commit ba5a313
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/travelgrunt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ var appVersion = "default"

var outFile string
var expression string
var editFile bool
var top bool
var version bool

func init() {
flag.StringVar(&outFile, "out-file", "", "output selected path into the file specified")
flag.StringVar(&expression, "x", "", "use arbitrary expression passed instead of configured rules")
flag.BoolVar(&editFile, "e", false, "edit file selected instead of changing working directory")
flag.BoolVar(&top, "top", false, "get to the repository top level (root) path")
flag.BoolVar(&version, "version", false, "print application version and exit")
}
Expand Down Expand Up @@ -132,6 +134,8 @@ func main() {
cfg = cfg.WithNameEx(expression)
}

cfg.UseFiles = editFile

entries, paths, err := directory.Collect(rootPath, cfg)

if err != nil {
Expand All @@ -149,7 +153,9 @@ func main() {
if outFile != "" {
path := getEntryPath(entries, selected, rootPath)

tag(path)
if !editFile {
tag(path)
}

writeFileAndExit(outFile, path)
}
Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- cmd/**/*
1 change: 1 addition & 0 deletions fixtures/config/rule/link-to-sample.hcl
1 change: 1 addition & 0 deletions fixtures/config/rule/sample.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# dummy sample
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ type Config struct {
Rules []rule.Rule `yaml:"rules"`

IsDefault bool
UseFiles bool
}

// DefaultConfig returns default travelgrunt repo-level configuration
func DefaultConfig() Config {
return Config{
Rules: []rule.Rule{{ModeFn: mode.IsTerragrunt}},
IsDefault: true,
UseFiles: false,
}
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/config/rule/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rule

import (
"os"
"testing"

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

const (
fixturePath = "../../../fixtures/config/rule"
)

func TestPathPrefixed(t *testing.T) {
assert := assert.New(t)

assert.False(Rule{Prefix: "./prefix"}.pathPrefixed("/etc/passwd"))
assert.True(Rule{Prefix: ""}.pathPrefixed("/etc/passwd"))
assert.True(Rule{Prefix: "./prefix"}.pathPrefixed("./prefix/file"))
}

func TestNormalizeNameEx(t *testing.T) {
assert := assert.New(t)

r := Rule{NameEx: "*.inc.php"}

assert.Equal(r.normalizeNameEx(), "^.*\\.inc\\.php$")
}

func TestNameMatched(t *testing.T) {
assert := assert.New(t)

files, err := os.ReadDir(fixturePath)
if err != nil {
panic(err)
}

for _, file := range files {
assert.True(Rule{}.nameMatched(file))
assert.True(Rule{NameEx: ".*"}.nameMatched(file))
assert.False(Rule{NameEx: "some-nonsense"}.nameMatched(file))
}
}
10 changes: 9 additions & 1 deletion pkg/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ func isHiddenDir(d os.DirEntry) bool {
return d.IsDir() && string(d.Name()[0]) == "."
}

func getAbsPath(path string, useFiles bool) string {
if useFiles {
return path
}

return filepath.Dir(path)
}

func isInScope(absPath string, rootPath string) bool {
return len(absPath) >= len(rootPath)
}
Expand All @@ -31,7 +39,7 @@ func Collect(rootPath string, cfg config.Config) (entries map[string]string, pat
return filepath.SkipDir
}

absPath := filepath.Dir(path)
absPath := getAbsPath(path, cfg.UseFiles)

if isInScope(absPath, rootPath) {
relPath := strings.TrimPrefix(absPath, rootPath+"/")
Expand Down
9 changes: 9 additions & 0 deletions pkg/directory/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const (
invalidPath = "/c2RmZ3JlZ2ZncnR3Z3I0aGd3dGd3d3d3d3d3d2RmZGZmZnJlZmdydDRndnQ0M3RodWprbDhpb2s4"
)

func TestGetAbsPath(t *testing.T) {
assert := assert.New(t)

const testPath = "/etc/passwd"

assert.Equal(getAbsPath(testPath, false), "/etc")
assert.Equal(getAbsPath(testPath, true), "/etc/passwd")
}

func TestCollect(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit ba5a313

Please sign in to comment.