Skip to content

Commit

Permalink
fix: add proper error when missing rules file
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Nov 9, 2020
1 parent 9e56ed3 commit 094a3b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"os"

"github.com/spf13/cobra"

Expand All @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions internal/rules/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 094a3b5

Please sign in to comment.