113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package account
|
|
|
|
import (
|
|
"mbase/pkg/client"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Util struct {
|
|
subCmd *cobra.Command
|
|
createAccountParams CreateAccountParams
|
|
listAccountsParams ListAccountsParams
|
|
updateAccountParams UpdateAccountParams
|
|
deleteAccountParams DeleteAccountParams
|
|
commonParams CommonParams
|
|
access client.Access
|
|
}
|
|
|
|
func NewUtil() *Util {
|
|
return &Util{}
|
|
}
|
|
|
|
const (
|
|
defaultHostname = "localhost"
|
|
defaultPort = client.DefaultPort
|
|
)
|
|
|
|
type CommonParams struct {
|
|
Hostname string
|
|
Port uint32
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
func (util *Util) GetRooCmd() *cobra.Command {
|
|
return util.subCmd
|
|
}
|
|
|
|
func (util *Util) BuildCmds() *cobra.Command {
|
|
subCmd := &cobra.Command{
|
|
Use: "account",
|
|
Short: "\nAccount operations",
|
|
SilenceUsage: true,
|
|
}
|
|
subCmd.CompletionOptions.DisableDefaultCmd = true
|
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Hostname, "host", "H", defaultHostname, "Service hostname")
|
|
subCmd.PersistentFlags().Uint32VarP(&util.commonParams.Port, "port", "I", defaultPort, "Service port")
|
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Username, "user", "U", util.commonParams.Username, "Access username")
|
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Password, "pass", "P", util.commonParams.Password, "Access password")
|
|
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
|
|
|
vi := viper.New()
|
|
vi.SetEnvPrefix("mstore")
|
|
vi.BindEnv("user")
|
|
vi.BindEnv("pass")
|
|
util.commonParams.Username = vi.GetString("user")
|
|
util.commonParams.Password = vi.GetString("pass")
|
|
|
|
var createAccountCmd = &cobra.Command{
|
|
Use: "create name password ",
|
|
Short: "Create account",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: util.CreateAccount,
|
|
PreRun: util.SetAccess,
|
|
}
|
|
subCmd.AddCommand(createAccountCmd)
|
|
|
|
var listAccountsCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List accounts",
|
|
Run: util.ListAccounts,
|
|
PreRun: util.SetAccess,
|
|
}
|
|
subCmd.AddCommand(listAccountsCmd)
|
|
|
|
var updateAccountsCmd = &cobra.Command{
|
|
Use: "update name|id",
|
|
Short: "Update account",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: util.UpdateAccount,
|
|
PreRun: util.SetAccess,
|
|
}
|
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "N", util.updateAccountParams.NewPassword, "New password")
|
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "M", util.updateAccountParams.NewUsername, "New username")
|
|
updateAccountsCmd.MarkFlagsOneRequired("newpass", "newname")
|
|
subCmd.AddCommand(updateAccountsCmd)
|
|
|
|
var deleteAccountCmd = &cobra.Command{
|
|
Use: "delete name|id",
|
|
Short: "Delete account",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: util.DeleteAccount,
|
|
PreRun: util.SetAccess,
|
|
}
|
|
subCmd.AddCommand(deleteAccountCmd)
|
|
|
|
util.subCmd = subCmd
|
|
return util.subCmd
|
|
}
|
|
|
|
func (util *Util) SetAccess(cmd *cobra.Command, args []string) {
|
|
util.access = client.Access{
|
|
Hostname: util.commonParams.Hostname,
|
|
Port: util.commonParams.Port,
|
|
Username: util.commonParams.Username,
|
|
Password: util.commonParams.Password,
|
|
}
|
|
}
|