mctl/cmd/config.go

66 lines
1.6 KiB
Go
Raw Normal View History

/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package cmd
import (
"encoding/base64"
"fmt"
"os"
"code.jakeyoungdev.com/jake/mctl/database"
"code.jakeyoungdev.com/jake/mctl/model"
"github.com/spf13/cobra"
"golang.org/x/term"
)
var (
cfgserver string
cfgport int
cfgname string
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Example: "mctl config -n serverAlias -s x.x.x.x -p 61695",
Short: "Create and populate config file",
Long: `Creates the .mctl file in the user home directory
populating it with the server address, rcon password, and
rcon port to be pulled when using Login command`,
Run: func(cmd *cobra.Command, args []string) {
//read in password using term to keep it secure/hidden from bash history
fmt.Printf("Password: ")
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
cobra.CheckErr(err)
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
err = db.Init()
cobra.CheckErr(err)
err = db.SaveServer(model.Server{
Name: cfgname,
Server: cfgserver,
Port: cfgport,
Password: base64.StdEncoding.EncodeToString(ps),
})
cobra.CheckErr(err)
},
}
func init() {
configCmd.Flags().StringVarP(&cfgserver, "server", "s", "", "server address")
err := configCmd.MarkFlagRequired("server")
cobra.CheckErr(err)
configCmd.Flags().IntVarP(&cfgport, "port", "p", 0, "server rcon port")
err = configCmd.MarkFlagRequired("port")
cobra.CheckErr(err)
configCmd.Flags().StringVarP(&cfgname, "name", "n", "", "server alias")
err = configCmd.MarkFlagRequired("name")
cobra.CheckErr(err)
rootCmd.AddCommand(configCmd)
}