Skip to content

Commit

Permalink
chore: Clean up flag error handling with must library
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 15, 2024
1 parent e774400 commit 39dced9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 43 deletions.
11 changes: 4 additions & 7 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gabe565.com/changelog-generator/internal/config"
"gabe565.com/changelog-generator/internal/git"
"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

Expand All @@ -27,13 +28,9 @@ func New(opts ...cobrax.Option) *cobra.Command {
return cmd
}

func run(cmd *cobra.Command, args []string) error {
completionFlag, err := cmd.Flags().GetString(config.CompletionFlag)
if err != nil {
return err
}
if completionFlag != "" {
return completion(cmd, args)
func run(cmd *cobra.Command, _ []string) error {
if shell := must.Must2(cmd.Flags().GetString(config.CompletionFlag)); shell != "" {
return completion(cmd, shell)
}

conf, err := config.Load(cmd)
Expand Down
12 changes: 3 additions & 9 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ import (
"errors"
"fmt"

"gabe565.com/changelog-generator/internal/config"
"github.com/spf13/cobra"
)

var ErrInvalidShell = errors.New("invalid shell")

func completion(cmd *cobra.Command, _ []string) error {
completionFlag, err := cmd.Flags().GetString(config.CompletionFlag)
if err != nil {
panic(err)
}

switch completionFlag {
func completion(cmd *cobra.Command, shell string) error {
switch shell {
case "bash":
return cmd.Root().GenBashCompletion(cmd.OutOrStdout())
case "zsh":
Expand All @@ -26,6 +20,6 @@ func completion(cmd *cobra.Command, _ []string) error {
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout())
default:
return fmt.Errorf("%w: %s", ErrInvalidShell, completionFlag)
return fmt.Errorf("%w: %s", ErrInvalidShell, shell)
}
}
23 changes: 10 additions & 13 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "github.com/spf13/cobra"
import (
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

const (
ConfigFlag = "config"
Expand All @@ -10,23 +13,17 @@ const (

func RegisterFlags(cmd *cobra.Command) {
cmd.Flags().String(ConfigFlag, "", `Config file (default ".changelog-generator.yaml")`)
if err := cmd.RegisterFlagCompletionFunc(ConfigFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
must.Must(cmd.RegisterFlagCompletionFunc(ConfigFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"yaml"}, cobra.ShellCompDirectiveFilterFileExt
}); err != nil {
panic(err)
}
}))

cmd.Flags().String(RepoFlag, ".", `Path to the git repo root. Parent directories will be walked until .git is found.`)
if err := cmd.RegisterFlagCompletionFunc(RepoFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
must.Must(cmd.RegisterFlagCompletionFunc(RepoFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{}, cobra.ShellCompDirectiveFilterDirs
}); err != nil {
panic(err)
}
}))

cmd.Flags().String(CompletionFlag, "", "Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.")
if err := cmd.RegisterFlagCompletionFunc(CompletionFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
must.Must(cmd.RegisterFlagCompletionFunc(CompletionFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"bash", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
panic(err)
}
}))
}
14 changes: 5 additions & 9 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"

"gabe565.com/utils/must"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
Expand All @@ -27,17 +28,11 @@ func Load(cmd *cobra.Command) (*Config, error) {

// Find config file
cfgFiles := make([]string, 0, 4)
cfgFile, err := cmd.Flags().GetString(ConfigFlag)
if err != nil {
return nil, err
}
cfgFile := must.Must2(cmd.Flags().GetString(ConfigFlag))
if cfgFile != "" {
cfgFiles = append(cfgFiles, cfgFile)
} else {
repoPath, err := cmd.Flags().GetString(RepoFlag)
if err != nil {
return nil, err
}
repoPath := must.Must2(cmd.Flags().GetString(RepoFlag))
cfgFiles = append(cfgFiles,
filepath.Join(repoPath, ".changelog-generator.yaml"),
filepath.Join(repoPath, ".changelog-generator.yml"),
Expand Down Expand Up @@ -108,12 +103,13 @@ func Load(cmd *cobra.Command) (*Config, error) {

if conf.Tag.Regexp != "" {
conf.Tag.Regexp = "^" + conf.Tag.Regexp + "$"
var err error
if conf.Tag.re, err = regexp.Compile(conf.Tag.Regexp); err != nil {
return nil, err
}
}

return conf, err
return conf, nil
}

func isGoReleaser(path string) bool {
Expand Down
7 changes: 2 additions & 5 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import (
"slices"

"gabe565.com/changelog-generator/internal/config"
"gabe565.com/utils/must"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/spf13/cobra"
)

func FindRepo(cmd *cobra.Command) (*git.Repository, error) {
repoPath, err := cmd.Flags().GetString(config.RepoFlag)
if err != nil {
return nil, err
}

repoPath := must.Must2(cmd.Flags().GetString(config.RepoFlag))
repo, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return nil, err
Expand Down

0 comments on commit 39dced9

Please sign in to comment.