90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package grant
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"mbase/app/operator"
|
|
)
|
|
|
|
type Util struct {
|
|
oper *operator.Operator
|
|
operID string
|
|
subCmd *cobra.Command
|
|
setGrantParams SetGrantParams
|
|
deleteGrantParams DeleteGrantParams
|
|
commonParams CommonParams
|
|
}
|
|
|
|
func NewUtil(oper *operator.Operator) *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: "grant",
|
|
Short: "\nGrant 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 setGrantCmd = &cobra.Command{
|
|
Use: "set name|id password",
|
|
Short: "Set grant for account",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: util.SetGrant,
|
|
}
|
|
subCmd.AddCommand(setGrantCmd)
|
|
|
|
var deleteGrantCmd = &cobra.Command{
|
|
Use: "delete name|id grant",
|
|
Short: "Delete grant for account",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: util.DeleteGrant,
|
|
}
|
|
subCmd.AddCommand(deleteGrantCmd)
|
|
|
|
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
|
|
}
|