Skip to content

Commit a80e917

Browse files
author
Hiroki Konishi
committed
TMP:
1 parent 74ff60d commit a80e917

File tree

11 files changed

+119
-50
lines changed

11 files changed

+119
-50
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bench:
1717

1818
.PHONY: lint
1919
lint:
20-
golint ./main.go
20+
golangci-lint run ./...
2121

2222
.PHONY: docker
2323
docker:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.13
44

55
require (
66
github.com/fatih/color v1.7.0
7+
github.com/mattn/go-zglob v0.0.1
78
github.com/rakyll/statik v0.1.6
89
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx
44
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
55
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
66
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7+
github.com/mattn/go-zglob v0.0.1 h1:xsEx/XUoVlI6yXjqBK062zYhRTZltCNmYPx6v+8DNaY=
8+
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
79
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
810
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
911
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c h1:+rQG0CRZFZw5xK4A9RCLxRcWdROI4DzCU66iWZx/0HE=

main.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020

2121
func mainRoutine() {
2222
outString := pmy.Run(
23-
pmy.PmyRulePath,
23+
pmy.RulePath,
2424
pmy.Input{
2525
BufferLeft: bufferLeft,
2626
BufferRight: bufferRight,
@@ -45,11 +45,23 @@ func initCmdRoutine() {
4545
fmt.Printf(string(bs))
4646
}
4747

48-
func ruleListCmdRoutine() {
48+
func debug() {
49+
}
4950

51+
func ruleListCmdRoutine() {
52+
pmy.SetConfigs()
53+
paths := pmy.GetAllRuleJSONPaths()
54+
for _, path := range paths {
55+
fmt.Println(path)
56+
}
5057
}
5158

5259
func snippetListCmdRoutine() {
60+
pmy.SetConfigs()
61+
paths := pmy.GetAllSnippetJSONPaths()
62+
for _, path := range paths {
63+
fmt.Println(path)
64+
}
5365
}
5466

5567
func main() {
@@ -65,21 +77,27 @@ func main() {
6577
// Subcommand for listing the all loaded snippet json paths
6678
snippetsListFlagSet := flag.NewFlagSet("snippet-list", flag.ExitOnError)
6779

80+
// Subcommand for debuggin
81+
debugFlagSet := flag.NewFlagSet("debug", flag.ExitOnError)
82+
6883
pmy.SetConfigs()
6984

7085
subCommand := colorflag.Parse([]*flag.FlagSet{
7186
initFlagSet,
7287
ruleListFlagSet,
7388
snippetsListFlagSet,
89+
debugFlagSet,
7490
})
7591

7692
switch subCommand {
7793
case "init":
7894
initCmdRoutine()
7995
case "rule-list":
80-
fmt.Println("rule-list")
96+
ruleListCmdRoutine()
8197
case "snippet-list":
82-
fmt.Println("snippet-list")
98+
snippetListCmdRoutine()
99+
case "debug":
100+
debug()
83101
default:
84102
mainRoutine()
85103
}

src/config.go

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package pmy
22

33
import (
4+
"fmt"
45
"log"
56
"os"
7+
"strings"
8+
9+
"github.com/mattn/go-zglob"
610
)
711

812
const (
9-
pmyRulesPathVarName string = "PMY_RULE_PATH"
10-
pmyTagDelimiterVarName string = "PMY_TAG_DELIMITER"
11-
pmySnippetRootVarName string = "PMY_SNIPPET_ROOT"
13+
defaultRulePath string = "${HOME}/.pmy/rules"
14+
defaultSnippetPath string = "${HOME}/.pmy/snippets"
15+
RulesPathVarName string = "PMY_RULE_PATH"
16+
SnippetPathVarName string = "PMY_SNIPPET_PATH"
17+
TagDelimiterVarName string = "PMY_TAG_DELIMITER"
1218
)
1319

1420
var (
15-
// PmyRulePath is a json path contining rules
16-
PmyRulePath string
17-
// PmyDelimiter defines delimiter string
21+
// RulePath is a json path contining rules
22+
RulePath string
23+
// Delimiter defines delimiter string
1824
// that divide `tag` and one line of source
19-
PmyDelimiter string
20-
// PmySnippetRoot defines snippet root directry path
21-
PmySnippetRoot string
25+
Delimiter string
26+
// SnippetPath defines snippet root directry path
27+
SnippetPath string
2228
)
2329

30+
// setConfig set go varible from the given environment variable
2431
func setConfig(
2532
target *string,
2633
varName string,
@@ -35,7 +42,41 @@ func setConfig(
3542
// SetConfigs set all Pmy config variable from shell's
3643
// environment variables.
3744
func SetConfigs() {
38-
setConfig(&PmyRulePath, pmyRulesPathVarName)
39-
setConfig(&PmyDelimiter, pmyTagDelimiterVarName)
40-
setConfig(&PmySnippetRoot, pmySnippetRootVarName)
45+
setConfig(&RulePath, RulesPathVarName)
46+
setConfig(&Delimiter, TagDelimiterVarName)
47+
setConfig(&SnippetPath, SnippetPathVarName)
48+
}
49+
50+
// GetAllRuleJSONPaths get all pmy rules json paths
51+
// configured by environment variable
52+
func GetAllRuleJSONPaths() []string {
53+
ruleRoots := []string{defaultRulePath}
54+
ruleRoots = append(ruleRoots, strings.Split(RulePath, ":")...)
55+
res := []string{}
56+
for _, ruleRoot := range ruleRoots {
57+
globPattern := fmt.Sprintf(`%v/**/*pmy_rules.json`, os.ExpandEnv(ruleRoot))
58+
matches, err := zglob.Glob(globPattern)
59+
if err != nil {
60+
panic(err)
61+
}
62+
res = append(res, matches...)
63+
}
64+
return res
65+
}
66+
67+
// GetAllSnippetJSONPaths get all pmy rules json paths
68+
// configured by environment variable
69+
func GetAllSnippetJSONPaths() []string {
70+
snippetsRoots := []string{defaultSnippetPath}
71+
snippetsRoots = append(snippetsRoots, strings.Split(SnippetPath, ":")...)
72+
res := []string{}
73+
for _, snippetsRoot := range snippetsRoots {
74+
globPattern := fmt.Sprintf(`%v/**/*.txt`, os.ExpandEnv(snippetsRoot))
75+
matches, err := zglob.Glob(globPattern)
76+
if err != nil {
77+
panic(err)
78+
}
79+
res = append(res, matches...)
80+
}
81+
return res
4182
}

src/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func Run(cfgPath string, in Input) string {
6868
return ""
6969
}
7070
rule := resRules[0]
71-
pmyOut := newPmyOutFromRule(rule)
71+
Out := newOutFromRule(rule)
7272

7373
// Ouput result
7474
// log.Print(out)
75-
return pmyOut.toShellVariables()
75+
return Out.toShellVariables()
7676
}

src/core_bench_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// DumpDummyRulesJSON dumps arbitrary number of rules into given file path
1313
func DumpDummyRulesJSON(resultPath string, ruleNum int, cmdGroupNum int) error {
14-
pmyRules := pmyRules{}
14+
Rules := Rules{}
1515
for i := 0; i < ruleNum; i++ {
1616
cgs := CmdGroups{}
1717
for j := 0; j < cmdGroupNum; j++ {
@@ -22,18 +22,18 @@ func DumpDummyRulesJSON(resultPath string, ruleNum int, cmdGroupNum int) error {
2222
}
2323
cgs = append(cgs, cg)
2424
}
25-
rule := &pmyRule{
25+
rule := &Rule{
2626
Name: fmt.Sprintf("test%v", ruleNum),
2727
RegexpLeft: ".*test.*",
2828
RegexpRight: "",
2929
CmdGroups: cgs,
3030
BufferLeft: "[]",
3131
BufferRight: "[]",
3232
}
33-
pmyRules = append(pmyRules, rule)
33+
Rules = append(Rules, rule)
3434
}
3535

36-
cgsJSON, _ := json.Marshal(pmyRules)
36+
cgsJSON, _ := json.Marshal(Rules)
3737
err := ioutil.WriteFile(resultPath, cgsJSON, 0644)
3838
if err != nil {
3939
return err

src/fetch.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ type ruleFetcher interface {
66
// GetCommand returns the command to be executed
77
// given the
88
fetch(
9-
rules pmyRules,
9+
rules Rules,
1010
bufferLeft string,
1111
bufferRight string,
12-
) (pmyRules, error)
12+
) (Rules, error)
1313
}
1414

1515
// Mock of rruleFetcher
1616
type ruleFetcherMock struct {
1717
}
1818

1919
func (cg *ruleFetcherMock) fetch(
20-
rules pmyRules,
20+
rules Rules,
2121
bufferLeft string,
2222
bufferRight string,
23-
) (pmyRules, error) {
23+
) (Rules, error) {
2424
return nil, nil
2525
}
2626

2727
type ruleFetcherImpl struct {
2828
}
2929

3030
func (cg *ruleFetcherImpl) fetch(
31-
rules pmyRules,
31+
rules Rules,
3232
bufferLeft string,
3333
bufferRight string,
34-
) (pmyRules, error) {
35-
resRules := pmyRules{}
34+
) (Rules, error) {
35+
resRules := Rules{}
3636
for _, rule := range rules {
3737
ok, err := rule.match(
3838
bufferLeft,

src/out.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ type afterCmd struct {
2323
after string
2424
}
2525

26-
// pmyOut represents Output of pmy against zsh routine.
26+
// Out represents Output of pmy against zsh routine.
2727
// This struct has strings exported to shell, whose embedded
2828
// variables are all expanded.
29-
type pmyOut struct {
29+
type Out struct {
3030
bufferLeft string
3131
bufferRight string
3232
cmdGroups CmdGroups
@@ -35,10 +35,10 @@ type pmyOut struct {
3535
allEmptyTag bool
3636
}
3737

38-
// newPmyOutFromRule create new pmyOut from rule
38+
// newOutFromRule create new Out from rule
3939
// which matches query and already has paramMap
40-
func newPmyOutFromRule(rule *pmyRule) pmyOut {
41-
out := pmyOut{}
40+
func newOutFromRule(rule *Rule) Out {
41+
out := Out{}
4242
// pass resulting buffer informaiton
4343
out.bufferLeft = rule.BufferLeft
4444
out.bufferRight = rule.BufferRight
@@ -57,7 +57,7 @@ func newPmyOutFromRule(rule *pmyRule) pmyOut {
5757

5858
// buildMainCommand builds main command that concatenate
5959
// all results of given command groups
60-
func (out *pmyOut) buildMainCommand() string {
60+
func (out *Out) buildMainCommand() string {
6161
res := ""
6262
pmyDelimiter := os.Getenv("PMY_TAG_DELIMITER")
6363
for _, cg := range out.cmdGroups {
@@ -80,9 +80,9 @@ func (out *pmyOut) buildMainCommand() string {
8080
return res
8181
}
8282

83-
// toShellVariables create zsh statement where pmyOut's attributes are
83+
// toShellVariables create zsh statement where Out's attributes are
8484
// passed into shell variables
85-
func (out *pmyOut) toShellVariables() string {
85+
func (out *Out) toShellVariables() string {
8686
res := ""
8787
res += fmt.Sprintf("local %v=$'%v';", shellCommandVariableName, utils.Escape(out.buildMainCommand(), "'"))
8888
res += fmt.Sprintf("local %v=$'%v';", shellBufferLeftVariableName, utils.Escape(out.bufferLeft, "'"))
@@ -115,15 +115,19 @@ func expand(org string, paramMap map[string]string) string {
115115
return res
116116
}
117117

118+
func fetchSnippetJSONPath(snippetRelPath string) string {
119+
return ""
120+
}
121+
118122
// expandAllMagics expands all magic commnad in Stmt
119123
// written in `%hoge` format
120-
func (out *pmyOut) expandAllMagics() {
124+
func (out *Out) expandAllMagics() {
121125
for _, cg := range out.cmdGroups {
122126
if !strings.HasPrefix(cg.Stmt, "%") {
123127
continue
124128
}
125-
snippetBaseName := strings.Replace(cg.Stmt, "%", "", -1)
126-
snippetPath := fmt.Sprintf("%v/%v.txt", PmySnippetRoot, snippetBaseName)
129+
snippetRelPath := strings.Replace(cg.Stmt, "%", "", -1)
130+
snippetPath := fetchSnippetJSONPath(snippetRelPath)
127131
cg.Stmt = fmt.Sprintf(
128132
"cat %v | taggo -q '0:yellow' -d ' '",
129133
snippetPath,
@@ -133,7 +137,7 @@ func (out *pmyOut) expandAllMagics() {
133137
}
134138

135139
// expandAllParams expands all params that refer to regexp parameters
136-
func (out *pmyOut) expandAllParams(paramMap map[string]string) {
140+
func (out *Out) expandAllParams(paramMap map[string]string) {
137141
out.bufferLeft = expand(out.bufferLeft, paramMap)
138142
out.bufferRight = expand(out.bufferRight, paramMap)
139143
out.fuzzyFinderCmd = expand(out.fuzzyFinderCmd, paramMap)
@@ -143,12 +147,12 @@ func (out *pmyOut) expandAllParams(paramMap map[string]string) {
143147
return
144148
}
145149

146-
func (out *pmyOut) toJSON() string {
150+
func (out *Out) toJSON() string {
147151
bytes, _ := json.Marshal(out)
148152
str := string(bytes)
149153
return str
150154
}
151155

152-
// func (out *pmyOut) serialize() string {
156+
// func (out *Out) serialize() string {
153157
// return out.BufferLeft + delimiter + out.BufferRight + delimiter + out.Command
154158
// }

0 commit comments

Comments
 (0)