aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Taskfile.yml2
-rw-r--r--config.json5
-rw-r--r--main.go82
3 files changed, 82 insertions, 7 deletions
diff --git a/Taskfile.yml b/Taskfile.yml
index ec302d9..b168e69 100644
--- a/Taskfile.yml
+++ b/Taskfile.yml
@@ -11,7 +11,7 @@ tasks:
desc: "Runs cthcous binary."
deps: [build]
cmds:
- - build/cthcous
+ - build/cthcous {{.CLI_ARGS}}
run-container:
desc: "Runs the container bridge binary."
diff --git a/config.json b/config.json
new file mode 100644
index 0000000..f288679
--- /dev/null
+++ b/config.json
@@ -0,0 +1,5 @@
+{
+ "ports": [],
+ "socketDir": "./",
+ "pathOverrides": {}
+} \ No newline at end of file
diff --git a/main.go b/main.go
index 79bb0d4..0ac0413 100644
--- a/main.go
+++ b/main.go
@@ -5,7 +5,9 @@ import (
"context"
"encoding/base64"
"encoding/binary"
+ "encoding/json"
"errors"
+ "flag"
"fmt"
"io"
"log/slog"
@@ -18,6 +20,10 @@ import (
"time"
)
+const version = "0.0.1"
+
+const defaultConfigPath = "/etc/cthcous/config.json"
+
const defaultReadTimeout = 2 * time.Second
type BridgeInfo struct {
@@ -27,14 +33,35 @@ type BridgeInfo struct {
}
func main() {
- slog.Info("starting cthcous...")
+ slog.Info(fmt.Sprintf("starting cthcous v%s...", version))
+
+ flags := parseFlags()
+ if flags.ConfigPath == "" {
+ slog.Warn("no config path specified. using default.", slog.String("path", defaultConfigPath))
+ flags.ConfigPath = defaultConfigPath
+ }
- ports := []uint{1234, 4321}
- socketDir := "./"
+ slog.Info("loading config...", slog.String("path", flags.ConfigPath))
+ config, err := loadConfig(flags.ConfigPath)
+ if err != nil {
+ slog.Error("could not load config.", slog.Any("error", err))
+ return
+ }
- bridgeInfos := make([]BridgeInfo, len(ports))
- for i, port := range ports {
- socketPath := fmt.Sprintf("%s/%d.socket", socketDir, port)
+ if len(config.Ports) == 0 {
+ slog.Error("no ports specified in config. nothing to do. exiting.")
+ return
+ }
+
+ bridgeInfos := make([]BridgeInfo, len(config.Ports))
+ for i, port := range config.Ports {
+ var socketPath string
+ if override, ok := config.PathOverrides[port]; ok {
+ slog.Info("using override for a socket path.", slog.Any("port", port), slog.String("path", override))
+ socketPath = override
+ } else {
+ socketPath = fmt.Sprintf("%s/%d.socket", config.SocketDir, port)
+ }
socketPath = path.Clean(socketPath)
bridgeInfos[i] = BridgeInfo{
@@ -63,6 +90,7 @@ func main() {
}
wg.Wait()
+ slog.Info("cthcous wait group completed. cya!")
}
func openBridgeSocket(ctx context.Context, wg *sync.WaitGroup, logger *slog.Logger, info BridgeInfo) {
@@ -164,3 +192,45 @@ func makeID() string {
binary.LittleEndian.PutUint64(b[:], r)
return base64.StdEncoding.EncodeToString(b[:])
}
+
+type Flags struct {
+ ConfigPath string
+}
+
+func parseFlags() (flags Flags) {
+ flag.StringVar(&flags.ConfigPath, "c", "", "path to config file")
+ flag.Parse()
+ return
+}
+
+type Config struct {
+ Ports []uint `json:"ports"`
+ SocketDir string `json:"socketDir"`
+ PathOverrides map[uint]string `json:"pathOverrides"`
+}
+
+var defaultConfig = Config{
+ Ports: []uint{},
+ SocketDir: "/run/cthcous/",
+ PathOverrides: map[uint]string{},
+}
+
+func loadConfig(path string) (Config, error) {
+ configFile, err := os.Open(path)
+ if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ return Config{}, fmt.Errorf("config file does not exist: %w", err)
+ }
+
+ return Config{}, err
+ }
+ defer configFile.Close()
+
+ config := defaultConfig
+ err = json.NewDecoder(configFile).Decode(&config)
+ if err != nil {
+ return Config{}, fmt.Errorf("could not decode config file: %w", err)
+ }
+
+ return config, nil
+}