starting sqlite rewrite

- adding db connector
- starting rewrite of commands
- WIP
This commit is contained in:
2025-06-16 19:17:52 -04:00
parent 58ece42142
commit 77bb3166c4
14 changed files with 421 additions and 152 deletions

View File

@@ -10,28 +10,36 @@ import (
"os"
"code.jakeyoungdev.com/jake/mctl/client"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
name string
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Example: "mctl login",
Example: "mctl login <name>",
SilenceUsage: true,
Short: "Login to server and send commands",
Long: `Login to server using saved config and enter command loop
sending commands to server and printing the response.`,
Run: func(cmd *cobra.Command, args []string) {
//grab saved credentials
server := viper.Get("server").(string)
cli, err := client.New()
server := args[0]
cli, err := client.New(server)
cobra.CheckErr(err)
defer cli.Close()
//start command loop
fmt.Println("Connected! Type 'mctl' to close")
scanner := bufio.NewScanner(os.Stdin)
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
var runningCmd string
for runningCmd != "mctl" {
fmt.Printf("RCON@%s /> ", server)
@@ -48,6 +56,11 @@ var loginCmd = &cobra.Command{
break
}
if runningCmd == ".run" {
}
//hmm this gets weird af tbh
dbcmd, err := db.GetCmd(runningCmd)
res, err := cli.Command(runningCmd)
cobra.CheckErr(err)
fmt.Printf("\n%s\n", res)
@@ -57,9 +70,24 @@ var loginCmd = &cobra.Command{
fmt.Printf("Disconnected from %s\n", server)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
//ensure config command has been run
if !viper.IsSet("server") || !viper.IsSet("password") || !viper.IsSet("port") {
return errors.New("the 'config' command must be run before you can interact with servers")
if len(args) == 0 {
return errors.New("must specify which server to login to")
}
srv := args[0]
db, err := database.New()
if err != nil {
return err
}
defer db.Close()
server, err := db.GetServer(srv)
if err != nil {
return err
}
if server.Name == "" {
return fmt.Errorf("server %s not found", server.Name)
}
return nil