config read + types added

Config added
config get from server
This commit is contained in:
darius 2024-05-24 20:32:42 +02:00
parent 186ee99763
commit 00748f4a88
13 changed files with 166 additions and 39 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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")
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 {
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)
a1, _, _, _ := client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil)
defaultConfig, _, _, _ := client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil)
configPath := filepath.Join(path, "/config.toml")
configString, _ := base64.StdEncoding.DecodeString(*defaultConfig.Content)
err := os.WriteFile(configPath, []byte(*a1.Content), 0644)
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
}

View File

@ -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",

View File

@ -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",

View File

@ -1 +1,19 @@
test123
# 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"

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

18
main.go
View File

@ -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),
},
}

25
services/getConfigPath.go Normal file
View File

@ -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
}

26
services/readConfig.go Normal file
View File

@ -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
}

23
types/config.go Normal file
View File

@ -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"`
}