dev/command-saving (#1)
Reviewed-on: #1 Co-authored-by: jake <jake.young.dev@gmail.com> Co-committed-by: jake <jake.young.dev@gmail.com>
This commit is contained in:
33
cmd/login.go
33
cmd/login.go
@@ -5,45 +5,25 @@ package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jake-young-dev/mcr"
|
||||
"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",
|
||||
Short: "Login to server and send commands",
|
||||
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")
|
||||
password := viper.Get("password")
|
||||
port := viper.Get("port")
|
||||
fmt.Printf("Logging into %s on port %d\n", server, port)
|
||||
|
||||
//setup decrypter
|
||||
nonce := viper.Get("nonce")
|
||||
block, err := aes.NewCipher([]byte(viper.Get("device").(string)))
|
||||
cobra.CheckErr(err)
|
||||
aesg, err := cipher.NewGCM(block)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
//decrypt password
|
||||
pwd := []byte(password.(string))
|
||||
nn := []byte(nonce.(string))
|
||||
pt, err := aesg.Open(nil, nn, pwd, nil)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
//connect to game server
|
||||
cli := mcr.NewClient(server.(string), mcr.WithPort(port.(int)))
|
||||
err = cli.Connect(string(pt))
|
||||
server := viper.Get("server").(string)
|
||||
cli, err := client.New()
|
||||
cobra.CheckErr(err)
|
||||
defer cli.Close()
|
||||
|
||||
@@ -61,6 +41,7 @@ var loginCmd = &cobra.Command{
|
||||
continue
|
||||
}
|
||||
|
||||
//mctl exits command terminal
|
||||
if runningCmd == "mctl" {
|
||||
break
|
||||
}
|
||||
|
Reference in New Issue
Block a user