49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
|
/*
|
||
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||
|
*/
|
||
|
package server
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
|
||
|
"code.jakeyoungdev.com/jake/mctl/database"
|
||
|
"github.com/spf13/cobra"
|
||
|
"golang.org/x/term"
|
||
|
)
|
||
|
|
||
|
var updateCmd = &cobra.Command{
|
||
|
Use: "update",
|
||
|
Example: "mctl server update <name>",
|
||
|
// Short: "Saves a new server configuration",
|
||
|
// Long: `Saves server address, alias, port, and password.`,
|
||
|
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.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps))
|
||
|
cobra.CheckErr(err)
|
||
|
|
||
|
fmt.Printf("%s password updated!", args[0])
|
||
|
},
|
||
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||
|
if len(args) == 0 {
|
||
|
return errors.New("name command required")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
ServerCmd.AddCommand(updateCmd)
|
||
|
}
|