Skip to content

Commit

Permalink
fix changes
Browse files Browse the repository at this point in the history
  • Loading branch information
travbale committed Dec 31, 2023
1 parent 1973eba commit 0f09f99
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 85 deletions.
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kyallanum/athena/v1.0.0
module github.com/kyallanum/athena

go 1.21

Expand Down
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"
"github.com/kyallanum/athena/models/config"
"github.com/kyallanum/athena/models/library"
"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,7 +9,7 @@ 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")
Expand All @@ -22,7 +22,7 @@ func TestNewFileConfig(t *testing.T) {

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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestNewWebSource(t *testing.T) {
configSource := ConfigFileSource.New(ConfigFileSource{}, "../../examples/apt-term-config.json")
configSource := NewWebSource("../../examples/apt-term-config.json")
configFileString, _ := configSource.Config()

testTable := []struct {
Expand All @@ -27,20 +27,20 @@ func TestNewWebSource(t *testing.T) {
}

for _, testServer := range testTable {
testWebSource := ConfigWebSource.New(ConfigWebSource{}, testServer.server.URL)
testWebSource := NewWebSource(testServer.server.URL)
if testWebSource.SourceType() != "web" {
t.Errorf("Config source type not set properly.")
}

if reflect.TypeOf(testWebSource).String() != "*models.ConfigWebSource" {
if reflect.TypeOf(testWebSource).String() != "*models.WebSource" {
t.Errorf("Config source not of the right type.")
}
}

}

func TestLoadWebConfig(t *testing.T) {
configSource := ConfigFileSource.New(ConfigFileSource{}, "../../examples/apt-term-config.json")
configSource := NewFileSource("../../examples/apt-term-config.json")
configFileString, _ := configSource.Config()

testTable := []struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestLoadWebConfig(t *testing.T) {
for _, testServer := range testTable {
t.Run(testServer.name, func(t *testing.T) {
defer testServer.server.Close()
testWebSource := ConfigWebSource.New(ConfigWebSource{}, testServer.server.URL)
testWebSource := NewWebSource(testServer.server.URL)
configFileString, err := testWebSource.Config()

if reflect.TypeOf(configFileString).String() != reflect.TypeOf(testServer.expectedOutput).String() {
Expand Down
2 changes: 1 addition & 1 deletion models/config/summary_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"slices"
"strings"

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

func summaryKeys(summaryLine string) [][]string {
Expand Down
2 changes: 1 addition & 1 deletion models/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Library struct {
name string
}

func (Library) New(name string) *Library {
func New(name string) *Library {
return &Library{
rule_data_collection: make(map[string]RuleData),
name: name,
Expand Down
Loading

0 comments on commit 0f09f99

Please sign in to comment.