From 094a3b56faca84a16c097e3dc5a4bca998386594 Mon Sep 17 00:00:00 2001 From: Doron Cohen Date: Mon, 9 Nov 2020 08:51:47 +0200 Subject: [PATCH] fix: add proper error when missing rules file --- cmd/clean.go | 5 +++++ internal/rules/config.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/cmd/clean.go b/cmd/clean.go index 99ac2cf..670d736 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -2,6 +2,7 @@ package cmd import ( "log" + "os" "github.com/spf13/cobra" @@ -23,6 +24,10 @@ var cleanCmd = &cobra.Command{ _, err := rules.LoadRulesConfig(rulesFilePath) if err != nil { + if _, rulesMissing := err.(*rules.MissingRulesFile); rulesMissing { + log.Println("Couldn't find rules file. Please run `antidot update`.") + os.Exit(2) + } log.Fatalln("Failed to read rules file: ", err) } diff --git a/internal/rules/config.go b/internal/rules/config.go index 227d269..75409a0 100644 --- a/internal/rules/config.go +++ b/internal/rules/config.go @@ -3,11 +3,18 @@ package rules import ( "io/ioutil" "log" + "os" "github.com/mitchellh/mapstructure" "gopkg.in/yaml.v2" ) +type MissingRulesFile struct{} + +func (e *MissingRulesFile) Error() string { + return "Rules file is missing" +} + type RulesConfig struct { Version int Rules []Rule @@ -19,6 +26,9 @@ func LoadRulesConfig(filepath string) (RulesConfig, error) { log.Printf("Loading rules config file %s", filepath) rulesBytes, err := ioutil.ReadFile(filepath) if err != nil { + if os.IsNotExist(err) { + return RulesConfig{}, &MissingRulesFile{} + } return RulesConfig{}, err }