Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enhance some code parts #824

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/evilmartians/lefthook/internal/lefthook"
"github.com/evilmartians/lefthook/internal/log"
)

type dump struct{}
Expand All @@ -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",
Expand All @@ -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
}
54 changes: 38 additions & 16 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions internal/git/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions internal/lefthook/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type DumpArgs struct {
JSON bool
TOML bool
JSON bool
TOML bool
Format string
}

func Dump(opts *Options, args DumpArgs) {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/dump.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

Expand Down
Loading