115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package account
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"mbase/app/operator"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const (
|
|
defaultHostname = "localhost:1025"
|
|
)
|
|
|
|
type Util struct {
|
|
oper *operator.Logic
|
|
subCmd *cobra.Command
|
|
createAccountParams CreateAccountParams
|
|
listAccountsParams ListAccountsParams
|
|
updateAccountParams UpdateAccountParams
|
|
deleteAccountParams DeleteAccountParams
|
|
commonParams CommonParams
|
|
operID string
|
|
}
|
|
|
|
func NewUtil(oper *operator.Logic) *Util {
|
|
return &Util{
|
|
oper: oper,
|
|
}
|
|
}
|
|
|
|
type CommonParams struct {
|
|
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,
|
|
PersistentPreRunE: util.ValidateOper,
|
|
}
|
|
subCmd.CompletionOptions.DisableDefaultCmd = true
|
|
|
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Username, "user", "U", util.commonParams.Username, "Username")
|
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Password, "pass", "P", util.commonParams.Password, "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,
|
|
}
|
|
subCmd.AddCommand(createAccountCmd)
|
|
|
|
var listAccountsCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List accounts",
|
|
Run: util.ListAccounts,
|
|
}
|
|
subCmd.AddCommand(listAccountsCmd)
|
|
|
|
var updateAccountsCmd = &cobra.Command{
|
|
Use: "update name|id",
|
|
Short: "Update account",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: util.UpdateAccount,
|
|
}
|
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Password, "newpass", "N", util.updateAccountParams.Password, "New password")
|
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Username, "newname", "M", util.updateAccountParams.Username, "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,
|
|
}
|
|
subCmd.AddCommand(deleteAccountCmd)
|
|
|
|
util.subCmd = subCmd
|
|
return util.subCmd
|
|
}
|
|
|
|
func (util *Util) ValidateOper(cmd *cobra.Command, args []string) error {
|
|
var err error
|
|
ctx := context.Background()
|
|
authOk, operID, err := util.oper.ValidateAccount(ctx, util.commonParams.Username, util.commonParams.Password)
|
|
if !authOk {
|
|
err := fmt.Errorf("Authentification error")
|
|
return err
|
|
}
|
|
util.operID = operID
|
|
return err
|
|
}
|