Skip to content

Commit

Permalink
feat: ✨support rule file in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Apr 19, 2024
1 parent 62ff144 commit cc409b8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 34 deletions.
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,32 @@ First, generate a rule file.
```
git cx gen
# The default name is .cx.json.
# The default name is .cx.yaml.
# You can give it a name
git cx gen myrule.json
git cx gen myrule.yaml
```

Then, edit the file.

```
{
"headerFormat": "{{.type}}{{.scope_with_parens}}{{.bang}}: {{.emoji_unicode}}{{.description}}",
"headerFormatHint": ".type, .scope, .scope_with_parens, .bang(if BREAKING CHANGE), .emoji, .emoji_unicode, .description",
"types": {
"# comment1": {
"description": "comment, if name starts with #"
},
"feat": {
"description": "A new feature",
"emoji": ":sparkles:"
},
"fix": {
"description": "A bug fix",
"emoji": ":bug:"
},
```yaml
headerformat: '{{.type}}{{.scope_with_parens}}{{.bang}}: {{.emoji_unicode}}{{.description}}'
headerformathint: .type, .scope, .scope_with_parens, .bang(if BREAKING CHANGE), .emoji, .emoji_unicode, .description
types:
'# comment1':
desc: 'comment starts with #'
emoji: ""
feat:
desc: A new feature
emoji: ':sparkles:'
fix:
desc: A bug fix
emoji: ':bug:'
:
},
"denyEmptyType": false,
"denyAdlibType": false,
"useBreakingChange": false
}
denyemptytype: false
denyadlibtype: false
usebreakingchange: false
```

## Record and complete scope history
Expand Down Expand Up @@ -94,11 +90,11 @@ dummy text
1. gitconfig ([cx] rule={PATH})
2. current worktree root
3. config directory
- {CONFIG_DIR}/git-cx/.cx.json
- Windows: %appdata%\git-cx\.cx.json
- {CONFIG_DIR}/git-cx/.cx.yaml
- Windows: %appdata%\git-cx\.cx.yaml
- (see https://cs.opensource.google/go/go/+/go1.17.3:src/os/file.go;l=457)
4. exe dir
- .cx.json
- Place the json in the same location as the executable.
- .cx.yaml
- Place the yaml in the same location as the executable.

<!-- vim: set et ft=markdown sts=4 sw=4 ts=4 tw=0 : -->
26 changes: 24 additions & 2 deletions cmd_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
)

type genCmd struct {
}

func (c genCmd) Run(g globalCmd, args []string) error {
filename := defaultRuleFileName + ".json"
filename := defaultRuleFileName + ".yaml"
if len(args) > 0 {
filename = args[0]
}
Expand All @@ -25,7 +27,27 @@ func (c genCmd) Run(g globalCmd, args []string) error {

rule := defaultRule()

content, err := json.MarshalIndent(rule, "", " ")
if in(filepath.Ext(filename), ".json") {
content, err := json.MarshalIndent(rule, "", " ")
if err != nil {
return err
}

file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

_, err = file.WriteString(string(content))
if err != nil {
return err
}

return nil
}

content, err := yaml.Marshal(rule)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/shu-go/findcfg v0.1.1
github.com/shu-go/gli v1.5.7
github.com/shu-go/orderedmap v0.1.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -45,5 +46,4 @@ require (
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.20.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
41 changes: 36 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/shu-go/findcfg"
"github.com/shu-go/gli"
"github.com/shu-go/orderedmap"
"gopkg.in/yaml.v3"
)

const (
Expand All @@ -46,7 +47,7 @@ type globalCmd struct {

Debug bool `cli:"debug" default:"false" help:"do not commit, do output to stdout"`

Gen genCmd `cli:"generate,gen" help:"generate rule json file"`
Gen genCmd `cli:"generate,gen" help:"generate rule file"`
}

func (c globalCmd) Run() error {
Expand Down Expand Up @@ -235,6 +236,7 @@ func readRuleFile(repos *git.Repository) (*Rule, string) {

finder := findcfg.New(
findcfg.ExactPath(exactPath),
findcfg.YAML(),
findcfg.JSON(),
findcfg.Dir(rootDir),
findcfg.UserConfigDir(userConfigFolder),
Expand Down Expand Up @@ -277,10 +279,25 @@ func tryReadRuleFile(filename string) (*Rule, error) {
r := Rule{
Types: orderedmap.New[string, CommitType](),
}
if err := json.Unmarshal(content, &r); err != nil {
return nil, err
}

if in(filepath.Ext(filename), ".yaml", ".yml") {
if err := yaml.Unmarshal(content, &r); err != nil {
return nil, err
}
return &r, nil
}
if in(filepath.Ext(filename), ".json") {
if err := json.Unmarshal(content, &r); err != nil {
return nil, err
}
return &r, nil
}
if err := yaml.Unmarshal(content, &r); err != nil {
if err := json.Unmarshal(content, &r); err != nil {
return nil, err
}
return &r, nil
}
return &r, nil
}

Expand Down Expand Up @@ -678,7 +695,7 @@ git cx
# customize
git cx gen
(edit .cx.json)
(edit .cx.yaml)
git cx
` + rule + scope + `
Expand All @@ -703,3 +720,17 @@ func getPathToHelp() (rule string, scope string) {

return rule, scope
}

func in(s string, choices ...string) bool {
if len(choices) == 0 {
return false
}

for i := 0; i < len(choices); i++ {
if strings.EqualFold(s, choices[i]) {
return true
}
}

return false
}

0 comments on commit cc409b8

Please sign in to comment.