Skip to content

Commit f747e41

Browse files
relastleHiroki-Konishi
authored andcommitted
Fix: behavior of magic command (#23)
Fix: behavior of magic command
2 parents 2d79559 + 17ce12a commit f747e41

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

src/aop.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
// a started time.
1212
func MeasureElapsedTime(start time.Time, name string) {
1313
logPath := os.ExpandEnv(defaultLogPath)
14-
os.MkdirAll(path.Dir(logPath), os.ModePerm)
14+
err := os.MkdirAll(path.Dir(logPath), os.ModePerm)
15+
if err != nil {
16+
panic(err)
17+
}
1518
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
1619
if err != nil {
1720
log.Fatalf("error opening file: %v", err)

src/out.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,19 @@ func (out *Out) expandAllMagics() error {
132132
if !strings.HasPrefix(cg.Stmt, "%") {
133133
continue
134134
}
135-
snippetRelPath := strings.Replace(cg.Stmt, "%", "", -1)
135+
startIndex := strings.Index(cg.Stmt, "%") + 1 // inclusive
136+
length := strings.Index(cg.Stmt[startIndex:], "%") // exclusive
137+
snippetRelPath := cg.Stmt[startIndex : startIndex+length]
136138
snippetFileToApply, err := getToApply(snippetFiles, snippetRelPath)
137139
if err != nil {
138140
return err
139141
}
140142

141-
cg.Stmt = fmt.Sprintf(
142-
"cat %v | taggo -q '0:yellow' -d ' '",
143-
snippetFileToApply.Path,
143+
cg.Stmt = strings.Replace(
144+
cg.Stmt,
145+
"%"+snippetRelPath+"%",
146+
fmt.Sprintf("cat %s", snippetFileToApply.Path),
147+
-1,
144148
)
145149
}
146150
return nil

src/rule.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ func GetAllRuleFiles() []*RuleFile {
7979

8080
res := []*RuleFile{}
8181
for _, ruleRoot := range ruleRoots {
82+
// expand environment variable
83+
ruleRoot = os.ExpandEnv(ruleRoot)
8284
if ruleRoot == "" {
8385
continue
8486
}
8587
globPattern := fmt.Sprintf(
8688
`%v/**/*%v`,
87-
os.ExpandEnv(ruleRoot),
89+
ruleRoot,
8890
pmyRuleSuffixCommon,
8991
)
9092
matches, err := zglob.Glob(globPattern)

src/snippets.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pmy
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"path"
78
"path/filepath"
@@ -36,12 +37,14 @@ func GetAllSnippetFiles() []*SnippetFile {
3637

3738
res := []*SnippetFile{}
3839
for _, snippetRoot := range snippetRoots {
40+
// expand environment variable
41+
snippetRoot = os.ExpandEnv(snippetRoot)
3942
if snippetRoot == "" {
4043
continue
4144
}
4245
globPattern := fmt.Sprintf(
4346
`%v/**/*%v`,
44-
os.ExpandEnv(snippetRoot),
47+
snippetRoot,
4548
pmySnippetSuffix,
4649
)
4750
matches, err := zglob.Glob(globPattern)
@@ -51,6 +54,7 @@ func GetAllSnippetFiles() []*SnippetFile {
5154
for _, snippetPath := range matches {
5255
relpath, err := filepath.Rel(snippetRoot, snippetPath)
5356
if err != nil {
57+
log.Fatal(err)
5458
relpath = ""
5559
}
5660
res = append(

test/rules/pmy_rules.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"cmdGroups": [
2929
{
3030
"tag": "",
31-
"stmt": "%magic",
31+
"stmt": "%magic%",
3232
"after": "awk '{print $1}'"
3333
}
3434
],

0 commit comments

Comments
 (0)