From f1ed3c977a252f2c65743965b4866b9ab09288d7 Mon Sep 17 00:00:00 2001 From: Alexey Date: Fri, 28 Nov 2025 20:12:45 +0200 Subject: [PATCH] add project first skeleton --- Makefile | 2 +- cmd/root.go | 17 ++++++++++++++++ cmd/serve.go | 41 +++++++++++++++++++++++++++++++++++++++ cmd/tsctl/main.go | 7 ------- internal/app/app.go | 6 ++++++ internal/app/errors.go | 9 +++++++++ internal/config/config.go | 26 +++++++++++++++++++++++++ main.go | 9 +++++++++ 8 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 cmd/root.go create mode 100644 cmd/serve.go delete mode 100644 cmd/tsctl/main.go create mode 100644 internal/app/app.go create mode 100644 internal/app/errors.go create mode 100644 internal/config/config.go create mode 100644 main.go diff --git a/Makefile b/Makefile index ac5762a..54bdf33 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Makefile NAME = tsctl -ENTRY = ./cmd/$(NAME)/main.go +ENTRY = ./main.go BIN_DIR = bin BINARY = ${BIN_DIR}/$(NAME) CHECK_LINTER = command -v golangci-lint >/dev/null 2>&1 diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..2a9a658 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "tsctl", + Short: "Tool for managing TriggerSmith", + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, +} + +func Execute() error { + return rootCmd.Execute() +} \ No newline at end of file diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..4bc27e1 --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "log/slog" + + "git.oblat.lv/alex/triggerssmith/internal/config" + "github.com/spf13/cobra" +) + +var opts = struct { + ConfigPath *string + Debug *bool +}{} + + +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Start the server", + Run: func(cmd *cobra.Command, args []string) { + if *opts.Debug { + slog.SetDefault(slog.New(slog.NewTextHandler(cmd.OutOrStdout(), &slog.HandlerOptions{Level: slog.LevelDebug}))) + } else { + slog.SetDefault(slog.New(slog.NewTextHandler(cmd.OutOrStdout(), &slog.HandlerOptions{Level: slog.LevelInfo}))) + } + slog.Debug("Starting server") + slog.Debug("Reading configuration", slog.String("path", *opts.ConfigPath)) + config, err := config.LoadConfig(*opts.ConfigPath) + if err != nil { + slog.Error("Failed to load configuration", slog.String("path", *opts.ConfigPath), slog.String("error", err.Error())) + return + } + slog.Debug("Configuration loaded", slog.Any("config", config)) + slog.Info("Server started", slog.Int("port", config.Server.Port), slog.String("address", config.Server.Addr)) + }, +} + +func init() { + opts.Debug = serveCmd.Flags().BoolP("debug", "d", false, "Enable debug logs") + opts.ConfigPath = serveCmd.Flags().StringP("config", "c", "config.yaml", "Path to configuration file") + rootCmd.AddCommand(serveCmd) +} diff --git a/cmd/tsctl/main.go b/cmd/tsctl/main.go deleted file mode 100644 index edf1b22..0000000 --- a/cmd/tsctl/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package tsctl - -import "fmt" - -func main() { - fmt.Println("tsctl main function") -} \ No newline at end of file diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 0000000..fd750c6 --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,6 @@ +package app + + +type App struct { + // Application state and configurations can be added here +} \ No newline at end of file diff --git a/internal/app/errors.go b/internal/app/errors.go new file mode 100644 index 0000000..a61c6de --- /dev/null +++ b/internal/app/errors.go @@ -0,0 +1,9 @@ +package app + +import ( + "errors" +) + +var ( + ErrNilPointerWarn = errors.New("nil pointer dereference warning") +) \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..ac000e5 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,26 @@ +package config + +import ( + "github.com/akyaiy/GSfass/core/config" +) + +type ServerConfig struct { + Port int `mapstructure:"port"` + Addr string `mapstructure:"address"` +} + +type Config struct { + Server ServerConfig `mapstructure:"server"` +} + +func LoadConfig(path string) (*Config, error) { + var cfg Config + err := config.Read().Config().FilePath(path).SetBy(&cfg).SetDefaults(map[string]any{ + "server.port": 8080, + "server.address": "127.0.0.0", + }).End() + if err != nil { + return nil, err + } + return &cfg, nil +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..4f402af --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "git.oblat.lv/alex/triggerssmith/cmd" +) + +func main() { + cmd.Execute() +} \ No newline at end of file