working commit

This commit is contained in:
2026-06-06 18:53:34 +02:00
parent 25d01fc940
commit 52dd2cef20
+29 -8
View File
@@ -5,14 +5,16 @@ package grant
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"mbase/app/operator" "mbase/app/operator"
) )
type Util struct { type Util struct {
oper *operator.Logic oper *operator.Logic
rootCmd *cobra.Command subCmd *cobra.Command
createGrantParams createGrantParams createGrantParams createGrantParams
deleteGrantParams deleteGrantParams deleteGrantParams deleteGrantParams
commonParams CommonParams
} }
func NewUtil(oper *operator.Logic) *Util { func NewUtil(oper *operator.Logic) *Util {
@@ -21,17 +23,36 @@ func NewUtil(oper *operator.Logic) *Util {
} }
} }
type CommonParams struct {
Username string
Password string
}
func (util *Util) GetRooCmd() *cobra.Command { func (util *Util) GetRooCmd() *cobra.Command {
return util.rootCmd return util.subCmd
} }
func (util *Util) BuildCmds() *cobra.Command { func (util *Util) BuildCmds() *cobra.Command {
rootCmd := &cobra.Command{ subCmd := &cobra.Command{
Use: "grant", Use: "grant",
Short: "\nGrant operations", Short: "\nGrant operations",
SilenceUsage: true, SilenceUsage: true,
} }
rootCmd.CompletionOptions.DisableDefaultCmd = 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{ var createGrantCmd = &cobra.Command{
Use: "create name|id password", Use: "create name|id password",
@@ -39,7 +60,7 @@ func (util *Util) BuildCmds() *cobra.Command {
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
Run: util.CreateGrant, Run: util.CreateGrant,
} }
rootCmd.AddCommand(createGrantCmd) subCmd.AddCommand(createGrantCmd)
var deleteGrantCmd = &cobra.Command{ var deleteGrantCmd = &cobra.Command{
Use: "delete name|id grant", Use: "delete name|id grant",
@@ -47,8 +68,8 @@ func (util *Util) BuildCmds() *cobra.Command {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: util.DeleteGrant, Run: util.DeleteGrant,
} }
rootCmd.AddCommand(deleteGrantCmd) subCmd.AddCommand(deleteGrantCmd)
util.rootCmd = rootCmd util.subCmd = subCmd
return util.rootCmd return util.subCmd
} }