Skip to content

Commit

Permalink
feat: config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Dec 5, 2023
1 parent 258f7a0 commit bcf191e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cmd/vale/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func fetch(src, dst string) error {
}

func install(args []string, flags *core.CLIFlags) error {
cfg, _, err := core.ReadPipeline("ini", flags, false)
cfg, err := core.ReadPipeline(flags, false)
if err != nil {
return err
}
Expand Down
62 changes: 13 additions & 49 deletions cmd/vale/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -37,16 +36,16 @@ var commandInfo = map[string]string{

// Actions are the available CLI commands.
var Actions = map[string]func(args []string, flags *core.CLIFlags) error{
"ls-config": printConfig,
"ls-metrics": printMetrics,
"get-config": getConfig,
"write-config": writeConfig,
"dc": printConfig,
"tag": runTag,
"compile": compileRule,
"run": runRule,
"sync": sync,
"fix": fix,
"ls-config": printConfig,
"ls-metrics": printMetrics,
"dc": printConfig,
"tag": runTag,
"sync": sync,
"fix": fix,

// private
"compile": compileRule,
"run": runRule,
}

func fix(args []string, flags *core.CLIFlags) error {
Expand All @@ -63,7 +62,7 @@ func fix(args []string, flags *core.CLIFlags) error {
alert = string(b)
}

cfg, _, err := core.ReadPipeline("ini", flags, false)
cfg, err := core.ReadPipeline(flags, false)
if err != nil {
return err
}
Expand All @@ -77,7 +76,7 @@ func fix(args []string, flags *core.CLIFlags) error {
}

func sync(_ []string, flags *core.CLIFlags) error {
cfg, _, err := core.ReadPipeline("ini", flags, true)
cfg, err := core.ReadPipeline(flags, true)
if err != nil {
return err
} else if err = initPath(cfg); err != nil {
Expand Down Expand Up @@ -110,49 +109,14 @@ func sync(_ []string, flags *core.CLIFlags) error {
}

func printConfig(_ []string, flags *core.CLIFlags) error {
cfg, _, err := core.ReadPipeline("ini", flags, false)
cfg, err := core.ReadPipeline(flags, false)
if err != nil {
return err
}
fmt.Println(cfg.String())
return nil
}

func getConfig(_ []string, flags *core.CLIFlags) error {
var out bytes.Buffer

_, sourced, err := core.ReadPipeline("ini", flags, false)
if err != nil {
return err
}

// Remove empty sections.
for _, section := range sourced.Sections() {
if len(section.Keys()) == 0 {
sourced.DeleteSection(section.Name())
}
}

if _, err = sourced.WriteTo(&out); err != nil {
return err
}

return printJSON(out.String())
}

func writeConfig(ini []string, flags *core.CLIFlags) error {
cfg, _, err := core.ReadPipeline("ini", flags, false)
if err != nil {
return err
}

if len(ini) == 0 {
return errors.New("no data provided")
}

return os.WriteFile(cfg.RootINI, []byte(ini[0]), os.ModePerm)
}

func printMetrics(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("ls-metrics", errors.New("one argument expected"))
Expand Down
3 changes: 1 addition & 2 deletions cmd/vale/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ var hidden = []string{
"compile",
"run",
"fix",
"get-config",
"write-config",
"verify",
}

// PrintIntro shows basic usage / getting started info.
Expand Down
2 changes: 1 addition & 1 deletion cmd/vale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func main() {
}
}

config, _, err := core.ReadPipeline("ini", &Flags, false)
config, err := core.ReadPipeline(&Flags, false)
if err != nil {
handleError(err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/core/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func shadowLoad(source interface{}, others ...interface{}) (*ini.File, error) {
SpaceBeforeInlineComment: true}, source, others...)
}

func loadStdin(src string, cfg *Config, dry bool) (*ini.File, error) {
uCfg, err := shadowLoad([]byte(src))
if err != nil {
return nil, NewE100("loadStdin", err)
}
return processConfig(uCfg, cfg, []string{}, dry)
}

func loadINI(cfg *Config, dry bool) (*ini.File, error) {
var uCfg *ini.File
var sources []string
Expand Down
61 changes: 39 additions & 22 deletions internal/core/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,67 @@ import (
"github.com/errata-ai/ini"
)

func validateFlags(cfg *Config) error {
if cfg.Flags.Path != "" && !FileExists(cfg.Flags.Path) {
return NewE100(
"--config",
fmt.Errorf("path '%s' does not exist", cfg.Flags.Path))
}
return nil
}
type ConfigSrc int

func ReadPipeline(provider string, flags *CLIFlags, dry bool) (*Config, *ini.File, error) {
var sourced *ini.File
const (
FileSrc ConfigSrc = iota
StringSrc
)

func ReadPipeline(flags *CLIFlags, dry bool) (*Config, error) {
config, err := NewConfig(flags)
if err != nil {
return config, nil, err
return config, err
} else if err = validateFlags(config); err != nil {
return config, nil, err
return config, err
}

sourced, err = from(provider, config, dry)
_, err = FromFile(config, dry)
if err != nil {
return config, nil, err
return config, err
}

sources, err := pipeConfig(config)
if err != nil {
return config, nil, err
return config, err
}

if len(sources) > 0 {
config.Flags.Sources = strings.Join(sources, ",")

sourced, err = from(provider, config, dry)
_, err = FromFile(config, dry)
if err != nil {
return config, nil, err
return config, err
}
}

return config, sourced, nil
return config, nil
}

// from updates an existing configuration with values from a user-provided
// from updates an existing configuration with values From a user-provided
// source.
func from(provider string, cfg *Config, dry bool) (*ini.File, error) {
func from(provider ConfigSrc, src string, cfg *Config, dry bool) (*ini.File, error) {
switch provider {
case "ini":
case FileSrc:
return loadINI(cfg, dry)
case StringSrc:
return loadStdin(src, cfg, dry)
default:
return nil, NewE100(
"source/From", fmt.Errorf("unknown provider '%s'", provider))
"source/From", fmt.Errorf("unknown provider '%v'", provider))
}
}

// FromFile loads an INI configuration from a file.
func FromFile(cfg *Config, dry bool) (*ini.File, error) {
return from(FileSrc, "", cfg, dry)
}

// FromString loads an INI configuration from a string.
func FromString(src string, cfg *Config, dry bool) (*ini.File, error) {
return from(StringSrc, src, cfg, dry)
}

// FindAsset tries to locate a Vale-related resource by looking in the
// user-defined StylesPath.
func FindAsset(cfg *Config, path string) string {
Expand All @@ -75,3 +83,12 @@ func FindAsset(cfg *Config, path string) string {

return determinePath(cfg.Flags.Path, path)
}

func validateFlags(cfg *Config) error {
if cfg.Flags.Path != "" && !FileExists(cfg.Flags.Path) {
return NewE100(
"--config",
fmt.Errorf("path '%s' does not exist", cfg.Flags.Path))
}
return nil
}

0 comments on commit bcf191e

Please sign in to comment.