Files
gmbase/cmd/mbaseadmin/grant/util.go
T
2026-06-06 18:53:34 +02:00

76 lines
1.6 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package grant
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"mbase/app/operator"
)
type Util struct {
oper *operator.Logic
subCmd *cobra.Command
createGrantParams createGrantParams
deleteGrantParams deleteGrantParams
commonParams CommonParams
}
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: "grant",
Short: "\nGrant operations",
SilenceUsage: true,
}
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 createGrantCmd = &cobra.Command{
Use: "create name|id password",
Short: "Create grant",
Args: cobra.ExactArgs(2),
Run: util.CreateGrant,
}
subCmd.AddCommand(createGrantCmd)
var deleteGrantCmd = &cobra.Command{
Use: "delete name|id grant",
Short: "Delete grant",
Args: cobra.ExactArgs(1),
Run: util.DeleteGrant,
}
subCmd.AddCommand(deleteGrantCmd)
util.subCmd = subCmd
return util.subCmd
}