Skip to content

Commit

Permalink
Feature/add logging (#12)
Browse files Browse the repository at this point in the history
* Adds logging
* Fixes execution not stopping after help command. Fixes #11
  • Loading branch information
kyallanum authored Jan 1, 2024
1 parent 1f53304 commit 0d42341
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 45 deletions.
24 changes: 19 additions & 5 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package cmd

import (
"io"
"os"
"testing"

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

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

os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()
logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config.json")

_, err := resolveLogFile(logFile, configuration)
_, err := resolveLogFile(logFile, configuration, logger)

if err != nil {
t.Errorf("An error occurred while resolving log file: %s", err)
Expand All @@ -28,47 +33,56 @@ func TestResolveLogFileBadLog(t *testing.T) {
logFile, _ := logs.LoadLogFile("../examples/apt-term-bad.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config.json")

_, err := resolveLogFile(logFile, configuration)
_, err := resolveLogFile(logFile, configuration, nil)

if err.Error() != "log file contains no contents" {
t.Errorf("Error was not properly returned when checking log file contents")
}
}

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

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

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config-bad.json")

_, err := resolveLogFile(logFile, configuration)
_, err := resolveLogFile(logFile, configuration, logger)

if err.Error() != "configuration file has no contents" {
t.Errorf("Error was not properly returned when checking configuration contents")
}
}

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

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration := &config.Configuration{
Rules: make([]config.Rule, 1),
}

_, err := resolveLogFile(logFile, configuration)
_, err := resolveLogFile(logFile, configuration, logger)

if err.Error() != "configuration file contains no log name" {
t.Errorf("Error was not properly returned when checking configuration name")
}
}

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

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration := &config.Configuration{
Name: "test",
}

_, err := resolveLogFile(logFile, configuration)
_, err := resolveLogFile(logFile, configuration, logger)
if err.Error() != "configuration does not have any rules" {
t.Errorf("Error was not properly returned when checking configuration rules")
}
Expand Down
64 changes: 43 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

config "github.com/kyallanum/athena/models/config"
library "github.com/kyallanum/athena/models/library"
logger_pkg "github.com/kyallanum/athena/models/logger"
logs "github.com/kyallanum/athena/models/logs"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var configFile, logFile string
var configFile, logFile, logOutput string

var rootCmd = &cobra.Command{
Use: "athena [flags]",
Expand All @@ -19,10 +21,10 @@ 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
},
Expand All @@ -35,35 +37,55 @@ func Execute() error {
}
}

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

err := rootCmd.Execute()
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)
fmt.Println("Athena v1.0.0 Starting")

fmt.Println("Getting Configuration File: ", configFile, "...")
logger.Info("Athena v1.0.0 Starting")

logger.Info("Getting Configuration File: ", configFile, "...")
configuration, err := config.CreateConfiguration(configFile)
errCheck(err)
fmt.Println("Configuration Loaded")
logger.Info("Configuration Loaded")

fmt.Println("Loading Log File: ", logFile, "... ")
logger.Info("Loading Log File: ", logFile, "... ")
logFileContents, err := logs.LoadLogFile(logFile)
errCheck(err)
fmt.Println("Log File Loaded")
logger.Info("Log File Loaded")

library, err := resolveLogFile(logFileContents, configuration)
library, err := resolveLogFile(logFileContents, configuration, logger)
errCheck(err)

err = printSummary(library)
err = printSummary(library, logger)
errCheck(err)

return nil
}

func init() {
rootCmd.Flags().StringVarP(&configFile, "config", "c", os.Getenv("ATHENA_CONFIG_FILE"), "")
rootCmd.Flags().StringVarP(&logFile, "log-file", "l", os.Getenv("ATHENA_LOG_FILE"), "")
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) (*library.Library, error) {
func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration, logger *logrus.Logger) (*library.Library, error) {

wrapError := func(err error) error {
return fmt.Errorf("unable to resolve log file: \n\t%w", err)
}
Expand All @@ -80,22 +102,22 @@ func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration)

ret_library := library.New(configuration.Name)

fmt.Println("Resolving Log File")
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)
}

ret_library.AddRuleData(configuration.Rules[i].Name, currentRuleData)
}

fmt.Println("Log File Resolved")
logger.Info("Log File Resolved")

return ret_library, nil
}

func printSummary(library *library.Library) error {
func printSummary(library *library.Library, logger *logrus.Logger) error {
wrapError := func(err error) error {
return fmt.Errorf("unable to print summary: \n\t%w", err)
}
Expand All @@ -105,20 +127,20 @@ func printSummary(library *library.Library) error {
return wrapError(err)
}

fmt.Printf("\n--------------- %s Log File Summary ---------------\n", libraryName)
logger.Infof("\n--------------- %s Log File Summary ---------------\n", libraryName)
libraryKeys := library.LibraryKeys()
for _, rule := range libraryKeys {
fmt.Printf("Rule: %s\n", rule)
logger.Infof("Rule: %s\n", rule)
ruleData, _ := library.RuleData(rule)
summaryDataLen := ruleData.SummaryDataLen()
if summaryDataLen == 0 {
fmt.Println("No summary lines provided.")
logger.Info("No summary lines provided.")
} else {
for i := 0; i < summaryDataLen; i++ {
fmt.Println("\t", ruleData.SummaryData(i))
logger.Info("\t", ruleData.SummaryData(i))
}
}
fmt.Println()
logger.Info()
}

return nil
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ go 1.21

require (
github.com/davidmytton/url-verifier v1.0.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidmytton/url-verifier v1.0.0 h1:TIdwZ+rWVnfnJPMD/DclwEJ0XewV4jkr0qEcX/58n+4=
Expand All @@ -10,12 +11,19 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 1 addition & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package main

import (
"fmt"
"os"

"github.com/kyallanum/athena/cmd"
)

func main() {
defer func() {
if err := recover(); err != nil {
fmt.Fprintln(os.Stderr, "\nAn error occured: ", err)
}
}()

if err := cmd.Execute(); 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
Loading

0 comments on commit 0d42341

Please sign in to comment.