-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split main.go into multiple files based on their purpose
- Loading branch information
Showing
5 changed files
with
252 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/fatih/color" | ||
"github.com/jdrews/logstation/api/server/models" | ||
"github.com/spf13/viper" | ||
"regexp" | ||
) | ||
|
||
// LogMessage is used to associate the log line Text with the originating/source LogFile when shipping log lines around | ||
type LogMessage struct { | ||
Text string `json:"text"` | ||
LogFile string `json:"logfile"` | ||
} | ||
|
||
// CompiledRegexColors is used to associate the regex with the selected ANSI color | ||
type CompiledRegexColors struct { | ||
regex *regexp.Regexp | ||
color string | ||
} | ||
|
||
// ParseRegexPatterns processes all the regular expression patterns associated with each color | ||
// and compiles them at boot time to optimize regex matching. | ||
func ParseRegexPatterns() []CompiledRegexColors { | ||
var syntaxColors models.SyntaxColors | ||
err := viper.UnmarshalKey("syntaxColors", &syntaxColors) | ||
if err != nil { | ||
logger.Fatal("Unable to unmarshall syntax colors from config file. Please check the colors in the config.") | ||
return nil | ||
} | ||
crcs := make([]CompiledRegexColors, len(syntaxColors)) | ||
for index, element := range syntaxColors { | ||
regex, err := regexp.Compile(element.Regex) | ||
if err != nil { | ||
logger.Fatal("Unable to compile the regex of ", element.Regex, " associated with the color ", element.Color, ". Please check the conf file and ensure your regex syntax is valid. More details here: ", err) | ||
} | ||
crcs[index] = CompiledRegexColors{regex, element.Color} | ||
} | ||
return crcs | ||
} | ||
|
||
// Colorize runs each line from a logFile through the regex patterns to determine if the line should be colored. | ||
// Outputs a LogMessage with line colors in ANSI format | ||
func Colorize(line string, logFile string, patterns []CompiledRegexColors) LogMessage { | ||
for _, element := range patterns { | ||
if element.regex.MatchString(line) { | ||
switch element.color { | ||
case "red": | ||
line = color.RedString(line) | ||
case "green": | ||
line = color.GreenString(line) | ||
case "yellow": | ||
line = color.YellowString(line) | ||
case "blue": | ||
line = color.BlueString(line) | ||
case "magenta": | ||
line = color.MagentaString(line) | ||
case "cyan": | ||
line = color.CyanString(line) | ||
case "hired": | ||
line = color.HiRedString(line) | ||
case "higreen": | ||
line = color.HiGreenString(line) | ||
case "hiyellow": | ||
line = color.HiYellowString(line) | ||
case "hiblue": | ||
line = color.HiBlueString(line) | ||
case "himagenta": | ||
line = color.HiMagentaString(line) | ||
case "hicyan": | ||
line = color.HiCyanString(line) | ||
} | ||
break | ||
} | ||
} | ||
return LogMessage{line, logFile} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"github.com/spf13/viper" | ||
"os" | ||
) | ||
|
||
// HandleConfigFile processes the given config file and sets up app variables | ||
// If no config file is provided or the configFilePath is empty, | ||
// it will make a config file with the [logstation.default.conf] and quit the app | ||
func HandleConfigFile(configFilePath string) { | ||
|
||
configFilename := "logstation.conf" | ||
viper.SetConfigName(configFilename) | ||
viper.SetConfigType("toml") | ||
viper.AddConfigPath(".") | ||
viper.SetDefault("logs", []string{`test\logfile.log`, `test\logfile2.log`}) | ||
viper.SetDefault("server_settings.webserveraddress", "0.0.0.0") | ||
viper.SetDefault("server_settings.disablecors", false) | ||
|
||
viper.SetConfigFile(configFilePath) | ||
if err := viper.ReadInConfig(); err != nil { | ||
if errors.Is(err, os.ErrNotExist) || errors.As(err, &viper.ConfigFileNotFoundError{}) { | ||
logger.Warn("Config file not found at ", configFilePath) | ||
logger.Warn("Writing default config file to ", configFilename) | ||
logger.Warn("Please open and edit config file before running this application again. Exiting...") | ||
err := os.WriteFile(configFilename, defaultConfigFile, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
os.Exit(0) | ||
} else { | ||
panic(fmt.Errorf("config file %q loading error: %s", viper.ConfigFileUsed(), err)) | ||
} | ||
} | ||
logger.Info("Loaded ", viper.ConfigFileUsed()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.