From eb24c87bc72f72a2dd1b614a5685094bd1d63d20 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 23 Sep 2024 11:50:43 +0300 Subject: [PATCH] chore: enhance some code parts (#824) * chore: enhance some code parts * fix: fix testdata --- cmd/dump.go | 15 +++++++++++ internal/config/config.go | 54 +++++++++++++++++++++++++++------------ internal/git/state.go | 8 +++--- internal/lefthook/dump.go | 28 +++++++++++++++++--- testdata/dump.txt | 4 +-- 5 files changed, 84 insertions(+), 25 deletions(-) diff --git a/cmd/dump.go b/cmd/dump.go index 13d38744..919e36b7 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/evilmartians/lefthook/internal/lefthook" + "github.com/evilmartians/lefthook/internal/log" ) type dump struct{} @@ -21,6 +22,10 @@ func (dump) New(opts *lefthook.Options) *cobra.Command { }, } + dumpCmd.Flags().StringVarP( + &dumpArgs.Format, "format", "f", "yaml", "'yaml', 'toml', or 'json'", + ) + dumpCmd.Flags().BoolVarP( &dumpArgs.JSON, "json", "j", false, "dump in JSON format", @@ -31,5 +36,15 @@ func (dump) New(opts *lefthook.Options) *cobra.Command { "dump in TOML format", ) + err := dumpCmd.Flags().MarkDeprecated("json", "use --format=json") + if err != nil { + log.Warn("Unexpected error:", err) + } + + err = dumpCmd.Flags().MarkDeprecated("toml", "use --format=toml") + if err != nil { + log.Warn("Unexpected error:", err) + } + return &dumpCmd } diff --git a/internal/config/config.go b/internal/config/config.go index 2af54794..9f8fbab2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,7 +12,15 @@ import ( "github.com/evilmartians/lefthook/internal/version" ) -const dumpIndent = 2 +type DumpFormat int + +const ( + YAMLFormat DumpFormat = iota + JSONFormat + TOMLFormat + + dumpIndent = 2 +) type Config struct { MinVersion string `mapstructure:"min_version,omitempty"` @@ -37,7 +45,7 @@ func (c *Config) Validate() error { return version.CheckCovered(c.MinVersion) } -func (c *Config) Dump(asJSON bool, asTOML bool) error { +func (c *Config) Dump(format DumpFormat) error { res := make(map[string]interface{}) if err := mapstructure.Decode(c, &res); err != nil { return err @@ -54,18 +62,28 @@ func (c *Config) Dump(asJSON bool, asTOML bool) error { res[hookName] = hook } - if asJSON { - return dumpJSON(res) + var dumper dumper + switch format { + case YAMLFormat: + dumper = yamlDumper{} + case TOMLFormat: + dumper = tomlDumper{} + case JSONFormat: + dumper = jsonDumper{} + default: + dumper = yamlDumper{} } - if asTOML { - return dumpTOML(res) - } + return dumper.Dump(res) +} - return dumpYAML(res) +type dumper interface { + Dump(map[string]interface{}) error } -func dumpYAML(input map[string]interface{}) error { +type yamlDumper struct{} + +func (yamlDumper) Dump(input map[string]interface{}) error { encoder := yaml.NewEncoder(os.Stdout) encoder.SetIndent(dumpIndent) defer encoder.Close() @@ -78,23 +96,27 @@ func dumpYAML(input map[string]interface{}) error { return nil } -func dumpJSON(input map[string]interface{}) error { - res, err := json.MarshalIndent(input, "", " ") +type tomlDumper struct{} + +func (tomlDumper) Dump(input map[string]interface{}) error { + encoder := toml.NewEncoder(os.Stdout) + err := encoder.Encode(input) if err != nil { return err } - log.Info(string(res)) - return nil } -func dumpTOML(input map[string]interface{}) error { - encoder := toml.NewEncoder(os.Stdout) - err := encoder.Encode(input) +type jsonDumper struct{} + +func (jsonDumper) Dump(input map[string]interface{}) error { + res, err := json.MarshalIndent(input, "", " ") if err != nil { return err } + log.Info(string(res)) + return nil } diff --git a/internal/git/state.go b/internal/git/state.go index 69d6dfbf..dda14139 100644 --- a/internal/git/state.go +++ b/internal/git/state.go @@ -21,13 +21,13 @@ var refBranchRegexp = regexp.MustCompile(`^ref:\s*refs/heads/(.+)$`) func (r *Repository) State() State { branch := r.Branch() - if r.isMergeState() { + if r.inMergeState() { return State{ Branch: branch, Step: MergeStep, } } - if r.isRebaseState() { + if r.inRebaseState() { return State{ Branch: branch, Step: RebaseStep, @@ -65,14 +65,14 @@ func (r *Repository) Branch() string { return "" } -func (r *Repository) isMergeState() bool { +func (r *Repository) inMergeState() bool { if _, err := r.Fs.Stat(filepath.Join(r.GitPath, "MERGE_HEAD")); os.IsNotExist(err) { return false } return true } -func (r *Repository) isRebaseState() bool { +func (r *Repository) inRebaseState() bool { if _, mergeErr := r.Fs.Stat(filepath.Join(r.GitPath, "rebase-merge")); os.IsNotExist(mergeErr) { if _, applyErr := r.Fs.Stat(filepath.Join(r.GitPath, "rebase-apply")); os.IsNotExist(applyErr) { return false diff --git a/internal/lefthook/dump.go b/internal/lefthook/dump.go index be96fda7..54372091 100644 --- a/internal/lefthook/dump.go +++ b/internal/lefthook/dump.go @@ -6,8 +6,9 @@ import ( ) type DumpArgs struct { - JSON bool - TOML bool + JSON bool + TOML bool + Format string } func Dump(opts *Options, args DumpArgs) { @@ -23,7 +24,28 @@ func Dump(opts *Options, args DumpArgs) { return } - if err := cfg.Dump(args.JSON, args.TOML); err != nil { + var format config.DumpFormat + + switch args.Format { + case "yaml": + format = config.YAMLFormat + case "json": + format = config.JSONFormat + case "toml": + format = config.TOMLFormat + default: + format = config.YAMLFormat + } + + if args.JSON { + format = config.JSONFormat + } + + if args.TOML { + format = config.TOMLFormat + } + + if err := cfg.Dump(format); err != nil { log.Errorf("couldn't dump config: %s\n", err) return } diff --git a/testdata/dump.txt b/testdata/dump.txt index 5a5559e3..a57cfb25 100644 --- a/testdata/dump.txt +++ b/testdata/dump.txt @@ -5,11 +5,11 @@ exec lefthook dump cmp stdout lefthook-dumped.yml ! stderr . -exec lefthook dump --json +exec lefthook dump --format=json cmp stdout lefthook-dumped.json ! stderr . -exec lefthook dump --toml +exec lefthook dump -f toml cmp stdout lefthook-dumped.toml ! stderr .