mctl/cmd/login.go

66 lines
1.3 KiB
Go
Raw Normal View History

/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package cmd
import (
"bufio"
"fmt"
"os"
"code.jakeyoungdev.com/jake/mctl/client"
"github.com/spf13/cobra"
)
var (
server string
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Example: "mctl login -s <server>",
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) {
cli, err := client.New(server)
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" {
2025-06-19 15:28:21 -04:00
fmt.Printf("RCON /> ")
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)
}
}
2025-06-19 15:28:21 -04:00
fmt.Println("Disconnected")
},
}
func init() {
loginCmd.Flags().StringVarP(&server, "server", "s", "", "server alias")
rootCmd.AddCommand(loginCmd)
}