2025-06-17 23:12:49 -04:00
|
|
|
/*
|
|
|
|
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{
|
2025-06-19 17:23:17 -04:00
|
|
|
Use: "update",
|
|
|
|
Example: "mctl server update <name>",
|
|
|
|
Short: "updates a saved servers password in the database",
|
|
|
|
SilenceUsage: true,
|
2025-06-17 23:12:49 -04:00
|
|
|
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))
|
2025-06-19 17:28:56 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == ErrInit {
|
|
|
|
fmt.Println(ErrInitRsp)
|
|
|
|
return
|
|
|
|
}
|
2025-06-19 17:23:17 -04:00
|
|
|
}
|
2025-06-17 23:12:49 -04:00
|
|
|
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)
|
|
|
|
}
|