config read + types added
Config added config get from server
This commit is contained in:
parent
186ee99763
commit
00748f4a88
@ -2,10 +2,11 @@ package boom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/DariusKlein/kleinCommand/types"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Command() *cli.Command {
|
func Command(config types.Config) *cli.Command {
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "boom",
|
Name: "boom",
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
package bubbleTeaTest
|
package bubbleTeaTest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/DariusKlein/kleinCommand/types"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Command() *cli.Command {
|
func Command(config types.Config) *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "BubbleTeaTest",
|
Name: "BubbleTeaTest",
|
||||||
Usage: "USAGE OF COMMAND",
|
Usage: "USAGE OF COMMAND",
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/DariusKlein/kleinCommand/types"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Command() *cli.Command {
|
func Command(config types.Config) *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "config",
|
Name: "config",
|
||||||
Usage: "manage config file",
|
Usage: "manage config file",
|
||||||
|
|||||||
@ -2,13 +2,13 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/DariusKlein/kleinCommand/services"
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func subcommands() []*cli.Command {
|
func subcommands() []*cli.Command {
|
||||||
@ -16,38 +16,59 @@ func subcommands() []*cli.Command {
|
|||||||
{
|
{
|
||||||
Name: "create",
|
Name: "create",
|
||||||
Usage: "Generates a new configuration file",
|
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 {
|
func creatAction(c *cli.Context) error {
|
||||||
var path string
|
path, configPath, err := services.GetConfigPath()
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Creating configuration file")
|
||||||
|
if err = os.MkdirAll(path, 0770); err != nil {
|
||||||
return err
|
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)
|
fmt.Println("Created: " + configPath)
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@ -2,10 +2,11 @@ package template
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/DariusKlein/kleinCommand/types"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Command() *cli.Command {
|
func Command(config types.Config) *cli.Command {
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "NAME",
|
Name: "NAME",
|
||||||
|
|||||||
@ -2,10 +2,11 @@ package welcome
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/DariusKlein/kleinCommand/types"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Command() *cli.Command {
|
func Command(config types.Config) *cli.Command {
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "welcome",
|
Name: "welcome",
|
||||||
|
|||||||
20
config.toml
20
config.toml
@ -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
1
go.mod
@ -9,6 +9,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||||
github.com/charmbracelet/x/ansi v0.1.1 // indirect
|
github.com/charmbracelet/x/ansi v0.1.1 // indirect
|
||||||
github.com/charmbracelet/x/input v0.1.0 // indirect
|
github.com/charmbracelet/x/input v0.1.0 // indirect
|
||||||
github.com/charmbracelet/x/term v0.1.1 // indirect
|
github.com/charmbracelet/x/term v0.1.1 // indirect
|
||||||
|
|||||||
2
go.sum
2
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 h1:iXyGvI+FfOWqkB2V07m1DF3xxQijxjY2j8PqiXYqasg=
|
||||||
github.com/charmbracelet/bubbletea v0.26.3/go.mod h1:bpZHfDHTYJC5g+FBK+ptJRCQotRC+Dhh3AoMxa/2+3Q=
|
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=
|
github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=
|
||||||
|
|||||||
18
main.go
18
main.go
@ -6,13 +6,19 @@ import (
|
|||||||
"github.com/DariusKlein/kleinCommand/commands/config"
|
"github.com/DariusKlein/kleinCommand/commands/config"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/template"
|
"github.com/DariusKlein/kleinCommand/commands/template"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/welcome"
|
"github.com/DariusKlein/kleinCommand/commands/welcome"
|
||||||
|
"github.com/DariusKlein/kleinCommand/services"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.CreatAction(&cli.Context{})
|
|
||||||
|
Config, err := services.ReadConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error reading config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Name: "KleinCommand",
|
Name: "KleinCommand",
|
||||||
Usage: "manage your home server",
|
Usage: "manage your home server",
|
||||||
@ -27,11 +33,11 @@ func main() {
|
|||||||
},
|
},
|
||||||
DefaultCommand: "help",
|
DefaultCommand: "help",
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
template.Command(),
|
template.Command(Config),
|
||||||
welcome.Command(),
|
welcome.Command(Config),
|
||||||
boom.Command(),
|
boom.Command(Config),
|
||||||
bubbleTeaTest.Command(),
|
bubbleTeaTest.Command(Config),
|
||||||
config.Command(),
|
config.Command(Config),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
25
services/getConfigPath.go
Normal file
25
services/getConfigPath.go
Normal 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
26
services/readConfig.go
Normal 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
23
types/config.go
Normal 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"`
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user