2025-04-16 16:18:40 -04:00
|
|
|
/*
|
|
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
"code.jakeyoungdev.com/jake/mctl/client"
|
2025-04-16 16:18:40 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// loginCmd represents the login command
|
|
|
|
var loginCmd = &cobra.Command{
|
2025-04-17 15:40:54 +00:00
|
|
|
Use: "login",
|
|
|
|
Example: "mctl login",
|
|
|
|
Short: "Login to server and send commands",
|
2025-04-16 16:18:40 -04:00
|
|
|
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
|
2025-04-17 15:40:54 +00:00
|
|
|
server := viper.Get("server").(string)
|
|
|
|
cli, err := client.New()
|
2025-04-16 16:18:40 -04:00
|
|
|
cobra.CheckErr(err)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
//start command loop
|
|
|
|
fmt.Println("Connected! Type 'mctl' to close")
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
var runningCmd string
|
|
|
|
for runningCmd != "mctl" {
|
|
|
|
fmt.Printf("RCON@%s /> ", server)
|
|
|
|
|
|
|
|
if scanner.Scan() {
|
|
|
|
runningCmd = scanner.Text()
|
|
|
|
|
|
|
|
if runningCmd == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
//mctl exits command terminal
|
2025-04-16 16:18:40 -04:00
|
|
|
if runningCmd == "mctl" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cli.Command(runningCmd)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Printf("\n%s\n", res)
|
|
|
|
}
|
|
|
|
}
|
2025-04-16 16:29:51 -04:00
|
|
|
|
|
|
|
fmt.Printf("Disconnected from %s\n", server)
|
2025-04-16 16:18:40 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(loginCmd)
|
|
|
|
}
|