Skip to content

Commit

Permalink
Merge branch 'feature/unescape'
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetsu committed Oct 8, 2022
2 parents db8711f + a57ecf6 commit 49a32a0
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 22 deletions.
33 changes: 16 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func New(command string) *Config {
// InitConfig loads a configuration file.
// Priority: default < ~/.gaze.yml < ~/.config/gaze.yaml < -f option)
func InitConfig() (*Config, error) {
configPath := searchConfigPath()
home := homeDirPath()
configPath := searchConfigPath(home)
return makeConfigFromFile(configPath)
}

Expand Down Expand Up @@ -99,24 +100,22 @@ func prepare(configs *Config) *Config {
return configs
}

func searchConfigPath() string {
home := homeDirPath()
if home != "" {
configDir := path.Join(home, ".config", "gaze")
for _, n := range []string{"gaze.yml", "gaze.yaml"} {
candidate := path.Join(configDir, n)
if fs.IsFile(candidate) {
return candidate
}
func searchConfigPath(home string) string {
if !fs.IsDir(home) {
return ""
}
configDir := path.Join(home, ".config", "gaze")
for _, n := range []string{"gaze.yml", "gaze.yaml"} {
candidate := path.Join(configDir, n)
if fs.IsFile(candidate) {
return candidate
}

for _, n := range []string{".gaze.yml", ".gaze.yaml"} {
candidate := path.Join(home, n)
if fs.IsFile(candidate) {
return candidate
}
}
for _, n := range []string{".gaze.yml", ".gaze.yaml"} {
candidate := path.Join(home, n)
if fs.IsFile(candidate) {
return candidate
}

}
return ""
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package config

import (
"io/ioutil"
"os"
"path"
"testing"
)

Expand Down Expand Up @@ -85,6 +87,40 @@ func TestInvalidYaml(t *testing.T) {
}
}

func TestSearchConfigPath(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "__gaze_test")

if searchConfigPath("") != "" {
t.Fatal()
}

// Should be not found
if searchConfigPath(tempDir) != "" {
t.Fatal()
}

os.Create(path.Join(tempDir, ".gaze.yaml"))
if searchConfigPath(tempDir) != path.Join(tempDir, ".gaze.yaml") {
t.Fatal()
}

os.Create(path.Join(tempDir, ".gaze.yml"))
if searchConfigPath(tempDir) != path.Join(tempDir, ".gaze.yml") {
t.Fatal()
}

os.MkdirAll(path.Join(tempDir, ".config", "gaze"), os.ModePerm)
os.Create(path.Join(tempDir, ".config", "gaze", "gaze.yaml"))
if searchConfigPath(tempDir) != path.Join(tempDir, ".config", "gaze", "gaze.yaml") {
t.Fatal()
}

os.Create(path.Join(tempDir, ".config", "gaze", "gaze.yml"))
if searchConfigPath(tempDir) != path.Join(tempDir, ".config", "gaze", "gaze.yml") {
t.Fatal()
}
}

func getFirstMatch(config *Config, fileName string) *Command {
var result *Command
for _, command := range config.Commands {
Expand Down
8 changes: 4 additions & 4 deletions pkg/gazer/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func kill(cmd *exec.Cmd, reason string) bool {
}

func createCommand(commandString string) *exec.Cmd {
perser := shellwords.NewParser()
// perser.ParseBacktick = true
// perser.ParseEnv = true
args, err := perser.Parse(commandString)
parser := shellwords.NewParser()
// parser.ParseBacktick = true
// parser.ParseEnv = true
args, err := parser.Parse(commandString)
if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gazer/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func getOrCreateTemplate(sourceString string) (*mustache.Template, error) {
return cachedTemplate, nil
}

template, err := mustache.ParseString(sourceString)
template, err := mustache.ParseStringRaw(sourceString, true)
if err != nil {
return nil, fmt.Errorf("%v(%s)", err, sourceString)
}
Expand Down
1 change: 1 addition & 0 deletions test/e2e/files/he&llo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello, world!(Python)")
1 change: 1 addition & 0 deletions test/e2e/files/he'llo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello, world!(Python)")
35 changes: 35 additions & 0 deletions test/e2e/test03.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /bin/sh


dir=$(cd $(dirname $0); pwd)
filedir=$dir/files

cd $dir
rm -f test.*.log

timeout -sKILL 3 ./main -v files/*.* | tee test.log &

sleep 0.1
touch "$filedir/he'llo.py"
sleep 0.1
touch "$filedir/he&llo.py"
sleep 0.1
touch "$filedir/he'llo.py"
sleep 0.1
touch "$filedir/he&llo.py"
sleep 0.1
touch "$filedir/he'llo.py"
sleep 0.1
touch "$filedir/he&llo.py"

wait

num=`cat test.log | grep "hello, world!" | wc -l`

if [ $num -ne6 ]; then
echo "Failed:${num}"
exit 1
fi

echo "OK"
exit 0

0 comments on commit 49a32a0

Please sign in to comment.