finishing sqlite changes

- finished default/active server logic
- dev done
- needs testing
This commit is contained in:
2025-06-18 18:38:01 -04:00
parent fe37cac2da
commit c15c16be8d
19 changed files with 254 additions and 69 deletions

View File

@@ -15,8 +15,7 @@ import (
var addCmd = &cobra.Command{
Use: "add",
Example: "mctl command add",
// Short: "Saves a new server configuration",
// Long: `Saves server address, alias, port, and password.`,
Short: "Saves a new command to the database",
Run: func(cmd *cobra.Command, args []string) {
scanner := bufio.NewScanner(os.Stdin)

View File

@@ -11,12 +11,6 @@ import (
var CommandCmd = &cobra.Command{
Use: "command",
Example: "mctl command <subcommand>",
// Short: "A remote console client",
// Long: `mctl is a terminal-friendly remote console client made to manage game servers.`,
// Version: "v0.3.4",
// Run: func(cmd *cobra.Command, args []string) { },
}
func init() {
// rootCmd.AddCommand()
}
func init() {}

View File

@@ -14,8 +14,7 @@ import (
var deleteCmd = &cobra.Command{
Use: "delete",
Example: "mctl command delete <name>",
// Short: "Deletes a server",
// Long: `Deletes server configuration`,
Short: "Deletes a command from the database",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)

75
cmd/command/run.go Normal file
View File

@@ -0,0 +1,75 @@
/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package command
import (
"errors"
"fmt"
"strings"
"code.jakeyoungdev.com/jake/mctl/client"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
var (
cfgserver string
)
var runCmd = &cobra.Command{
Use: "run",
Example: "mctl command run -s <server> <command> args...",
Short: "Runs a saved command on a server",
Long: `Runs the named command with the provided args on the default/active server unless -s is specified`,
RunE: func(cmd *cobra.Command, args []string) error {
cname := args[0]
db, err := database.New()
if err != nil {
return err
}
defer db.Close()
crun, err := db.GetCmd(cname)
if err != nil {
return err
}
count := strings.Count(crun, "%s")
l := len(args)
if l-1 != count {
return fmt.Errorf("not enough arguments to fill all command placeholders, required: %d - have: %d", count, l)
}
cli, err := client.New(cfgserver)
cobra.CheckErr(err)
defer cli.Close()
var fargs []any
for _, x := range args[1:] {
fargs = append(fargs, x)
}
res, err := cli.Command(fmt.Sprintf(crun, fargs...))
if err != nil {
return err
}
fmt.Println(res)
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("name parameter required")
}
return nil
},
}
func init() {
runCmd.Flags().StringVarP(&cfgserver, "server", "s", "", "server name")
CommandCmd.AddCommand(runCmd)
}

View File

@@ -13,7 +13,7 @@ import (
var viewCmd = &cobra.Command{
Use: "view",
Example: "mctl command view",
// Short: "view all commands",
Short: "view all saved commands",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)

View File

@@ -4,7 +4,10 @@ Copyright © 2025 Jake jake.young.dev@gmail.com
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
@@ -12,18 +15,27 @@ import (
// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
// Short: "Clear all configuration",
// Long: `Clears all configuration values for mctl. [WARNING] all server configuration will be lost`,
Use: "destroy",
Short: "clears all configuration",
Long: `clear all data and drop database tables, this will delete all previously saved data and the 'inti' command
must be run again before use`,
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("Are you sure you want to destroy your config? (yes|no): ")
if scanner.Scan() {
if strings.EqualFold(scanner.Text(), "yes") {
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
err = db.Destroy()
cobra.CheckErr(err)
err = db.Destroy()
cobra.CheckErr(err)
fmt.Println("Configuration is cleared, the 'init' command must be run again.")
fmt.Println("Configuration is cleared, the 'init' command must be run again.")
}
} else {
return
}
},
}

View File

@@ -9,12 +9,9 @@ import (
)
var initCmd = &cobra.Command{
Use: "init",
Example: "mctl init",
SilenceUsage: true,
// Short: "Login to server and send commands",
// Long: `Login to default server and enter command loop. The default server
// is used unless the server flag is set`,
Use: "init",
Example: "mctl init",
Short: "initializes the database tables, must be run before mctl can be used",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
@@ -26,5 +23,5 @@ var initCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(initCmd)
}

View File

@@ -32,7 +32,7 @@ var loginCmd = &cobra.Command{
//get default server
if server == "" {
ds, err := db.GetServer("default")
ds, err := db.GetActiveServer()
cobra.CheckErr(err)
server = ds.Name

View File

@@ -16,8 +16,7 @@ var rootCmd = &cobra.Command{
Use: "mctl",
Short: "A remote console client",
Long: `mctl is a terminal-friendly remote console client made to manage game servers.`,
Version: "v0.3.4",
// Run: func(cmd *cobra.Command, args []string) { },
Version: "v0.3.4", //change version number
}
// Execute adds all child commands to the root command and sets flags appropriately.

35
cmd/server/active.go Normal file
View File

@@ -0,0 +1,35 @@
/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package server
import (
"errors"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
var activeCmd = &cobra.Command{
Use: "active",
Example: "mctl server active <server>",
Short: "sets the active server to run commands on",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
err = db.SetActiveServer(args[0])
cobra.CheckErr(err)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("server parameter missing")
}
return nil
},
}
func init() {
ServerCmd.AddCommand(activeCmd)
}

View File

@@ -20,7 +20,7 @@ var addCmd = &cobra.Command{
Use: "add",
Example: "mctl server add",
Short: "Saves a new server configuration",
Long: `Saves server address, alias, port, and password.`,
Long: `Saves server address, alias, port, and password to the database.`,
Run: func(cmd *cobra.Command, args []string) {
scanner := bufio.NewScanner(os.Stdin)

View File

@@ -13,8 +13,7 @@ import (
var deleteCmd = &cobra.Command{
Use: "delete",
Example: "mctl server delete <server>",
// Short: "Deletes a server",
// Long: `Deletes server configuration`,
Short: "deletes a server from the database",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)

View File

@@ -10,12 +10,6 @@ import (
var ServerCmd = &cobra.Command{
Use: "server",
Example: "mctl server <subcommand>",
// Short: "A remote console client",
// Long: `mctl is a terminal-friendly remote console client made to manage game servers.`,
// Version: "v0.3.4",
// Run: func(cmd *cobra.Command, args []string) { },
}
func init() {
// rootCmd.AddCommand()
}
func init() {}

View File

@@ -17,8 +17,7 @@ import (
var updateCmd = &cobra.Command{
Use: "update",
Example: "mctl server update <name>",
// Short: "Saves a new server configuration",
// Long: `Saves server address, alias, port, and password.`,
Short: "updates a saved servers password in the database",
Run: func(cmd *cobra.Command, args []string) {
//read in password using term to keep it secure/hidden from bash history
fmt.Printf("Password: ")

View File

@@ -13,7 +13,7 @@ import (
var viewCmd = &cobra.Command{
Use: "view",
Example: "mctl server view",
Short: "view all servers",
Short: "view all saved servers",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
@@ -27,7 +27,8 @@ var viewCmd = &cobra.Command{
fmt.Printf("Name: %s\n", s.Name)
fmt.Printf("Address: %s\n", s.Server)
fmt.Printf("Port: %d\n", s.Port)
fmt.Printf("Password: %s\n", s.Password)
fmt.Println("Password: [REDACTED]")
fmt.Printf("Default: %t\n", s.Default)
}
},
}