mctl/cmd/server/update.go

55 lines
1.1 KiB
Go
Raw Permalink Normal View History

/*
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: "updates a saved servers password in the database",
SilenceUsage: true,
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))
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
}
}
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)
}