From 00748f4a889f529b6abc2c79ccb39f760c4deaa5 Mon Sep 17 00:00:00 2001 From: darius Date: Fri, 24 May 2024 20:32:42 +0200 Subject: [PATCH] config read + types added Config added config get from server --- commands/boom/boom.go | 3 +- commands/bubbleTeaTest/bubbleteaTest.go | 3 +- commands/config/config.go | 3 +- commands/config/subcommands.go | 75 ++++++++++++++++--------- commands/template/template.go | 3 +- commands/welcome/welcome.go | 3 +- config.toml | 20 ++++++- go.mod | 1 + go.sum | 2 + main.go | 18 ++++-- services/getConfigPath.go | 25 +++++++++ services/readConfig.go | 26 +++++++++ types/config.go | 23 ++++++++ 13 files changed, 166 insertions(+), 39 deletions(-) create mode 100644 services/getConfigPath.go create mode 100644 services/readConfig.go create mode 100644 types/config.go diff --git a/commands/boom/boom.go b/commands/boom/boom.go index fbf2642..bbf1b0f 100644 --- a/commands/boom/boom.go +++ b/commands/boom/boom.go @@ -2,10 +2,11 @@ package boom import ( "fmt" + "github.com/DariusKlein/kleinCommand/types" "github.com/urfave/cli/v2" ) -func Command() *cli.Command { +func Command(config types.Config) *cli.Command { return &cli.Command{ Name: "boom", diff --git a/commands/bubbleTeaTest/bubbleteaTest.go b/commands/bubbleTeaTest/bubbleteaTest.go index 2a93f90..85b9e74 100644 --- a/commands/bubbleTeaTest/bubbleteaTest.go +++ b/commands/bubbleTeaTest/bubbleteaTest.go @@ -1,11 +1,12 @@ package bubbleTeaTest import ( + "github.com/DariusKlein/kleinCommand/types" tea "github.com/charmbracelet/bubbletea" "github.com/urfave/cli/v2" ) -func Command() *cli.Command { +func Command(config types.Config) *cli.Command { return &cli.Command{ Name: "BubbleTeaTest", Usage: "USAGE OF COMMAND", diff --git a/commands/config/config.go b/commands/config/config.go index 6cf656d..2ea3b30 100644 --- a/commands/config/config.go +++ b/commands/config/config.go @@ -1,10 +1,11 @@ package config import ( + "github.com/DariusKlein/kleinCommand/types" "github.com/urfave/cli/v2" ) -func Command() *cli.Command { +func Command(config types.Config) *cli.Command { return &cli.Command{ Name: "config", Usage: "manage config file", diff --git a/commands/config/subcommands.go b/commands/config/subcommands.go index 30c56f0..13f2cb6 100644 --- a/commands/config/subcommands.go +++ b/commands/config/subcommands.go @@ -2,13 +2,13 @@ package config import ( "context" + "encoding/base64" "errors" "fmt" + "github.com/DariusKlein/kleinCommand/services" "github.com/google/go-github/github" "github.com/urfave/cli/v2" "os" - "path/filepath" - "runtime" ) func subcommands() []*cli.Command { @@ -16,38 +16,59 @@ func subcommands() []*cli.Command { { Name: "create", Usage: "Generates a new configuration file", - Action: CreatAction, + Action: creatAction, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "force", + Aliases: []string{"f"}, + Usage: "force overwrite", + }, + }, + }, + { + Name: "get", + Usage: "prints configuration file to stdout", + Action: printAction, }, } } -func CreatAction(c *cli.Context) error { - var path string - - homeDir, _ := os.UserHomeDir() - - switch runtime.GOOS { - case "windows": - path = filepath.Dir(homeDir + "\\AppData\\Local\\kleinCommand\\") - case "linux": - path = filepath.Dir(homeDir + "/.config/kleinCommand") - default: - return errors.New("unsupported platform") - } - - fmt.Println("Creating configuration file") - if err := os.MkdirAll(path, 0770); err != nil { - return err - } - client := github.NewClient(nil) - a1, _, _, _ := client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil) - - configPath := filepath.Join(path, "/config.toml") - - err := os.WriteFile(configPath, []byte(*a1.Content), 0644) +func creatAction(c *cli.Context) error { + path, configPath, err := services.GetConfigPath() if err != nil { + fmt.Println(err) + } + fmt.Println("Creating configuration file") + if err = os.MkdirAll(path, 0770); err != nil { return err } + + if _, err = os.Stat(configPath); errors.Is(err, os.ErrNotExist) || c.Bool("force") { + + client := github.NewClient(nil) + defaultConfig, _, _, _ := client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil) + + configString, _ := base64.StdEncoding.DecodeString(*defaultConfig.Content) + + err = os.WriteFile(configPath, configString, 0644) + if err != nil { + return err + } + } fmt.Println("Created: " + configPath) return nil } + +func printAction(c *cli.Context) error { + _, configPath, err := services.GetConfigPath() + if err != nil { + fmt.Println(err) + } + + file, err := os.ReadFile(configPath) + if err != nil { + return err + } + fmt.Println(string(file)) + return nil +} diff --git a/commands/template/template.go b/commands/template/template.go index d60b4c9..6cb7305 100644 --- a/commands/template/template.go +++ b/commands/template/template.go @@ -2,10 +2,11 @@ package template import ( "fmt" + "github.com/DariusKlein/kleinCommand/types" "github.com/urfave/cli/v2" ) -func Command() *cli.Command { +func Command(config types.Config) *cli.Command { return &cli.Command{ Name: "NAME", diff --git a/commands/welcome/welcome.go b/commands/welcome/welcome.go index 1063334..6e492d4 100644 --- a/commands/welcome/welcome.go +++ b/commands/welcome/welcome.go @@ -2,10 +2,11 @@ package welcome import ( "fmt" + "github.com/DariusKlein/kleinCommand/types" "github.com/urfave/cli/v2" ) -func Command() *cli.Command { +func Command(config types.Config) *cli.Command { return &cli.Command{ Name: "welcome", diff --git a/config.toml b/config.toml index 0c759a4..05dd5ad 100644 --- a/config.toml +++ b/config.toml @@ -1 +1,19 @@ -test123 \ No newline at end of file +# Example config for kleinCommand + +# Settings for kleinCommand +[settings] +server_name = "klein server" + +# Variable overrides +[variables] +string1="example" +int1=1 + +# Custom commands +[custom_commands] +# Generate commands with kleinCommand or configure here +[[custom_commands.games]] +name = "example" +status_command="example" +start_command="example" +stop_command="example" \ No newline at end of file diff --git a/go.mod b/go.mod index 476aed7..e5ec1c3 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( ) require ( + github.com/BurntSushi/toml v1.4.0 // indirect github.com/charmbracelet/x/ansi v0.1.1 // indirect github.com/charmbracelet/x/input v0.1.0 // indirect github.com/charmbracelet/x/term v0.1.1 // indirect diff --git a/go.sum b/go.sum index b32d042..2cc9be3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/charmbracelet/bubbletea v0.26.3 h1:iXyGvI+FfOWqkB2V07m1DF3xxQijxjY2j8PqiXYqasg= github.com/charmbracelet/bubbletea v0.26.3/go.mod h1:bpZHfDHTYJC5g+FBK+ptJRCQotRC+Dhh3AoMxa/2+3Q= github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk= diff --git a/main.go b/main.go index f48c3bc..6154c4b 100644 --- a/main.go +++ b/main.go @@ -6,13 +6,19 @@ import ( "github.com/DariusKlein/kleinCommand/commands/config" "github.com/DariusKlein/kleinCommand/commands/template" "github.com/DariusKlein/kleinCommand/commands/welcome" + "github.com/DariusKlein/kleinCommand/services" "github.com/urfave/cli/v2" "log" "os" ) func main() { - config.CreatAction(&cli.Context{}) + + Config, err := services.ReadConfig() + if err != nil { + log.Fatalf("Error reading config: %v", err) + } + app := &cli.App{ Name: "KleinCommand", Usage: "manage your home server", @@ -27,11 +33,11 @@ func main() { }, DefaultCommand: "help", Commands: []*cli.Command{ - template.Command(), - welcome.Command(), - boom.Command(), - bubbleTeaTest.Command(), - config.Command(), + template.Command(Config), + welcome.Command(Config), + boom.Command(Config), + bubbleTeaTest.Command(Config), + config.Command(Config), }, } diff --git a/services/getConfigPath.go b/services/getConfigPath.go new file mode 100644 index 0000000..4970d4b --- /dev/null +++ b/services/getConfigPath.go @@ -0,0 +1,25 @@ +package services + +import ( + "errors" + "os" + "path/filepath" + "runtime" +) + +func GetConfigPath() (path string, configPath string, err error) { + homeDir, _ := os.UserHomeDir() + + switch runtime.GOOS { + case "windows": + path = filepath.Dir(homeDir + "\\AppData\\Local\\kleinCommand\\") + case "linux": + path = filepath.Dir(homeDir + "/.config/kleinCommand") + default: + return "", "", errors.New("unsupported platform") + } + + configPath = filepath.Join(path, "/config.toml") + + return path, configPath, nil +} diff --git a/services/readConfig.go b/services/readConfig.go new file mode 100644 index 0000000..ddd5778 --- /dev/null +++ b/services/readConfig.go @@ -0,0 +1,26 @@ +package services + +import ( + "fmt" + "github.com/BurntSushi/toml" + "github.com/DariusKlein/kleinCommand/types" + "os" +) + +var config types.Config + +func ReadConfig() (types.Config, error) { + _, configPath, err := GetConfigPath() + if err != nil { + fmt.Println(err) + } + + file, err := os.ReadFile(configPath) + if err != nil { + return config, err + } + + _, err = toml.Decode(string(file), &config) + + return config, nil +} diff --git a/types/config.go b/types/config.go new file mode 100644 index 0000000..9c15723 --- /dev/null +++ b/types/config.go @@ -0,0 +1,23 @@ +package types + +type Config struct { + Settings Settings `toml:"settings"` + Variables Variables `toml:"variables"` + CustomCommands CustomCommands `toml:"custom_commands"` +} +type Settings struct { + ServerName string `toml:"server_name"` +} +type Variables struct { + String1 string `toml:"string1"` + Int1 int `toml:"int1"` +} +type Games struct { + Name string `toml:"name"` + StatusCommand string `toml:"status_command"` + StartCommand string `toml:"start_command"` + StopCommand string `toml:"stop_command"` +} +type CustomCommands struct { + Games []Games `toml:"games"` +}