feat: support config validate
parent
9277635a5c
commit
8adc79815b
|
@ -86,6 +86,19 @@ func (a *action) runDiff(c *cli.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *action) runValidate(c *cli.Context) error {
|
||||||
|
cfg, err := a.loadConfig(c, validateCommand)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cfg.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("config: failed to validate: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *action) loadConfig(c *cli.Context, command string) (config.Config, error) {
|
func (a *action) loadConfig(c *cli.Context, command string) (config.Config, error) {
|
||||||
a.getFlags(c)
|
a.getFlags(c)
|
||||||
a.log("start %s with flags %+v\n", command, a.flags)
|
a.log("start %s with flags %+v\n", command, a.flags)
|
||||||
|
|
|
@ -29,6 +29,9 @@ const (
|
||||||
diffCommand = "diff"
|
diffCommand = "diff"
|
||||||
diffUsage = "diff dotfiles with user configs"
|
diffUsage = "diff dotfiles with user configs"
|
||||||
|
|
||||||
|
validateCommand = "validate"
|
||||||
|
validateUsage = "validate config"
|
||||||
|
|
||||||
verboseFlag = "verbose"
|
verboseFlag = "verbose"
|
||||||
verboseUsage = "show what is going on"
|
verboseUsage = "show what is going on"
|
||||||
|
|
||||||
|
@ -44,6 +47,7 @@ var (
|
||||||
downloadAliases = []string{"d"}
|
downloadAliases = []string{"d"}
|
||||||
cleanAliases = []string{"c"}
|
cleanAliases = []string{"c"}
|
||||||
diffAliases = []string{"df"}
|
diffAliases = []string{"df"}
|
||||||
|
validateAliases = []string{"vl"}
|
||||||
)
|
)
|
||||||
|
|
||||||
// denyOSes contains OS which is not supported
|
// denyOSes contains OS which is not supported
|
||||||
|
@ -139,6 +143,18 @@ func NewApp() *App {
|
||||||
},
|
},
|
||||||
Action: a.runDiff,
|
Action: a.runDiff,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: validateCommand,
|
||||||
|
Aliases: validateAliases,
|
||||||
|
Usage: validateUsage,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: verboseFlag,
|
||||||
|
Usage: verboseUsage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: a.runValidate,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: a.runHelp,
|
Action: a.runHelp,
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ type Config interface {
|
||||||
Clean() error
|
Clean() error
|
||||||
Diff() error
|
Diff() error
|
||||||
Download() error
|
Download() error
|
||||||
|
Validate() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigApps struct {
|
type ConfigApps struct {
|
||||||
|
|
|
@ -66,3 +66,7 @@ func (c *configDemo) Clean() error {
|
||||||
func (c *configDemo) Diff() error {
|
func (c *configDemo) Diff() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *configDemo) Validate() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -11,6 +12,8 @@ import (
|
||||||
"github.com/make-go-great/diff-go"
|
"github.com/make-go-great/diff-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrConfigInvalid = errors.New("config invalid")
|
||||||
|
|
||||||
type configReal struct {
|
type configReal struct {
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
ConfigApps
|
ConfigApps
|
||||||
|
@ -105,22 +108,6 @@ func (c *configReal) Clean() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configReal) Diff() error {
|
|
||||||
for _, app := range c.Apps {
|
|
||||||
for _, p := range app.Paths {
|
|
||||||
if p.External == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := diff.Diff(p.Internal, p.External); err != nil {
|
|
||||||
return fmt.Errorf("failed to compare %s with %s: %w", p.Internal, p.External, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
|
func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
|
||||||
files, err := os.ReadDir(configDirPath)
|
files, err := os.ReadDir(configDirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -145,3 +132,35 @@ func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
|
||||||
|
|
||||||
return unusedDirs, nil
|
return unusedDirs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *configReal) Diff() error {
|
||||||
|
for _, app := range c.Apps {
|
||||||
|
for _, p := range app.Paths {
|
||||||
|
if p.External == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := diff.Diff(p.Internal, p.External); err != nil {
|
||||||
|
return fmt.Errorf("failed to compare %s with %s: %w", p.Internal, p.External, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *configReal) Validate() error {
|
||||||
|
for _, app := range c.Apps {
|
||||||
|
for _, p := range app.Paths {
|
||||||
|
if p.Internal == "" {
|
||||||
|
return fmt.Errorf("empty internal app [%s]: %w", app, ErrConfigInvalid)
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.External == "" && p.URL == "" {
|
||||||
|
return fmt.Errorf("empty external and url app [%s]: %w", app, ErrConfigInvalid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue