Skip to content

Commit

Permalink
docs(config): simplify documentation and init flag (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Jun 12, 2024
1 parent c4acc39 commit 1622ac1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
24 changes: 8 additions & 16 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (

configCmd = &cobra.Command{
Use: "config",
Short: "Show configuration information",
Short: "Print the config to stdout",
RunE: runConfig,
}
)
Expand All @@ -26,7 +26,7 @@ func init() {
rootCmd.AddCommand(configCmd)

configCmd.Flags().BoolVarP(&editConfigFlag, "edit", "e", false, "Edit the config in $EDITOR")
configCmd.Flags().BoolVarP(&initConfigFlag, "init", "i", false, "Create an initial config file")
configCmd.Flags().BoolVarP(&initConfigFlag, "init", "i", false, "Print the default config to stdout")
}

func runConfig(cmd *cobra.Command, args []string) error {
Expand All @@ -41,32 +41,24 @@ func runConfig(cmd *cobra.Command, args []string) error {
marshalled, err := yaml.Marshal(config)
if err != nil {
slog.Error("Failed to marshall config", "err", err)
return err
}

fmt.Println(configPathFlag)
fmt.Println(string(marshalled))
fmt.Printf("Config sourced from: %s\n\n%s\n", configPathFlag, marshalled)

return nil
}

func initConfig() error {
slog.Debug("creating initial config file", "path", configPathFlag)

if _, err := os.Stat(configPathFlag); err == nil {
return fmt.Errorf("config file %s already exists", configPathFlag)
}
slog.Debug("printing config file", "path", configPathFlag)

f, err := os.Create(configPathFlag)
marshalled, err := yaml.Marshal(configPkg.Default())
if err != nil {
return err
}
defer f.Close()

if _, err := f.Write([]byte(configPkg.Example)); err != nil {
slog.Error("Failed to marshall config", "err", err)
return err
}

fmt.Printf("Created config file: %s\n", configPathFlag)
fmt.Printf("%s\n", marshalled)

return nil
}
Expand Down
67 changes: 33 additions & 34 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/*
Package config provides a way to load the configuration from a file.
It also comes with a default configuration that can be used if no file is found.
See individual types for more information on the configuration.
Output the default configuration (free of rules) with `gh-not config --init`.
Example rules:
rules:
- name: showcasing conditionals
action: debug
# Filters are run one after the other, like they are joined by 'and'.
# Having 'or' can be done via '(cond1) or (cond2) or ...'.
filters:
- .author.login == "dependabot[bot]"
- >
(.subject.title | contains("something unimportant")) or
(.subject.title | contains("something already done"))
- name: ignore ci failures for the current repo
action: done
filters:
- .repository.full_name == "nobe4/gh-not"
- .reason == "ci_activity"
*/
package config

import (
Expand Down Expand Up @@ -26,38 +53,6 @@ type Cache struct {
Path string `yaml:"path"`
}

// TODO: deduplicate with the defaultCache and defaultEndpoint
// maybe keep in the documentation only?
const Example = `
---
cache:
ttl_in_hours: 1
path: ./cache.json
endpoint:
all: true
max_retry: 10
max_page: 5
rules:
- name: showcasing conditionals
action: debug
# Filters are run one after the other, like they are joined by 'and'.
# Having 'or' can be done via '(cond1) or (cond2) or ...'.
filters:
- .author.login == "dependabot[bot]"
- >
(.subject.title | contains("something unimportant")) or
(.subject.title | contains("something already done"))
- name: ignore ci failures for the current repo
action: done
filters:
- .repository.full_name == "nobe4/gh-not"
- .reason == "ci_activity"
`

var (
defaultCache = Cache{
TTLInHours: 1,
Expand All @@ -71,12 +66,16 @@ var (
}
)

func New(path string) (*Config, error) {
config := &Config{
func Default() *Config {
return &Config{
Cache: defaultCache,
Endpoint: defaultEndpoint,
Keymap: defaultKeymap,
}
}

func New(path string) (*Config, error) {
config := Default()

content, err := os.ReadFile(path)
if err != nil {
Expand Down

0 comments on commit 1622ac1

Please sign in to comment.