Skip to content

Commit

Permalink
feat: ✨output config paths into help
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Apr 17, 2024
1 parent 6d806e1 commit 05a5cb6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c genCmd) Run(g globalCmd, args []string) error {

fmt.Fprintf(os.Stderr, "output: %v\n", filename)

rule := g.defaultRule()
rule := defaultRule()

content, err := json.MarshalIndent(rule, "", " ")
if err != nil {
Expand Down
99 changes: 67 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c globalCmd) Run() error {
}
}

if err := c.prepare(wt.Filesystem.Root()); err != nil {
if err := c.prepare(repos); err != nil {
return err
}

Expand Down Expand Up @@ -137,22 +137,22 @@ func (c globalCmd) Run() error {
return nil
}

func (c *globalCmd) prepare(rootDir string) error {
c.rule = c.readRuleFile(rootDir)
func (c *globalCmd) prepare(repos *git.Repository) error {
c.rule, _ = readRuleFile(repos)

// scope history

c.scopes, c.scopesFileName, c.writeBackScopeHisotry = c.readScopesFile(rootDir)
c.scopes, c.scopesFileName, c.writeBackScopeHisotry = readScopesFile(repos)
if c.scopes == nil {
c.scopes = make(Scopes)
}

return nil
}

func (c globalCmd) defaultRule() Rule {
func defaultRule() Rule {
return Rule{
Types: c.defaultCommitTypes(),
Types: defaultCommitTypes(),
DenyEmptyType: false,
DenyAdlibType: false,
UseBreakingChange: false,
Expand All @@ -161,7 +161,7 @@ func (c globalCmd) defaultRule() Rule {
}
}

func (c globalCmd) defaultCommitTypes() *orderedmap.OrderedMap[string, CommitType] {
func defaultCommitTypes() *orderedmap.OrderedMap[string, CommitType] {
ct := orderedmap.New[string, CommitType]()
ct.Set("# comment1", commitTypeAsOM(
"comment starts with #",
Expand Down Expand Up @@ -219,36 +219,43 @@ func (c globalCmd) defaultCommitTypes() *orderedmap.OrderedMap[string, CommitTyp
return ct
}

func (c globalCmd) readRuleFile(rootDir string) *Rule {
// config
if cfg := c.getConfig(configRule); cfg != nil {
if r, err := tryReadRuleFile(filepath.Join(rootDir, *cfg)); err == nil {
return r
}
func readRuleFile(repos *git.Repository) (*Rule, string) {
var rootDir string
if wt, err := repos.Worktree(); err == nil {
rootDir = wt.Filesystem.Root()
}

// rootDir
if r, err := tryReadRuleFile(filepath.Join(rootDir, defaultRuleFileName)); err == nil {
return r
if rootDir != "" {
// config
if cfg := getGitConfig(repos, configRule); cfg != nil {
if r, err := tryReadRuleFile(filepath.Join(rootDir, *cfg)); err == nil {
return r, filepath.Join(rootDir, *cfg) + " (worktree config)"
}
}

// rootDir
if r, err := tryReadRuleFile(filepath.Join(rootDir, defaultRuleFileName)); err == nil {
return r, filepath.Join(rootDir, defaultRuleFileName) + " (worktree file)"
}
}

// user config dir
if cp, err := os.UserConfigDir(); err == nil {
if r, err := tryReadRuleFile(filepath.Join(cp, userConfigFolder, defaultRuleFileName)); err == nil {
return r
return r, filepath.Join(cp, userConfigFolder, defaultRuleFileName) + " (user config dir)"
}
}

// executable dir
if ep, err := os.Executable(); err != nil {
ed, _ := filepath.Split(ep)
if r, err := tryReadRuleFile(filepath.Join(ed, defaultRuleFileName)); err == nil {
return r
return r, filepath.Join(ed, defaultRuleFileName) + " (exe dir)"
}
}

r := c.defaultRule()
return &r
r := defaultRule()
return &r, "(default)"
}

func commitTypeAsOM(desc string, emoji string) CommitType {
Expand Down Expand Up @@ -284,21 +291,28 @@ func tryReadRuleFile(filename string) (*Rule, error) {
return &r, nil
}

func (c globalCmd) readScopesFile(rootDir string) (Scopes, string, bool) {
// config
if cfg := c.getConfig(configScopeHistory); cfg != nil {
filename := filepath.Join(rootDir, *cfg)
func readScopesFile(repos *git.Repository) (scopes Scopes, fileName string, writeback bool) {
var rootDir string
if wt, err := repos.Worktree(); err == nil {
rootDir = wt.Filesystem.Root()
}

if rootDir != "" {
// config
if cfg := getGitConfig(repos, configScopeHistory); cfg != nil {
filename := filepath.Join(rootDir, *cfg)
if sc, err := tryReadScopesFile(filename); err == nil {
return sc, filename, true
}
}

// rootDir
filename := filepath.Join(rootDir, defaultScopesFileName)
if sc, err := tryReadScopesFile(filename); err == nil {
return sc, filename, true
}
}

// rootDir
filename := filepath.Join(rootDir, defaultScopesFileName)
if sc, err := tryReadScopesFile(filename); err == nil {
return sc, filename, true
}

// user config dir
if cp, err := os.UserConfigDir(); err == nil {
filename := filepath.Join(cp, userConfigFolder, defaultScopesFileName)
Expand Down Expand Up @@ -343,8 +357,8 @@ func tryReadScopesFile(filename string) (Scopes, error) {
return sc, nil
}

func (c globalCmd) getConfig(key string) *string {
config, err := c.repository.Config()
func getGitConfig(repos *git.Repository, key string) *string {
config, err := repos.Config()
if err != nil {
return nil
}
Expand Down Expand Up @@ -660,6 +674,14 @@ func filterSuggestions(suggestions []prompt.Suggest, sub string, ignoreCase bool
var Version string

func main() {
rule, scope := getPathToHelp()
if rule != "" {
rule = "\nrule: " + rule + "\n"
}
if scope != "" {
scope = "scope: " + scope + "\n"
}

app := gli.NewWith(&globalCmd{})
app.Name = "git-cx"
app.Desc = "A conventional commits tool"
Expand All @@ -675,6 +697,7 @@ git cx
git cx gen
(edit .cx.json)
git cx
` + rule + scope + `
# record and complete scope history
(gitconfig: [cx] scopes=.scopes.json)`
Expand All @@ -685,3 +708,15 @@ git cx
os.Exit(1)
}
}

func getPathToHelp() (rule string, scope string) {
repos, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return "", ""
}

_, rule = readRuleFile(repos)
_, scope, _ = readScopesFile(repos)

return rule, scope
}

0 comments on commit 05a5cb6

Please sign in to comment.