Refactor code style for improved readability and consistency.
This commit is contained in:
1 parent
463c013056
commit
94bfc87e9d
4 files changed
+18
-56
No files matched your search
+5
-15
@@ -52,18 +52,14 @@ func run(args []string, stdout, stderr io.Writer) int {
|
||||
flags, configPath := newFlagSet("dibs", stderr)
|
||||
flags.Bool("headless", false, "run without the TUI, log to stdout")
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 2
|
||||
}
|
||||
if err := flags.Parse(args); err != nil { return 2 }
|
||||
|
||||
if flags.NArg() > 0 {
|
||||
_, _ = fmt.Fprintf(stderr, "dibs: unknown command %q\n\n%s", flags.Arg(0), usage)
|
||||
return 2
|
||||
}
|
||||
|
||||
if _, ok := loadConfig(*configPath, stderr); !ok {
|
||||
return 1
|
||||
}
|
||||
if _, ok := loadConfig(*configPath, stderr); !ok { return 1 }
|
||||
|
||||
_, _ = fmt.Fprintln(stderr, "dibs: watching is not implemented yet")
|
||||
return 1
|
||||
@@ -76,14 +72,10 @@ func runConfig(args []string, stdout, stderr io.Writer) int {
|
||||
}
|
||||
|
||||
flags, configPath := newFlagSet("dibs config validate", stderr)
|
||||
if err := flags.Parse(args[1:]); err != nil {
|
||||
return 2
|
||||
}
|
||||
if err := flags.Parse(args[1:]); err != nil { return 2 }
|
||||
|
||||
cfg, ok := loadConfig(*configPath, stderr)
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
if !ok { return 1 }
|
||||
|
||||
urls := 0
|
||||
for _, it := range cfg.Items {
|
||||
@@ -98,9 +90,7 @@ func runConfig(args []string, stdout, stderr io.Writer) int {
|
||||
}
|
||||
|
||||
func plural(n int, noun string) string {
|
||||
if n == 1 {
|
||||
return "1 " + noun
|
||||
}
|
||||
if n == 1 { return "1 " + noun }
|
||||
return fmt.Sprintf("%d %ss", n, noun)
|
||||
}
|
||||
|
||||
|
||||
@@ -130,9 +130,7 @@ type Options struct {
|
||||
// DefaultPath returns dibs.toml in the user config dir.
|
||||
func DefaultPath() (string, error) {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err != nil { return "", err }
|
||||
return filepath.Join(dir, "dibs", "dibs.toml"), nil
|
||||
}
|
||||
|
||||
@@ -140,14 +138,10 @@ func DefaultPath() (string, error) {
|
||||
// are returned together as Errors.
|
||||
func Load(path string, opts Options) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil { return nil, err }
|
||||
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil { return nil, err }
|
||||
|
||||
if opts.LookupEnv == nil {
|
||||
opts.LookupEnv = os.LookupEnv
|
||||
@@ -195,9 +189,7 @@ func Load(path string, opts Options) (*Config, error) {
|
||||
}
|
||||
|
||||
v.validate(cfg, opts)
|
||||
if len(v.errs) > 0 {
|
||||
return nil, v.errs
|
||||
}
|
||||
if len(v.errs) > 0 { return nil, v.errs }
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
@@ -84,9 +84,7 @@ func (ps positions) add(p *unstable.Parser, path string, n *unstable.Node) {
|
||||
// that a missing key points at the table it belongs in.
|
||||
func (ps positions) find(path string) (unstable.Position, bool) {
|
||||
for path != "" {
|
||||
if pos, ok := ps[path]; ok {
|
||||
return pos, true
|
||||
}
|
||||
if pos, ok := ps[path]; ok { return pos, true }
|
||||
|
||||
i := strings.LastIndexAny(path, ".[")
|
||||
if i < 0 {
|
||||
@@ -123,9 +121,7 @@ func keyParts(it unstable.Iterator) ([]string, *unstable.Node) {
|
||||
|
||||
for it.Next() {
|
||||
n := it.Node()
|
||||
if first == nil {
|
||||
first = n
|
||||
}
|
||||
if first == nil { first = n }
|
||||
keys = append(keys, string(n.Data))
|
||||
}
|
||||
|
||||
|
||||
@@ -59,9 +59,7 @@ type Error struct {
|
||||
func (e Error) Error() string {
|
||||
var b strings.Builder
|
||||
b.WriteString(e.File)
|
||||
if e.Line > 0 {
|
||||
_, _ = fmt.Fprintf(&b, ":%d:%d", e.Line, e.Col)
|
||||
}
|
||||
if e.Line > 0 { _, _ = fmt.Fprintf(&b, ":%d:%d", e.Line, e.Col) }
|
||||
|
||||
b.WriteString(": ")
|
||||
if e.Key != "" {
|
||||
@@ -78,9 +76,7 @@ type Errors []Error
|
||||
|
||||
func (es Errors) Error() string {
|
||||
lines := make([]string, len(es))
|
||||
for i, e := range es {
|
||||
lines[i] = e.Error()
|
||||
}
|
||||
for i, e := range es { lines[i] = e.Error() }
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
@@ -93,9 +89,7 @@ type validator struct {
|
||||
|
||||
func (v *validator) errorf(key, format string, args ...any) {
|
||||
e := Error{File: v.file, Key: key, Msg: fmt.Sprintf(format, args...)}
|
||||
if p, ok := v.pos.find(key); ok {
|
||||
e.Line, e.Col = p.Line, p.Column
|
||||
}
|
||||
if p, ok := v.pos.find(key); ok { e.Line, e.Col = p.Line, p.Column }
|
||||
v.errs = append(v.errs, e)
|
||||
}
|
||||
|
||||
@@ -119,17 +113,11 @@ func (v *validator) decodeError(de *toml.DecodeError, msg string) {
|
||||
// types, into TOML terms.
|
||||
func friendlyDecodeMsg(msg string) string {
|
||||
m := typeErrRe.FindStringSubmatch(msg)
|
||||
if m == nil {
|
||||
return msg
|
||||
}
|
||||
if m == nil { return msg }
|
||||
|
||||
want, ok := goTypeNames[m[2]]
|
||||
if !ok {
|
||||
if strings.HasPrefix(m[2], "map[") {
|
||||
want = "a table"
|
||||
} else {
|
||||
return msg
|
||||
}
|
||||
if strings.HasPrefix(m[2], "map[") { want = "a table" } else { return msg }
|
||||
}
|
||||
|
||||
return fmt.Sprintf("expected %s, got a TOML %s", want, strings.ToLower(m[1]))
|
||||
@@ -137,9 +125,7 @@ func friendlyDecodeMsg(msg string) string {
|
||||
|
||||
func (v *validator) validate(cfg *Config, opts Options) {
|
||||
shops := map[string]bool{}
|
||||
for _, b := range opts.Builtins {
|
||||
shops[b] = true
|
||||
}
|
||||
for _, b := range opts.Builtins { shops[b] = true }
|
||||
|
||||
v.plugins(cfg, shops)
|
||||
v.items(cfg, shops, opts.Builtins)
|
||||
@@ -300,9 +286,7 @@ func (v *validator) accounts(cfg *Config, shops map[string]bool, lookupEnv func(
|
||||
}
|
||||
|
||||
for _, f := range []struct{ name, raw string }{{"login_url", acc.LoginURL}, {"cart_url", acc.CartURL}} {
|
||||
if f.raw == "" {
|
||||
continue
|
||||
}
|
||||
if f.raw == "" { continue }
|
||||
|
||||
if msg := checkURL(f.raw); msg != "" {
|
||||
v.errorf(key+"."+f.name, "%s", msg)
|
||||
|
||||
Reference in new issue
Block a user