finishing sqlite changes
- finished default/active server logic - dev done - needs testing
This commit is contained in:
@@ -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)
|
||||
|
||||
|
@@ -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() {}
|
||||
|
@@ -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
75
cmd/command/run.go
Normal 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)
|
||||
}
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user