mctl/cmd/login.go
jake f943639a88 lots o updates
- moved mcr code to package
- added the save, run, and view commands
- commands can now be saved
- saved commands support placeholders
2025-04-17 11:20:39 -04:00

62 lines
1.2 KiB
Go

/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package cmd
import (
"bufio"
"fmt"
"os"
"code.jakeyoungdev.com/jake/mctl/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Example: "mctl login",
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()
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
}
//mctl exits command terminal
if runningCmd == "mctl" {
break
}
res, err := cli.Command(runningCmd)
cobra.CheckErr(err)
fmt.Printf("\n%s\n", res)
}
}
fmt.Printf("Disconnected from %s\n", server)
},
}
func init() {
rootCmd.AddCommand(loginCmd)
}