lots o progress
- added 'command' subcommand - removed viper - setup commands added - still a WIP - readme TODO update
This commit is contained in:
32
cmd/clear.go
32
cmd/clear.go
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// clearCmd represents the clear command
|
||||
var clearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "Clear config file",
|
||||
Long: `Clears all configuration values for mctl. [WARNING] all server configuration will be lost`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.Destroy()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Println("Configuration is cleared, the 'config' command must be run again.")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(clearCmd)
|
||||
}
|
50
cmd/command/add.go
Normal file
50
cmd/command/add.go
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package command
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Example: "mctl command add",
|
||||
// Short: "Saves a new server configuration",
|
||||
// Long: `Saves server address, alias, port, and password.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
//get server information
|
||||
var cfgname string
|
||||
fmt.Printf("Command alias: ")
|
||||
if scanner.Scan() {
|
||||
cfgname = scanner.Text()
|
||||
}
|
||||
|
||||
var cfgcmd string
|
||||
fmt.Printf("Command: ")
|
||||
if scanner.Scan() {
|
||||
cfgcmd = scanner.Text()
|
||||
}
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.Init()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
err = db.SaveCmd(cfgname, cfgcmd)
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
CommandCmd.AddCommand(addCmd)
|
||||
}
|
22
cmd/command/command.go
Normal file
22
cmd/command/command.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package command
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// CommandCmd is such a cool name lol
|
||||
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()
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
package command
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -11,12 +11,11 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// deleteCmd represents the delete command
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete <name>",
|
||||
Example: "mctl delete newcmd",
|
||||
Short: "Delete a saved command",
|
||||
Long: `Deletes a command stored using the save command`,
|
||||
Use: "delete",
|
||||
Example: "mctl command delete <name>",
|
||||
// Short: "Deletes a server",
|
||||
// Long: `Deletes server configuration`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
@@ -24,17 +23,17 @@ var deleteCmd = &cobra.Command{
|
||||
|
||||
err = db.DeleteCmd(args[0])
|
||||
cobra.CheckErr(err)
|
||||
fmt.Println("Command deleted")
|
||||
|
||||
fmt.Println("Command deleted!")
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("name argument is required")
|
||||
return errors.New("name parameter missing")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
CommandCmd.AddCommand(deleteCmd)
|
||||
}
|
35
cmd/command/view.go
Normal file
35
cmd/command/view.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view",
|
||||
Example: "mctl command view",
|
||||
// Short: "view all commands",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
ts, err := db.GetAllCmds()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
for _, s := range ts {
|
||||
fmt.Println("-----")
|
||||
fmt.Printf("Name: %s\n", s.Name)
|
||||
fmt.Printf("Command: %s\n", s.Command)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
CommandCmd.AddCommand(viewCmd)
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"code.jakeyoungdev.com/jake/mctl/model"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgserver string
|
||||
cfgport int
|
||||
cfgname string
|
||||
)
|
||||
|
||||
// configCmd represents the config command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Example: "mctl config -n serverAlias -s x.x.x.x -p 61695",
|
||||
Short: "Create and populate config file",
|
||||
Long: `Creates the .mctl file in the user home directory
|
||||
populating it with the server address, rcon password, and
|
||||
rcon port to be pulled when using Login command`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
//read in password using term to keep it secure/hidden from bash history
|
||||
fmt.Printf("Password: ")
|
||||
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.Init()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
err = db.SaveServer(model.Server{
|
||||
Name: cfgname,
|
||||
Server: cfgserver,
|
||||
Port: cfgport,
|
||||
Password: base64.StdEncoding.EncodeToString(ps),
|
||||
})
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
configCmd.Flags().StringVarP(&cfgserver, "server", "s", "", "server address")
|
||||
err := configCmd.MarkFlagRequired("server")
|
||||
cobra.CheckErr(err)
|
||||
configCmd.Flags().IntVarP(&cfgport, "port", "p", 0, "server rcon port")
|
||||
err = configCmd.MarkFlagRequired("port")
|
||||
cobra.CheckErr(err)
|
||||
configCmd.Flags().StringVarP(&cfgname, "name", "n", "", "server alias")
|
||||
err = configCmd.MarkFlagRequired("name")
|
||||
cobra.CheckErr(err)
|
||||
rootCmd.AddCommand(configCmd)
|
||||
}
|
32
cmd/destroy.go
Normal file
32
cmd/destroy.go
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 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`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.Destroy()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Println("Configuration is cleared, the 'init' command must be run again.")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(destroyCmd)
|
||||
}
|
30
cmd/init.go
Normal file
30
cmd/init.go
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
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`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.Init()
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(loginCmd)
|
||||
}
|
55
cmd/login.go
55
cmd/login.go
@@ -5,7 +5,6 @@ package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -15,19 +14,30 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
name string
|
||||
server string
|
||||
)
|
||||
|
||||
// loginCmd represents the login command
|
||||
var loginCmd = &cobra.Command{
|
||||
Use: "login",
|
||||
Example: "mctl login <name>",
|
||||
Example: "mctl login -s <server>",
|
||||
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.`,
|
||||
Long: `Login to default server and enter command loop. The default server
|
||||
is used unless the server flag is set`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
server := args[0]
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
//get default server
|
||||
if server == "" {
|
||||
ds, err := db.GetServer("default")
|
||||
cobra.CheckErr(err)
|
||||
|
||||
server = ds.Name
|
||||
}
|
||||
|
||||
cli, err := client.New(server)
|
||||
cobra.CheckErr(err)
|
||||
defer cli.Close()
|
||||
@@ -36,10 +46,6 @@ var loginCmd = &cobra.Command{
|
||||
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)
|
||||
@@ -56,11 +62,6 @@ 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)
|
||||
@@ -69,31 +70,9 @@ var loginCmd = &cobra.Command{
|
||||
|
||||
fmt.Printf("Disconnected from %s\n", server)
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
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
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
loginCmd.Flags().StringVarP(&server, "server", "s", "", "server alias")
|
||||
rootCmd.AddCommand(loginCmd)
|
||||
}
|
||||
|
@@ -6,6 +6,8 @@ package cmd
|
||||
import (
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/cmd/command"
|
||||
srv "code.jakeyoungdev.com/jake/mctl/cmd/server"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -31,4 +33,6 @@ func init() {
|
||||
rootCmd.CompletionOptions = cobra.CompletionOptions{
|
||||
DisableDefaultCmd: true,
|
||||
}
|
||||
rootCmd.AddCommand(srv.ServerCmd)
|
||||
rootCmd.AddCommand(command.CommandCmd) //the word command is in this four times, that can't be good.
|
||||
}
|
||||
|
87
cmd/run.go
87
cmd/run.go
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/client"
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// runCmd represents the run command
|
||||
var runCmd = &cobra.Command{
|
||||
Use: "run <name> args...",
|
||||
Example: "mctl run savedcmd 63 jake",
|
||||
SilenceUsage: true,
|
||||
Short: "Runs a previously saved command with supplied arguments on remote server",
|
||||
Long: `Loads a saved command, injects the supplied arguments into the command, and sends the command to the remove server
|
||||
printing the response`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
sc, err := db.GetCmd(args[0])
|
||||
if err == sql.ErrNoRows {
|
||||
fmt.Printf("command %s not found", args[0])
|
||||
return
|
||||
}
|
||||
cobra.CheckErr(err)
|
||||
|
||||
//convert arguments to interface
|
||||
var nargs []any
|
||||
for _, a := range args[1:] {
|
||||
nargs = append(nargs, a)
|
||||
}
|
||||
|
||||
//inject arguments
|
||||
fixed := fmt.Sprintf(sc, nargs...)
|
||||
fmt.Printf("Running saved command %s\n", fixed)
|
||||
|
||||
//create client and send command
|
||||
cli, err := client.New()
|
||||
cobra.CheckErr(err)
|
||||
defer cli.Close()
|
||||
|
||||
res, err := cli.Command(fixed)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Println(res)
|
||||
},
|
||||
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 !viper.IsSet("customcmd") {
|
||||
return errors.New("no saved commands to run")
|
||||
}
|
||||
|
||||
//ensure we have a command name
|
||||
al := len(args)
|
||||
if al == 0 {
|
||||
return errors.New("name argument is required")
|
||||
}
|
||||
|
||||
cmdMap := viper.Get("customcmd").(map[string]any)
|
||||
count := strings.Count(cmdMap[args[0]].(string), "%s")
|
||||
|
||||
//make sure enough arguments are sent to fill command placeholders
|
||||
if al < count+1 {
|
||||
return fmt.Errorf("not enough arguments to populate command. Supplied: %d, Needed: %d", al-1, count)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(runCmd)
|
||||
}
|
51
cmd/save.go
51
cmd/save.go
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// saveCmd represents the save command
|
||||
var saveCmd = &cobra.Command{
|
||||
Use: "save <name>",
|
||||
Example: "mctl save newcmd",
|
||||
Short: "Saves a server command under an alias for quick execution",
|
||||
Long: `Saves supplied command using alias <name> to allow the command to be executed using the run command. The %s placeholder can be
|
||||
used as a wildcard to be injected when running the command`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("Use %s as a wildcard in your command\n", "%s") //this is ugly
|
||||
fmt.Printf("Command: ")
|
||||
sc := bufio.NewScanner(os.Stdin)
|
||||
if sc.Scan() {
|
||||
txt := sc.Text()
|
||||
if txt != "" {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.SaveCmd(args[0], txt)
|
||||
cobra.CheckErr(err)
|
||||
fmt.Println("\nSaved!")
|
||||
}
|
||||
}
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("name argument is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(saveCmd)
|
||||
}
|
70
cmd/server/add.go
Normal file
70
cmd/server/add.go
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"code.jakeyoungdev.com/jake/mctl/model"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Example: "mctl server add",
|
||||
Short: "Saves a new server configuration",
|
||||
Long: `Saves server address, alias, port, and password.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
//get server information
|
||||
var cfgname string
|
||||
fmt.Printf("Server alias: ")
|
||||
if scanner.Scan() {
|
||||
cfgname = scanner.Text()
|
||||
}
|
||||
|
||||
var cfgaddress string
|
||||
fmt.Printf("Server address: ")
|
||||
if scanner.Scan() {
|
||||
cfgaddress = scanner.Text()
|
||||
}
|
||||
|
||||
var cfgport string
|
||||
fmt.Printf("Server port: ")
|
||||
if scanner.Scan() {
|
||||
cfgport = scanner.Text()
|
||||
}
|
||||
|
||||
//read in password using term to keep it secure/hidden from bash history
|
||||
fmt.Printf("Password: ")
|
||||
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
fp, err := strconv.Atoi(cfgport)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
err = db.SaveServer(model.Server{
|
||||
Name: cfgname,
|
||||
Server: cfgaddress,
|
||||
Port: fp,
|
||||
Password: base64.StdEncoding.EncodeToString(ps),
|
||||
})
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(addCmd)
|
||||
}
|
36
cmd/server/delete.go
Normal file
36
cmd/server/delete.go
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Example: "mctl server delete <server>",
|
||||
// Short: "Deletes a server",
|
||||
// Long: `Deletes server configuration`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.DeleteServer(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(deleteCmd)
|
||||
}
|
21
cmd/server/server.go
Normal file
21
cmd/server/server.go
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
48
cmd/server/update.go
Normal file
48
cmd/server/update.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
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.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
//read in password using term to keep it secure/hidden from bash history
|
||||
fmt.Printf("Password: ")
|
||||
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Printf("%s password updated!", args[0])
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("name command required")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(updateCmd)
|
||||
}
|
37
cmd/server/view.go
Normal file
37
cmd/server/view.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view",
|
||||
Example: "mctl server view",
|
||||
Short: "view all servers",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
ts, err := db.GetAllServers()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
for _, s := range ts {
|
||||
fmt.Println("-----")
|
||||
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)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(viewCmd)
|
||||
}
|
57
cmd/view.go
57
cmd/view.go
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// viewCmd represents the view command
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view <name>",
|
||||
Example: "mctl view test",
|
||||
Short: "View saved commands",
|
||||
Long: `Load command using the supplied name and displays it in the terminal, 'all' will list every saved command`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
if strings.EqualFold(args[0], "all") {
|
||||
cmds, err := db.GetAllCmds()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Println("\nCommands: ")
|
||||
for _, c := range cmds {
|
||||
fmt.Printf("%s - %s\n", c.Name, c.Command)
|
||||
}
|
||||
fmt.Println()
|
||||
} else {
|
||||
cmds, err := db.GetCmd(args[0])
|
||||
cobra.CheckErr(err)
|
||||
|
||||
if cmds == "" {
|
||||
fmt.Println("Command not found")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Command: %s\n", cmds)
|
||||
}
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("name argument is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(viewCmd)
|
||||
}
|
Reference in New Issue
Block a user