diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-08 11:43:43 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-09 12:37:18 +0300 |
| commit | 2d7ddbeae8286373ac19787dc7dde598a7cb0598 (patch) | |
| tree | 9749939f8b569951e9639d29450b18c84bb8b6c1 /internal/config | |
| parent | 7306afe9ab073c424ddca0ddc57950f237948118 (diff) | |
refactor
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/args.go | 4 | ||||
| -rw-r--r-- | internal/config/common.go | 14 | ||||
| -rw-r--r-- | internal/config/config.go | 27 | ||||
| -rw-r--r-- | internal/config/initializer.go | 131 |
4 files changed, 114 insertions, 62 deletions
diff --git a/internal/config/args.go b/internal/config/args.go index 3e2eb1f..a671ae3 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -17,10 +17,12 @@ type Args struct { ConnectionsPerCPU int Discovery string LogDir string + Logger string LogLevel string Mode omode.Mode NoColor bool PrivateKeyPathFile string + QueryStr string Quiet bool RegexInvert bool RegexStr string @@ -42,6 +44,7 @@ func (a *Args) String() string { sb.WriteString("Args(") sb.WriteString(fmt.Sprintf("%s:%s,", "LogDir", a.LogDir)) + sb.WriteString(fmt.Sprintf("%s:%s,", "Logger", a.Logger)) sb.WriteString(fmt.Sprintf("%s:%s,", "LogLevel", a.LogLevel)) sb.WriteString(fmt.Sprintf("%s:%v,", "Arguments", a.Arguments)) sb.WriteString(fmt.Sprintf("%s:%v,", "ConfigFile", a.ConfigFile)) @@ -50,6 +53,7 @@ func (a *Args) String() string { sb.WriteString(fmt.Sprintf("%s:%v,", "Mode", a.Mode)) sb.WriteString(fmt.Sprintf("%s:%v,", "NoColor", a.NoColor)) sb.WriteString(fmt.Sprintf("%s:%v,", "PrivateKeyPathFile", a.PrivateKeyPathFile)) + sb.WriteString(fmt.Sprintf("%s:%v,", "QueryStr", a.QueryStr)) sb.WriteString(fmt.Sprintf("%s:%v,", "Quiet", a.Quiet)) sb.WriteString(fmt.Sprintf("%s:%v,", "RegexInvert", a.RegexInvert)) sb.WriteString(fmt.Sprintf("%s:%v,", "RegexStr", a.RegexStr)) diff --git a/internal/config/common.go b/internal/config/common.go index 5e81bc9..9d22c95 100644 --- a/internal/config/common.go +++ b/internal/config/common.go @@ -6,14 +6,14 @@ type CommonConfig struct { SSHPort int // Enable experimental features (mainly for dev purposes) ExperimentalFeaturesEnable bool `json:",omitempty"` + // LogDir defines the log directory. + LogDir string + // Logger defines the name of the logger implementation. + Logger string // LogLevel defines how much is logged. LogLevel string `json:",omitempty"` - // The log strategy to use, one of - // stdout: only log to stdout (useful when used with systemd) - // daily: create a log file for every day + // LogStrategy defines the log rotation strategy. LogStrategy string - // The log directory - LogDir string // The cache directory CacheDir string // The temp directory @@ -26,7 +26,9 @@ func newDefaultCommonConfig() *CommonConfig { SSHPort: DefaultSSHPort, ExperimentalFeaturesEnable: false, LogDir: "log", - LogLevel: "INFO", + Logger: "stdout", + LogLevel: DefaultLogLevel, + LogStrategy: "daily", CacheDir: "cache", TmpDir: "/tmp", } diff --git a/internal/config/config.go b/internal/config/config.go index f216688..077a658 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -15,6 +15,14 @@ const ( DefaultConnectionsPerCPU int = 10 // DTailSSHServerDefaultPort is the default DServer port. DefaultSSHPort int = 2222 + // DefaultLogLevel specifies the default log level (obviously) + DefaultLogLevel string = "INFO" + // DefaultClientLogger specifies the default logger for the client commands. + DefaultClientLogger string = "fout" + // DefaultServerLogger specifies the default logger for dtail server. + DefaultServerLogger string = "file" + // DefaultHealthCheckLogger specifies the default logger used for health checks. + DefaultHealthCheckLogger string = "none" ) // Client holds a DTail client configuration. @@ -33,12 +41,15 @@ func Setup(sourceProcess source.Source, args *Args, additionalArgs []string) { Server: newDefaultServerConfig(), Client: newDefaultClientConfig(), } - initializer.parseConfig(args) - Client, Server, Common = initializer.transformConfig( - sourceProcess, - args, additionalArgs, - initializer.Client, - initializer.Server, - initializer.Common, - ) + if err := initializer.parseConfig(args); err != nil { + panic(err) + } + if err := initializer.transformConfig(sourceProcess, args, additionalArgs); err != nil { + panic(err) + } + + // Make config accessible globally + Server = initializer.Server + Client = initializer.Client + Common = initializer.Common } diff --git a/internal/config/initializer.go b/internal/config/initializer.go index ec758c8..5247699 100644 --- a/internal/config/initializer.go +++ b/internal/config/initializer.go @@ -18,14 +18,15 @@ type initializer struct { Client *ClientConfig } -func (c *initializer) parseConfig(args *Args) { +type transformCb func(*initializer, *Args, []string) error + +func (c *initializer) parseConfig(args *Args) error { if strings.ToUpper(args.ConfigFile) == "NONE" { - return + return nil } if args.ConfigFile != "" { - c.parseSpecificConfig(args.ConfigFile) - return + return c.parseSpecificConfig(args.ConfigFile) } if homeDir, err := os.UserHomeDir(); err != nil { @@ -38,85 +39,119 @@ func (c *initializer) parseConfig(args *Args) { } } } + + return nil } -func (c *initializer) parseSpecificConfig(configFile string) { +func (c *initializer) parseSpecificConfig(configFile string) error { fd, err := os.Open(configFile) if err != nil { - panic(fmt.Sprintf("Unable to read config file: %v", err)) + return fmt.Errorf("Unable to read config file: %v", err) } defer fd.Close() cfgBytes, err := ioutil.ReadAll(fd) if err != nil { - panic(fmt.Sprintf("Unable to read config file %s: %v", configFile, err)) + return fmt.Errorf("Unable to read config file %s: %v", configFile, err) } - err = json.Unmarshal([]byte(cfgBytes), c) - if err != nil { - panic(fmt.Sprintf("Unable to parse config file %s: %v", configFile, err)) + if err := json.Unmarshal([]byte(cfgBytes), c); err != nil { + return fmt.Errorf("Unable to parse config file %s: %v", configFile, err) } + + return nil } -func (c *initializer) transformConfig(sourceProcess source.Source, args *Args, additionalArgs []string, - client *ClientConfig, server *ServerConfig, common *CommonConfig) (*ClientConfig, *ServerConfig, *CommonConfig) { - if args.LogDir != "" { - common.LogDir = args.LogDir - } - if strings.Contains(common.LogDir, "~/") { - homeDir, err := os.UserHomeDir() - if err != nil { - panic(err) - } - common.LogDir = strings.ReplaceAll(common.LogDir, "~/", fmt.Sprintf("%s/", homeDir)) - } - if common.LogStrategy == "" { - common.LogStrategy = "daily" +func (i *initializer) transformConfig(sourceProcess source.Source, args *Args, additionalArgs []string) error { + + switch sourceProcess { + case source.Server: + return i.optimusPrime(transformServer, args, additionalArgs) + case source.Client: + return i.optimusPrime(transformClient, args, additionalArgs) + case source.HealthCheck: + return i.optimusPrime(transformHealthCheck, args, additionalArgs) + default: + return fmt.Errorf("Unable to transform config, unknown source '%s'", sourceProcess) } +} - if args.Spartan { - args.Quiet = true - args.NoColor = true - if args.LogLevel == "" { - args.LogLevel = "ERROR" - } +func (i *initializer) optimusPrime(sourceCb transformCb, args *Args, additionalArgs []string) error { + // Copy args to config objects. + if args.SSHPort != DefaultSSHPort { + i.Common.SSHPort = args.SSHPort + } + if args.LogLevel != DefaultLogLevel { + i.Common.LogLevel = args.LogLevel } if args.NoColor { - client.TermColorsEnable = false + i.Client.TermColorsEnable = false } - - if args.LogLevel != "" { - common.LogLevel = args.LogLevel - } else if sourceProcess == source.Client && args.ServersStr == "" && args.Discovery == "" { - // We are in serverless mode. Default log level is WARN. - common.LogLevel = "WARN" + if args.LogDir != "" { + i.Common.LogDir = args.LogDir + } + if args.Logger != "" { + i.Common.Logger = args.Logger } - if args.SSHPort != DefaultSSHPort { - common.SSHPort = args.SSHPort + // Setup log directory. + if strings.Contains(i.Common.LogDir, "~/") { + homeDir, err := os.UserHomeDir() + if err != nil { + panic(err) + } + i.Common.LogDir = strings.ReplaceAll(i.Common.LogDir, "~/", fmt.Sprintf("%s/", homeDir)) } + // Serverless mode. if args.Discovery == "" && (args.ServersStr == "" || strings.ToLower(args.ServersStr) == "serverless") { // We are not connecting to any servers. args.Serverless = true + i.Common.LogLevel = "WARN" } - if sourceProcess == source.HealthCheck { - args.TrustAllHosts = true - if !args.Serverless && strings.ToLower(args.ServersStr) == "" { - args.ServersStr = fmt.Sprintf("localhost:%d", DefaultSSHPort) + // Source type specific transormations. + sourceCb(i, args, additionalArgs) + + // Spartan mode. + if args.Spartan { + args.Quiet = true + args.NoColor = true + i.Client.TermColorsEnable = false + if args.LogLevel == "" { + args.LogLevel = "ERROR" + i.Common.LogLevel = "ERROR" } } - - // Interpret additional args as file list. + // Interpret additional args as file list or as query. if args.What == "" { var files []string - for _, file := range flag.Args() { - files = append(files, file) + for _, arg := range flag.Args() { + if args.QueryStr == "" && strings.Contains(strings.ToLower(arg), "select ") { + args.QueryStr = arg + continue + } + files = append(files, arg) } args.What = strings.Join(files, ",") } - return client, server, common + return nil +} + +func transformClient(i *initializer, args *Args, additionalArgs []string) error { + return nil +} + +func transformServer(i *initializer, args *Args, additionalArgs []string) error { + return nil +} + +func transformHealthCheck(i *initializer, args *Args, additionalArgs []string) error { + args.TrustAllHosts = true + if !args.Serverless && args.ServersStr == "" { + args.ServersStr = fmt.Sprintf("localhost:%d", DefaultSSHPort) + } + return nil } |
