Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to idiomatic constructor pattern #10

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"os"
"testing"

config "github.com/kyallanum/athena/v1.0.0/models/config"
logs "github.com/kyallanum/athena/v1.0.0/models/logs"
config "github.com/kyallanum/athena/models/config"
logs "github.com/kyallanum/athena/models/logs"
)

func TestResolveFile(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"os"

config "github.com/kyallanum/athena/v1.0.0/models/config"
library "github.com/kyallanum/athena/v1.0.0/models/library"
logs "github.com/kyallanum/athena/v1.0.0/models/logs"
config "github.com/kyallanum/athena/models/config"
library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ func Execute() error {
}

err := rootCmd.Execute()
errCheck(err)
errCheck(err)
fmt.Println("Athena v1.0.0 Starting")

fmt.Println("Getting Configuration File: ", configFile, "...")
Expand Down Expand Up @@ -78,7 +78,7 @@ func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration)
return nil, fmt.Errorf("configuration does not have any rules")
}

ret_library := library.Library.New(library.Library{}, configuration.Name)
ret_library := library.New(configuration.Name)

fmt.Println("Resolving Log File")
for i := 0; i < len(configuration.Rules); i++ {
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/kyallanum/athena/v1.0.0
module github.com/kyallanum/athena

go 1.21

require github.com/davidmytton/url-verifier v1.0.0
require (
github.com/davidmytton/url-verifier v1.0.0
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/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
101 changes: 100 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,111 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/kyallanum/athena/v1.0.0/cmd"
"github.com/kyallanum/athena/cmd"
config "github.com/kyallanum/athena/models/config"
library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
)

func errCheck(err error) {
if err != nil {
panic(err)
}
}

func parseFlags(configFile *string, logFile *string) error {
var configFlag string
var logFlag string

flag.StringVar(&configFlag, "c", "", "")
flag.StringVar(&configFlag, "config", "", "")
flag.StringVar(&logFlag, "l", "", "")
flag.StringVar(&logFlag, "log-file", "", "")
flag.Parse()

if *configFile == "" {
*configFile = configFlag
}
if *logFile == "" {
*logFile = logFlag
}

if *configFile == "" {
return fmt.Errorf("configuration file was not specified")
}

if *logFile == "" {
return fmt.Errorf("log file was not specified")
}

return nil
}

func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration) (*library.Library, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to resolve log file: \n\t%w", err)
}

if contents == nil || contents.Len() == 0 {
return nil, fmt.Errorf("log file contains no contents")
} else if configuration == nil || (configuration.Name == "" && configuration.Rules == nil) {
return nil, fmt.Errorf("configuration file has no contents")
} else if configuration.Name == "" {
return nil, fmt.Errorf("configuration file contains no log name")
} else if configuration.Rules == nil || len(configuration.Rules) == 0 {
return nil, fmt.Errorf("configuration does not have any rules")
}

ret_library := library.New(configuration.Name)

fmt.Println("Resolving Log File")
for i := 0; i < len(configuration.Rules); i++ {
currentRuleData, err := config.ResolveRule(contents, &configuration.Rules[i])
if err != nil {
return nil, wrapError(err)
}

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

fmt.Println("Log File Resolved")

return ret_library, nil
}

func printSummary(library *library.Library) error {
wrapError := func(err error) error {
return fmt.Errorf("unable to print summary: \n\t%w", err)
}

libraryName, err := library.Name()
if err != nil {
return wrapError(err)
}

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

return nil
}

func main() {
defer func() {
if err := recover(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"testing"

logs "github.com/kyallanum/athena/v1.0.0/models/logs"
logs "github.com/kyallanum/athena/models/logs"
)

func TestTranslateRegex(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion models/config/regex_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

library "github.com/kyallanum/athena/v1.0.0/models/library"
library "github.com/kyallanum/athena/models/library"
)

// func resolveLine determines whether the current log line matches a
Expand Down
6 changes: 3 additions & 3 deletions models/config/regex_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

library "github.com/kyallanum/athena/v1.0.0/models/library"
library "github.com/kyallanum/athena/models/library"
)

func TestResolveLine(t *testing.T) {
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestTranslateSearchTermReference(t *testing.T) {
}()

regex := `Testing {{test}}`
st_data := library.SearchTermData.New(library.SearchTermData{})
st_data := library.NewSearchTermData()
st_data.AddValue("test", "Test1")

newRegex, err := translateSearchTermReference(regex, st_data)
Expand All @@ -73,7 +73,7 @@ func TestTranslateSearchTermReferenceBadReference(t *testing.T) {
}()

regex := `Testing {{test}}`
st_data := library.SearchTermData.New(library.SearchTermData{})
st_data := library.NewSearchTermData()
st_data.AddValue("bad_test", "testing")

_, err := translateSearchTermReference(regex, st_data)
Expand Down
6 changes: 3 additions & 3 deletions models/config/rule_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package models
import (
"fmt"

library "github.com/kyallanum/athena/v1.0.0/models/library"
logs "github.com/kyallanum/athena/v1.0.0/models/logs"
library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
)

func ResolveRule(contents *logs.LogFile, rule *Rule) (*library.RuleData, error) {
Expand All @@ -15,7 +15,7 @@ func ResolveRule(contents *logs.LogFile, rule *Rule) (*library.RuleData, error)
allEntriesFound := false
linesResolved := []int{}

currentRuleData := library.RuleData.New(library.RuleData{})
currentRuleData := library.NewRuleData()

for !allEntriesFound {
currentSearchTermData, err := resolveSearchTerms(contents, rule, &linesResolved)
Expand Down
6 changes: 3 additions & 3 deletions models/config/searchterm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"slices"

library "github.com/kyallanum/athena/v1.0.0/models/library"
logs "github.com/kyallanum/athena/v1.0.0/models/logs"
library "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
)

func resolveSearchTerms(logFile *logs.LogFile, rule *Rule, linesResolved *[]int) (*library.SearchTermData, error) {
Expand All @@ -19,7 +19,7 @@ func resolveSearchTerms(logFile *logs.LogFile, rule *Rule, linesResolved *[]int)
}
}()

currentSearchTermData := library.SearchTermData.New(library.SearchTermData{})
currentSearchTermData := library.NewSearchTermData()
currentSearchTerm := rule.SearchTerms[0]
searchTermTranslated := false
for fileIndex, searchTermIndex := 0, 0; fileIndex < logFile.Len() && searchTermIndex < len(rule.SearchTerms); fileIndex++ {
Expand Down
4 changes: 2 additions & 2 deletions models/config/source_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Source(source string) (IConfigurationSource, error) {
}

if isUrl {
return ConfigWebSource.New(ConfigWebSource{}, source), nil
return NewWebSource(source), nil
}

isFile, err := verifyFilePath(source)
Expand All @@ -84,7 +84,7 @@ func Source(source string) (IConfigurationSource, error) {
}

if isFile {
return ConfigFileSource.New(ConfigFileSource{}, source), nil
return NewFileSource(source), nil
}

//Should never reach this
Expand Down
4 changes: 2 additions & 2 deletions models/config/source_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGetSourceFile(t *testing.T) {
t.Errorf("Error returned from getSource when it shouldn't have: \n\t%s", err.Error())
}

if reflect.TypeOf(configSource).String() != "*models.ConfigFileSource" {
if reflect.TypeOf(configSource).String() != "*models.FileSource" {
t.Errorf("Correct data type not returned from GetSource")
}
}
Expand All @@ -42,7 +42,7 @@ func TestGetSourceWeb(t *testing.T) {
t.Errorf("Error returned from getSource when it shouldn't have: \n\t%s", err.Error())
}

if reflect.TypeOf(configWeb).String() != "*models.ConfigWebSource" {
if reflect.TypeOf(configWeb).String() != "*models.WebSource" {
t.Errorf("Correct data type not returned from GetSource: \n\t%s", reflect.TypeOf(configWeb).String())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"path/filepath"
)

type ConfigFileSource struct {
type FileSource struct {
ConfigurationSource
}

func (config *ConfigFileSource) Config() ([]byte, error) {
func (config *FileSource) Config() ([]byte, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to load configuration from file: \n\t%w", err)
}
Expand Down Expand Up @@ -48,8 +48,8 @@ func (config *ConfigFileSource) Config() ([]byte, error) {
return bytes, nil
}

func (ConfigFileSource) New(source string) IConfigurationSource {
return &ConfigFileSource{
func NewFileSource(source string) IConfigurationSource {
return &FileSource{
ConfigurationSource: ConfigurationSource{
source_type: "file",
source: source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
func TestNewFileConfig(t *testing.T) {
source := "./examples/apt-term-config.json"

fileSource := ConfigFileSource.New(ConfigFileSource{}, source)
fileSource := NewFileSource(source)

if fileSource.SourceType() != "file" {
t.Errorf("Configuration source type not set properly")
}

if reflect.TypeOf(fileSource).String() != "*models.ConfigFileSource" {
if reflect.TypeOf(fileSource).String() != "*models.FileSource" {
t.Errorf("Configuration source not of the right type")
}
}

func TestLoadFileConfig(t *testing.T) {
source := "../../examples/apt-term-config.json"
fileSource := ConfigFileSource.New(ConfigFileSource{}, source)
fileSource := NewFileSource(source)

fileConfig, err := fileSource.Config()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"net/http"
)

type ConfigWebSource struct {
type WebSource struct {
ConfigurationSource
}

func (config *ConfigWebSource) Config() ([]byte, error) {
func (config *WebSource) Config() ([]byte, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to create configuration for web source: \n\t%w", err)
}
Expand All @@ -30,8 +30,8 @@ func (config *ConfigWebSource) Config() ([]byte, error) {
return data, nil
}

func (ConfigWebSource) New(source string) IConfigurationSource {
return &ConfigWebSource{
func NewWebSource(source string) IConfigurationSource {
return &WebSource{
ConfigurationSource: ConfigurationSource{
source_type: "web",
source: source,
Expand Down
Loading