Skip to content

Commit

Permalink
Fixes execution not stopping after help command. Fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
kyallanum committed Jan 1, 2024
1 parent 1f727fd commit 83af6b0
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
27 changes: 20 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,41 @@ var rootCmd = &cobra.Command{
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(configFile) == 0 {
return fmt.Errorf(`required flag(s) "config" not set`)
return fmt.Errorf("exiting: required flag(s) \"config\" not set")
}
if len(logFile) == 0 {
return fmt.Errorf(`required flag(s) "log-file" not set`)
return fmt.Errorf("exiting: required flag(s) \"log-file\" not set")
}
return nil
},
}

func Execute(logger *logrus.Logger) error {
func Execute() error {
errCheck := func(err error) {
if err != nil {
panic(err)
}
}

logger := logger_pkg.New()
defer func() {
if err := recover(); err != nil {
logger.Fatalf("An error occured: \n\t%s", err)
}
}()

err := rootCmd.Execute()
errCheck(err)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
} else if rootCmd.Flags().Changed("help") {
os.Exit(1)
}

if logOutput != "" {
logger_pkg.AddFileLogger(logger, logOutput)
}
errCheck(err)

logger.Info("Athena v1.0.0 Starting")

Expand All @@ -67,8 +80,8 @@ func Execute(logger *logrus.Logger) error {

func init() {
rootCmd.Flags().StringVarP(&configFile, "config", "c", os.Getenv("ATHENA_CONFIG_FILE"), "")
rootCmd.Flags().StringVarP(&logFile, "file", "f", os.Getenv("ATHENA_LOG_FILE"), "")
rootCmd.Flags().StringVarP(&logOutput, "log-output", "l", os.Getenv("ATHENA_LOG_OUTPUT"), "")
rootCmd.Flags().StringVarP(&logFile, "file", "l", os.Getenv("ATHENA_LOG_FILE"), "")
rootCmd.Flags().StringVarP(&logOutput, "log-output", "o", os.Getenv("ATHENA_LOG_OUTPUT"), "")
}

func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration, logger *logrus.Logger) (*library.Library, error) {
Expand All @@ -91,7 +104,7 @@ func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration,

logger.Info("Resolving Log File")
for i := 0; i < len(configuration.Rules); i++ {
currentRuleData, err := config.ResolveRule(contents, &configuration.Rules[i])
currentRuleData, err := config.ResolveRule(contents, &configuration.Rules[i], logger)
if err != nil {
return nil, wrapError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/apt-term-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Apt Terminal",
"rules": [{
"name": "GetPackage",
"printLog": false,
"printLog": true,
"searchTerms": [
"Preparing to unpack ...[\\w/]*/[\\d\\-]*(?<library_name>[\\w\\W]+?)_[\\w\\W]+? ...",
"Unpacking (?<full_package_name>{{library_name}}[:\\w]*?) \\((?<library_version>[\\w\\W]+?)\\) over \\([\\w\\W]+?\\) ...",
Expand Down
13 changes: 1 addition & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ package main

import (
"github.com/kyallanum/athena/cmd"
"github.com/kyallanum/athena/models/logger"
)

func main() {
logger := logger.New()

defer func() {
if err := recover(); err != nil {
logger.Fatalf("\nAn error occured: \n\t%s", err)
}
}()

if err := cmd.Execute(logger); err != nil {
panic(err)
}
cmd.Execute()
}
13 changes: 11 additions & 2 deletions models/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"bufio"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -11,6 +12,7 @@ import (
"testing"

logs "github.com/kyallanum/athena/models/logs"
"github.com/sirupsen/logrus"
)

func TestTranslateRegex(t *testing.T) {
Expand Down Expand Up @@ -139,6 +141,9 @@ func TestCreateConfigurationFromWeb(t *testing.T) {
}

func TestResolveRule(t *testing.T) {
logger := logrus.New()
logger.SetOutput(io.Discard)

os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()

Expand All @@ -148,7 +153,7 @@ func TestResolveRule(t *testing.T) {

currentRule := currentConfig.Rules[0]

ruleData, err := ResolveRule(logFile, &currentRule)
ruleData, err := ResolveRule(logFile, &currentRule, logger)
if err != nil {
t.Errorf("An error was returned when one should not have been: \n\t%s", err.Error())
}
Expand All @@ -166,12 +171,16 @@ func TestResolveRuleBadRule(t *testing.T) {
t.Errorf("%s", err.(error).Error())
}
}()

logger := logrus.New()
logger.SetOutput(io.Discard)

os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")

currentRule := &Rule{}

ResolveRule(logFile, currentRule)
ResolveRule(logFile, currentRule, logger)
}
5 changes: 3 additions & 2 deletions models/config/rule_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
"github.com/sirupsen/logrus"
)

func ResolveRule(contents *logs.LogFile, rule *Rule) (*library.RuleData, error) {
func ResolveRule(contents *logs.LogFile, rule *Rule, logger *logrus.Logger) (*library.RuleData, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to resolve rule %s: \n\t%w", rule.Name, err)
}
Expand All @@ -18,7 +19,7 @@ func ResolveRule(contents *logs.LogFile, rule *Rule) (*library.RuleData, error)
currentRuleData := library.NewRuleData()

for !allEntriesFound {
currentSearchTermData, err := resolveSearchTerms(contents, rule, &linesResolved)
currentSearchTermData, err := resolveSearchTerms(contents, rule, &linesResolved, logger)
if err != nil {
return nil, wrapError(err)
}
Expand Down
5 changes: 3 additions & 2 deletions models/config/searchterm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
"github.com/sirupsen/logrus"
)

func resolveSearchTerms(logFile *logs.LogFile, rule *Rule, linesResolved *[]int) (*library.SearchTermData, error) {
func resolveSearchTerms(logFile *logs.LogFile, rule *Rule, linesResolved *[]int, logger *logrus.Logger) (*library.SearchTermData, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to resolve search terms for rule %s: \n\t%w", rule.Name, err)
}
Expand Down Expand Up @@ -47,7 +48,7 @@ func resolveSearchTerms(logFile *logs.LogFile, rule *Rule, linesResolved *[]int)
}

if rule.PrintLog {
fmt.Printf("%d: %s\n", fileIndex+1, currentLine)
logger.Infof("%d: %s", fileIndex+1, currentLine)
}

*linesResolved = append(*linesResolved, fileIndex)
Expand Down
3 changes: 1 addition & 2 deletions models/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func AddFileLogger(logger *logrus.Logger, fileName string) {
if err != nil {
logger.Fatalf("An error occurred: \n\t%s", err.Error())
}
defer file.Close()

FileHook := &FormatterHook{
Writer: file,
Expand All @@ -32,7 +31,7 @@ func AddFileLogger(logger *logrus.Logger, fileName string) {
logger.AddHook(FileHook)
}

func New(fileName ...string) *logrus.Logger {
func New() *logrus.Logger {
newLogger := logrus.New()
newLogger.SetOutput(io.Discard)
newLogger.SetLevel(logrus.InfoLevel)
Expand Down
2 changes: 1 addition & 1 deletion models/logger/stderr_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ type StderrFormatter struct {

func (formatter *StderrFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := entry.Time.Format(formatter.TimestampFormat)
return []byte(fmt.Sprintf("%s Error: %s\n", timestamp, entry.Message)), nil
return []byte(fmt.Sprintf("%s Error: %s", timestamp, entry.Message)), nil
}

0 comments on commit 83af6b0

Please sign in to comment.