2024-05-24 19:41:44 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func subcommands() []*cli.Command {
|
|
|
|
|
return []*cli.Command{
|
|
|
|
|
{
|
|
|
|
|
Name: "create",
|
|
|
|
|
Usage: "Generates a new configuration file",
|
2024-05-24 19:46:23 +02:00
|
|
|
Action: CreatAction,
|
2024-05-24 19:41:44 +02:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 19:46:23 +02:00
|
|
|
func CreatAction(c *cli.Context) error {
|
2024-05-24 19:41:44 +02:00
|
|
|
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)
|
2024-05-24 19:46:23 +02:00
|
|
|
a1, _, _, _ := client.Repositories.GetContents(context.Background(), "DariusKlein", "kleinCommand", "config.toml", nil)
|
|
|
|
|
|
2024-05-24 19:41:44 +02:00
|
|
|
configPath := filepath.Join(path, "/config.toml")
|
|
|
|
|
|
2024-05-24 19:46:23 +02:00
|
|
|
err := os.WriteFile(configPath, []byte(*a1.Content), 0644)
|
2024-05-24 19:41:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Created: " + configPath)
|
|
|
|
|
return nil
|
|
|
|
|
}
|