Skip to content

Commit

Permalink
feat: add toml dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed May 22, 2023
1 parent 6bba269 commit 5f58232
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 42 deletions.
5 changes: 5 additions & 0 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ func newDumpCmd(opts *lefthook.Options) *cobra.Command {
"dump in JSON format",
)

dumpCmd.Flags().BoolVarP(
&dumpArgs.TOML, "toml", "t", false,
"dump in TOML format",
)

return &dumpCmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.6
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
24 changes: 12 additions & 12 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import (
var errFilesIncompatible = errors.New("One of your runners contains incompatible file types")

type Command struct {
Run string `mapstructure:"run" yaml:",omitempty" json:"run,omitempty"`
Run string `mapstructure:"run" yaml:",omitempty" toml:"run,omitempty" json:"run,omitempty"`

Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty"`
Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty"`
Glob string `mapstructure:"glob" yaml:",omitempty" json:"glob,omitempty"`
Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty"`
Skip interface{} `mapstructure:"skip" yaml:",omitempty" toml:"skip,omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" toml:"only,omitempty,inline" json:"only,omitempty"`
Tags []string `mapstructure:"tags" yaml:",omitempty" toml:"tags,omitempty" json:"tags,omitempty"`
Glob string `mapstructure:"glob" yaml:",omitempty" toml:"glob,omitempty" json:"glob,omitempty"`
Files string `mapstructure:"files" yaml:",omitempty" toml:"files,omitempty" json:"files,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" toml:"env,omitempty" json:"env,omitempty"`

Root string `mapstructure:"root" yaml:",omitempty" json:"root,omitempty"`
Exclude string `mapstructure:"exclude" yaml:",omitempty" json:"exclude,omitempty"`
Root string `mapstructure:"root" yaml:",omitempty" toml:"root,omitempty" json:"root,omitempty"`
Exclude string `mapstructure:"exclude" yaml:",omitempty" toml:"exclude,omitempty" json:"exclude,omitempty"`

FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty"`
FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" toml:"fail_text,omitempty" json:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" toml:"interactive,omitempty" json:"interactive,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" toml:"stage_fixed,omitempty" json:"stage_fixed,omitempty"`
}

func (c Command) Validate() error {
Expand Down
65 changes: 55 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

toml "github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"

"github.com/evilmartians/lefthook/internal/log"
Expand All @@ -13,15 +14,15 @@ import (
const dumpIndent = 2

type Config struct {
Colors interface{} `mapstructure:"colors" yaml:"colors,omitempty" json:"colors,omitempty"`
Extends []string `mapstructure:"extends" yaml:"extends,omitempty" json:"extends,omitempty"`
Remote Remote `mapstructure:"remote" yaml:"remote,omitempty" json:"remote,omitempty"`
MinVersion string `mapstructure:"min_version" yaml:"min_version,omitempty" json:"min_version,omitempty"`
SkipOutput []string `mapstructure:"skip_output" yaml:"skip_output,omitempty" json:"skip_output,omitempty"`
SourceDir string `mapstructure:"source_dir" yaml:"source_dir,omitempty" json:"source_dir,omitempty"`
SourceDirLocal string `mapstructure:"source_dir_local" yaml:"source_dir_local,omitempty" json:"source_dir_local,omitempty"`
Rc string `mapstructure:"rc" yaml:"rc,omitempty" json:"rc,omitempty"`
NoTTY bool `mapstructure:"no_tty" yaml:"no_tty,omitempty" json:"no_tty,omitempty"`
Colors interface{} `mapstructure:"colors" yaml:"colors,omitempty" toml:"colors,omitempty" json:"colors,omitempty"`
Extends []string `mapstructure:"extends" yaml:"extends,omitempty" toml:"extends,omitempty" json:"extends,omitempty"`
Remote Remote `mapstructure:"remote" yaml:"remote,omitempty" toml:"remote,omitempty" json:"remote,omitempty"`
MinVersion string `mapstructure:"min_version" yaml:"min_version,omitempty" toml:"min_version,omitempty" json:"min_version,omitempty"`
SkipOutput []string `mapstructure:"skip_output" yaml:"skip_output,omitempty" toml:"skip_output,omitempty" json:"skip_output,omitempty"`
SourceDir string `mapstructure:"source_dir" yaml:"source_dir,omitempty" toml:"source_dir,omitempty" json:"source_dir,omitempty"`
SourceDirLocal string `mapstructure:"source_dir_local" yaml:"source_dir_local,omitempty" toml:"source_dir_local,omitempty" json:"source_dir_local,omitempty"`
Rc string `mapstructure:"rc" yaml:"rc,omitempty" toml:"rc,omitempty" json:"rc,omitempty"`
NoTTY bool `mapstructure:"no_tty" yaml:"no_tty,omitempty" toml:"no_tty,omitempty" json:"no_tty,omitempty"`

Hooks map[string]*Hook `yaml:",inline" json:"-"`
}
Expand All @@ -30,11 +31,15 @@ func (c *Config) Validate() error {
return version.CheckCovered(c.MinVersion)
}

func (c *Config) Dump(asJSON bool) error {
func (c *Config) Dump(asJSON bool, asTOML bool) error {
if asJSON {
return c.dumpJSON()
}

if asTOML {
return c.dumpTOML()
}

return c.dumpYAML()
}

Expand Down Expand Up @@ -82,3 +87,43 @@ func (c *Config) dumpJSON() error {

return nil
}

func (c *Config) dumpTOML() error {
// type ConfigForMarshalling *Config
// res, err := toml.Marshal(ConfigForMarshalling(c))
// if err != nil {
// return err
// }

// var rawMarshalled map[string]interface{}
// if err = json.Unmarshal(res, &rawMarshalled); err != nil {
// return err
// }

// for hook, contents := range c.Hooks {
// var hookMarshalled interface{}
// hookMarshalled, err = toml.Marshal(contents)
// if err != nil {
// return err
// }
// rawMarshalled[hook] = hookMarshalled
// }

// res, err = toml.Marshal(rawMarshalled)
// if err != nil {
// return err
// }

// log.Info(string(res))

// return nil
encoder := toml.NewEncoder(os.Stdout)
encoder.SetIndentTables(false)

err := encoder.Encode(c)
if err != nil {
return err
}

return nil
}
20 changes: 10 additions & 10 deletions internal/config/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ type Hook struct {
// Should be unmarshalled with `mapstructure:"commands"`
// But replacing '{cmd}' is still an issue
// Unmarshaling it manually, so omit auto unmarshaling
Commands map[string]*Command `mapstructure:"?" yaml:",omitempty" json:"commands,omitempty"`
Commands map[string]*Command `mapstructure:"?" yaml:",omitempty" toml:"commands,omitempty" json:"commands,omitempty"`

// Should be unmarshalled with `mapstructure:"scripts"`
// But parsing keys with dots in it is still an issue: https://github.com/spf13/viper/issues/324
// Unmarshaling it manually, so omit auto unmarshaling
Scripts map[string]*Script `mapstructure:"?" yaml:",omitempty" json:"scripts,omitempty"`

Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty"`
Parallel bool `mapstructure:"parallel" yaml:",omitempty" json:"parallel,omitempty"`
Piped bool `mapstructure:"piped" yaml:",omitempty" json:"piped,omitempty"`
ExcludeTags []string `mapstructure:"exclude_tags" yaml:"exclude_tags,omitempty" json:"exclude_tags,omitempty"`
Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty"`
Follow bool `mapstructure:"follow" yaml:",omitempty" json:"follow,omitempty"`
Scripts map[string]*Script `mapstructure:"?" yaml:",omitempty" toml:"scripts,omitempty" json:"scripts,omitempty"`

Files string `mapstructure:"files" yaml:",omitempty" toml:"files,omitempty" json:"files,omitempty"`
Parallel bool `mapstructure:"parallel" yaml:",omitempty" toml:"parallel,omitempty" json:"parallel,omitempty"`
Piped bool `mapstructure:"piped" yaml:",omitempty" toml:"piped,omitempty" json:"piped,omitempty"`
ExcludeTags []string `mapstructure:"exclude_tags" yaml:"exclude_tags,omitempty" toml:"exclude_tags,omitempty" json:"exclude_tags,omitempty"`
Skip interface{} `mapstructure:"skip" yaml:",omitempty" toml:"skip,omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" toml:"only,omitempty" json:"only,omitempty"`
Follow bool `mapstructure:"follow" yaml:",omitempty" toml:"follow,omitempty" json:"follow,omitempty"`
}

func (h *Hook) Validate() error {
Expand Down
16 changes: 8 additions & 8 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
)

type Script struct {
Runner string `mapstructure:"runner" yaml:",omitempty" json:"runner,omitempty"`
Runner string `mapstructure:"runner" yaml:",omitempty" toml:"runner,omitempty" json:"runner,omitempty"`

Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty"`
Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty"`
Skip interface{} `mapstructure:"skip" yaml:",omitempty" toml:"skip,omitempty" json:"skip,omitempty"`
Only interface{} `mapstructure:"only" yaml:",omitempty" toml:"only,omitempty,inline" json:"only,omitempty"`
Tags []string `mapstructure:"tags" yaml:",omitempty" toml:"tags,omitempty" json:"tags,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" toml:"env,omitempty" json:"env,omitempty"`

FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty"`
FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" toml:"fail_text,omitempty" json:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" toml:"interactive,omitempty" json:"interactive,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" toml:"stage_fixed,omitempty" json:"stage_fixed,omitempty"`
}

func (s Script) DoSkip(gitState git.State) bool {
Expand Down
3 changes: 2 additions & 1 deletion internal/lefthook/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type DumpArgs struct {
JSON bool
TOML bool
}

func Dump(opts *Options, args DumpArgs) {
Expand All @@ -22,7 +23,7 @@ func Dump(opts *Options, args DumpArgs) {
return
}

if err := cfg.Dump(args.JSON); err != nil {
if err := cfg.Dump(args.JSON, args.TOML); err != nil {
log.Errorf("couldn't dump config: %s\n", err)
return
}
Expand Down

0 comments on commit 5f58232

Please sign in to comment.