/* 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) }