summaryrefslogtreecommitdiff
path: root/internal/config
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-09-26 16:42:47 +0300
committerPaul Buetow <paul@buetow.org>2021-10-02 12:26:36 +0300
commitfcaa94c7453efa0d74e330128c0f5c2cde8f11b3 (patch)
tree1f686e5eeeb1b180cc14a3586f388f1a3492899c /internal/config
parentfe3e68afd99d8ea246be52893730f987e138ec24 (diff)
refactor config reader - also looks in additional search paths for config file unless NONE is specified
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/args.go25
-rw-r--r--internal/config/common.go2
-rw-r--r--internal/config/config.go61
-rw-r--r--internal/config/setup.go32
4 files changed, 69 insertions, 51 deletions
diff --git a/internal/config/args.go b/internal/config/args.go
index 89e4bc9..767cc65 100644
--- a/internal/config/args.go
+++ b/internal/config/args.go
@@ -16,6 +16,7 @@ type Args struct {
ConfigFile string
ConnectionsPerCPU int
Discovery string
+ LogDir string
LogLevel string
Mode omode.Mode
NoColor bool
@@ -39,6 +40,8 @@ func (a *Args) String() string {
var sb strings.Builder
sb.WriteString("Args(")
+ // TODO: All commands should make use of this
+ sb.WriteString(fmt.Sprintf("%s:%s,", "LogDir", a.LogDir))
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))
@@ -66,16 +69,24 @@ func (a *Args) String() string {
}
// Based on the argument list, transform/manipulate some of the arguments.
-func (a *Args) transform(args []string) {
+func (a *Args) transformConfig(args []string, client *ClientConfig, server *ServerConfig, common *CommonConfig) (*ClientConfig, *ServerConfig, *CommonConfig) {
+ if a.LogDir != "" {
+ common.LogDir = a.LogDir
+ if common.LogStrategy == "" {
+ // TODO: Implement the other (not-daily) log strategy for the server.
+ common.LogStrategy = "daily"
+ }
+ }
+
if a.LogLevel != "" {
- Common.LogLevel = a.LogLevel
+ common.LogLevel = a.LogLevel
}
- if a.SSHPort != 2222 {
- Common.SSHPort = a.SSHPort
+ if a.SSHPort != DefaultSSHPort {
+ common.SSHPort = a.SSHPort
}
if a.NoColor {
- Client.TermColorsEnable = false
+ client.TermColorsEnable = false
}
if a.Spartan {
@@ -95,6 +106,8 @@ func (a *Args) transform(args []string) {
}
a.What = strings.Join(files, ",")
}
+
+ return client, server, common
}
// SerializeOptions returns a string ready to be sent over the wire to the server.
@@ -102,4 +115,4 @@ func (a *Args) SerializeOptions() string {
return fmt.Sprintf("quiet=%v:spartan=%v", a.Quiet, a.Spartan)
}
-// TODO: Put the DeseializeOptions function here (move it away from the internal/server package)
+// NEXT: Put the DeseializeOptions function here (move it away from the internal/server package)
diff --git a/internal/config/common.go b/internal/config/common.go
index 7d45261..acc8e6a 100644
--- a/internal/config/common.go
+++ b/internal/config/common.go
@@ -23,7 +23,7 @@ type CommonConfig struct {
// Create a new default configuration.
func newDefaultCommonConfig() *CommonConfig {
return &CommonConfig{
- SSHPort: 2222,
+ SSHPort: DefaultSSHPort,
ExperimentalFeaturesEnable: false,
LogDir: "log",
CacheDir: "cache",
diff --git a/internal/config/config.go b/internal/config/config.go
index 2d77041..c9f411c 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -2,24 +2,26 @@ package config
import (
"encoding/json"
+ "fmt"
"io/ioutil"
"os"
+ "strings"
)
-// ControlUser is used for various DTail specific operations.
-const ControlUser string = "DTAIL-CONTROL"
-
-// ScheduleUser is used for non-interactive scheduled mapreduce queries.
-const ScheduleUser string = "DTAIL-SCHEDULE"
-
-// ContinuousUser is used for non-interactive continuous mapreduce queries.
-const ContinuousUser string = "DTAIL-CONTINUOUS"
-
-// TestUser is used for unit tests and potentially also for integration tests.
-const TestUser string = "DTAIL-TEST"
-
-// InterruptTimeoutS is used to terminate DTail when Ctrl+C was pressed twice within a given interval.
-const InterruptTimeoutS int = 3
+const (
+ // ControlUser is used for various DTail specific operations.
+ ControlUser string = "DTAIL-CONTROL"
+ // ScheduleUser is used for non-interactive scheduled mapreduce queries.
+ ScheduleUser string = "DTAIL-SCHEDULE"
+ // ContinuousUser is used for non-interactive continuous mapreduce queries.
+ ContinuousUser string = "DTAIL-CONTINUOUS"
+ // InterruptTimeoutS is used to terminate DTail when Ctrl+C was pressed twice within a given interval.
+ InterruptTimeoutS int = 3
+ // ConnectionsPerCPU controls how many connections are established concurrently as a start (slow start)
+ DefaultConnectionsPerCPU int = 10
+ // DTailSSHServerDefaultPort is the default DServer port.
+ DefaultSSHPort int = 2222
+)
// Client holds a DTail client configuration.
var Client *ClientConfig
@@ -37,21 +39,42 @@ type configInitializer struct {
Client *ClientConfig
}
-// Parse and read a given config file in JSON format.
-func (c *configInitializer) parseConfig(configFile string) {
+func (c *configInitializer) parseConfig(args *Args) {
+ if strings.ToUpper(args.ConfigFile) == "NONE" {
+ return
+ }
+
+ if args.ConfigFile != "" {
+ c.parseSpecificConfig(args.ConfigFile)
+ return
+ }
+
+ if homeDir, err := os.UserHomeDir(); err != nil {
+ var paths []string
+ paths = append(paths, fmt.Sprintf("%s/.config/dtail/dtail.conf", homeDir))
+ paths = append(paths, fmt.Sprintf("%s/.dtail.conf", homeDir))
+ for _, configPath := range paths {
+ if _, err := os.Stat(configPath); !os.IsNotExist(err) {
+ c.parseSpecificConfig(configPath)
+ }
+ }
+ }
+}
+
+func (c *configInitializer) parseSpecificConfig(configFile string) {
fd, err := os.Open(configFile)
if err != nil {
- panic(err)
+ panic(fmt.Sprintf("Unable to read config file: %v", err))
}
defer fd.Close()
cfgBytes, err := ioutil.ReadAll(fd)
if err != nil {
- panic(err)
+ panic(fmt.Sprintf("Unable to read config file %s: %v", configFile, err))
}
err = json.Unmarshal([]byte(cfgBytes), c)
if err != nil {
- panic(err)
+ panic(fmt.Sprintf("Unable to parse config file %s: %v", configFile, err))
}
}
diff --git a/internal/config/setup.go b/internal/config/setup.go
index 3c4bcc4..be8e867 100644
--- a/internal/config/setup.go
+++ b/internal/config/setup.go
@@ -1,11 +1,5 @@
package config
-import (
- "os"
-)
-
-const NoConfigFile string = "Don't read a config file - use defaults only"
-
// Setup the DTail configuration.
func Setup(args *Args, additionalArgs []string) {
initializer := configInitializer{
@@ -13,23 +7,11 @@ func Setup(args *Args, additionalArgs []string) {
Server: newDefaultServerConfig(),
Client: newDefaultClientConfig(),
}
-
- if args.ConfigFile == "" {
- // TODO: Search more paths for config file (e.g. in /etc and in ~/.config/...
- args.ConfigFile = "./cfg/dtail.json"
- }
-
- if args.ConfigFile != NoConfigFile {
- if _, err := os.Stat(args.ConfigFile); !os.IsNotExist(err) {
- initializer.parseConfig(args.ConfigFile)
- }
- }
-
- // Assign pointers to global variables, so that we can access the
- // configuration from any place of the program.
- Common = initializer.Common
- Server = initializer.Server
- Client = initializer.Client
-
- args.transform(additionalArgs)
+ initializer.parseConfig(args)
+ Client, Server, Common = args.transformConfig(
+ additionalArgs,
+ initializer.Client,
+ initializer.Server,
+ initializer.Common,
+ )
}