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:
59
client/mcr.go
Normal file
59
client/mcr.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/cryptography"
|
||||
"github.com/jake-young-dev/mcr"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
/*
|
||||
This is a simple wrapper for the MCR client to provide easy use of mcr without having to manually
|
||||
decrypt the password/hit viper each time.
|
||||
*/
|
||||
|
||||
type Client struct {
|
||||
cli *mcr.Client
|
||||
}
|
||||
|
||||
type IClient interface {
|
||||
Close()
|
||||
Command(cmd string) (string, error)
|
||||
}
|
||||
|
||||
// creates a new mcr client using saved credentials and decrypted password
|
||||
func New() (*Client, error) {
|
||||
//grab saved credentials
|
||||
server := viper.Get("server").(string)
|
||||
password := viper.Get("password").(string)
|
||||
port := viper.Get("port").(int)
|
||||
fmt.Printf("Logging into %s on port %d\n", server, port)
|
||||
|
||||
//decrypt password
|
||||
pt, err := cryptography.DecryptPassword([]byte(password))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//connect to game server
|
||||
cli := mcr.NewClient(server, mcr.WithPort(port))
|
||||
err = cli.Connect(string(pt))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
cli: cli,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// closes client connection
|
||||
func (c *Client) Close() error {
|
||||
return c.cli.Close()
|
||||
}
|
||||
|
||||
// sends command to server, only exists to prevent exposing cli field
|
||||
func (c *Client) Command(cmd string) (string, error) {
|
||||
return c.cli.Command(cmd)
|
||||
}
|
Reference in New Issue
Block a user