make config validate exit with non-zero code on failure (#7855)

Этот коммит содержится в:
Chris
2017-11-17 08:27:26 -06:00
коммит произвёл enahum
родитель eb1a00ef5f
Коммит 065d8e9731
3 изменённых файлов: 12 добавлений и 23 удалений

Просмотреть файл

@@ -20,7 +20,8 @@ var configCmd = &cobra.Command{
var validateConfigCmd = &cobra.Command{ var validateConfigCmd = &cobra.Command{
Use: "validate", Use: "validate",
Short: "Validate config file", Short: "Validate config file",
Run: configValidateCmdF, Long: "If the config file is valid, this command will output a success message and have a zero exit code. If it is invalid, this command will output an error and have a non-zero exit code.",
RunE: configValidateCmdF,
} }
func init() { func init() {
@@ -29,39 +30,35 @@ func init() {
) )
} }
func configValidateCmdF(cmd *cobra.Command, args []string) { func configValidateCmdF(cmd *cobra.Command, args []string) error {
utils.TranslationsPreInit() utils.TranslationsPreInit()
filePath, err := cmd.Flags().GetString("config") filePath, err := cmd.Flags().GetString("config")
if err != nil { if err != nil {
CommandPrintErrorln(err) return err
return
} }
filePath = utils.FindConfigFile(filePath) filePath = utils.FindConfigFile(filePath)
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
CommandPrintErrorln(err) return err
return
} }
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
config := model.Config{} config := model.Config{}
err = decoder.Decode(&config) err = decoder.Decode(&config)
if err != nil { if err != nil {
CommandPrintErrorln(err) return err
return
} }
if _, err := file.Stat(); err != nil { if _, err := file.Stat(); err != nil {
CommandPrintErrorln(err) return err
return
} }
if err := config.IsValid(); err != nil { if err := config.IsValid(); err != nil {
CommandPrintErrorln(errors.New(utils.T(err.Id))) return errors.New(utils.T(err.Id))
return
} }
CommandPrettyPrintln("The document is valid") CommandPrettyPrintln("The document is valid")
return nil
} }

Просмотреть файл

@@ -25,5 +25,6 @@ func TestConfigValidate(t *testing.T) {
config.SetDefaults() config.SetDefaults()
require.NoError(t, ioutil.WriteFile(path, []byte(config.ToJson()), 0600)) require.NoError(t, ioutil.WriteFile(path, []byte(config.ToJson()), 0600))
assert.Contains(t, checkCommand(t, "--config", path, "config", "validate"), "The document is valid") assert.Error(t, runCommand(t, "--config", "foo.json", "config", "validate"))
assert.NoError(t, runCommand(t, "--config", path, "config", "validate"))
} }

Просмотреть файл

@@ -4,7 +4,6 @@
package main package main
import ( import (
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -25,17 +24,9 @@ func TestConfigFlag(t *testing.T) {
configPath := filepath.Join(dir, "foo.json") configPath := filepath.Join(dir, "foo.json")
require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600)) require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))
os.Mkdir(filepath.Join(dir, "i18n"), 0700)
i18n, ok := utils.FindDir("i18n") i18n, ok := utils.FindDir("i18n")
require.True(t, ok) require.True(t, ok)
en, err := os.Open(filepath.Join(i18n, "en.json")) require.NoError(t, utils.CopyDir(i18n, filepath.Join(dir, "i18n")))
require.NoError(t, err)
defer en.Close()
dest, err := os.OpenFile(filepath.Join(dir, "i18n", "en.json"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
require.NoError(t, err)
defer dest.Close()
_, err = io.Copy(dest, en)
require.NoError(t, err)
prevDir, err := os.Getwd() prevDir, err := os.Getwd()
require.NoError(t, err) require.NoError(t, err)