working commit
This commit is contained in:
@@ -4,20 +4,47 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/client"
|
||||
"mbase/pkg/mbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (util *Util) CreateAccount(cmd *cobra.Command, args []string) {
|
||||
//util.createAccountParams.Filename = args[0]
|
||||
util.createAccountParams.Username = args[0]
|
||||
util.createAccountParams.Password = args[1]
|
||||
res, err := util.createAccount(util.createAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type createAccountParams struct{}
|
||||
type createAccountResult struct{}
|
||||
type CreateAccountParams struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
type CreateAccountResult struct {
|
||||
AccountID string
|
||||
}
|
||||
|
||||
func (util *Util) createAccount(params createAccountParams) (createAccountResult, error) {
|
||||
func (util *Util) createAccount(params CreateAccountParams) (CreateAccountResult, error) {
|
||||
var err error
|
||||
res := createAccountResult{}
|
||||
res := CreateAccountResult{}
|
||||
|
||||
grpcConn, cli, err := client.NewClient(&util.access)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer grpcConn.Close()
|
||||
operParams := &mbctl.CreateAccountParams{
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
}
|
||||
ctx := context.Background()
|
||||
operRes, err := cli.CreateAccount(ctx, operParams)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.AccountID = operRes.AccountID
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -4,20 +4,41 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/client"
|
||||
"mbase/pkg/mbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (util *Util) DeleteAccount(cmd *cobra.Command, args []string) {
|
||||
//util.deleteAccountParams.Filename = args[0]
|
||||
util.deleteAccountParams.Username = args[0]
|
||||
res, err := util.deleteAccount(util.deleteAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type deleteAccountParams struct{}
|
||||
type deleteAccountResult struct{}
|
||||
type DeleteAccountParams struct {
|
||||
Username string
|
||||
}
|
||||
type DeleteAccountResult struct{}
|
||||
|
||||
func (util *Util) deleteAccount(params deleteAccountParams) (deleteAccountResult, error) {
|
||||
func (util *Util) deleteAccount(params DeleteAccountParams) (DeleteAccountResult, error) {
|
||||
var err error
|
||||
res := deleteAccountResult{}
|
||||
res := DeleteAccountResult{}
|
||||
|
||||
grpcConn, cli, err := client.NewClient(&util.access)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer grpcConn.Close()
|
||||
operParams := &mbctl.DeleteAccountParams{
|
||||
Username: params.Username,
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, err = cli.DeleteAccount(ctx, operParams)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -4,20 +4,40 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/client"
|
||||
"mbase/pkg/mbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (util *Util) ListAccounts(cmd *cobra.Command, args []string) {
|
||||
//util.listAccountsParams.Filename = args[0]
|
||||
res, err := util.listAccounts(util.listAccountsParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type listAccountsParams struct{}
|
||||
type listAccountsResult struct{}
|
||||
type ListAccountsParams struct {
|
||||
}
|
||||
type ListAccountsResult struct {
|
||||
Accounts []*mbctl.AccountShortDescr `json:"accounts"`
|
||||
}
|
||||
|
||||
func (util *Util) listAccounts(params listAccountsParams) (listAccountsResult, error) {
|
||||
func (util *Util) listAccounts(params ListAccountsParams) (ListAccountsResult, error) {
|
||||
var err error
|
||||
res := listAccountsResult{}
|
||||
res := ListAccountsResult{}
|
||||
|
||||
grpcConn, cli, err := client.NewClient(&util.access)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer grpcConn.Close()
|
||||
operParams := &mbctl.ListAccountsParams{}
|
||||
ctx := context.Background()
|
||||
operRes, err := cli.ListAccounts(ctx, operParams)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Accounts = operRes.Accounts
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -4,20 +4,45 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/client"
|
||||
"mbase/pkg/mbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||
//util.updateAccountParams.Filename = args[0]
|
||||
util.updateAccountParams.Username = args[0]
|
||||
res, err := util.updateAccount(util.updateAccountParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
type updateAccountParams struct{}
|
||||
type updateAccountResult struct{}
|
||||
type UpdateAccountParams struct {
|
||||
Username string
|
||||
NewUsername string
|
||||
NewPassword string
|
||||
}
|
||||
type UpdateAccountResult struct{}
|
||||
|
||||
func (util *Util) updateAccount(params updateAccountParams) (updateAccountResult, error) {
|
||||
func (util *Util) updateAccount(params UpdateAccountParams) (UpdateAccountResult, error) {
|
||||
var err error
|
||||
res := updateAccountResult{}
|
||||
res := UpdateAccountResult{}
|
||||
|
||||
grpcConn, cli, err := client.NewClient(&util.access)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer grpcConn.Close()
|
||||
operParams := &mbctl.UpdateAccountParams{
|
||||
Username: params.Username,
|
||||
NewPassword: params.NewPassword,
|
||||
NewUsername: params.NewUsername,
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, err = cli.UpdateAccount(ctx, operParams)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -4,64 +4,109 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"mbase/pkg/client"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Util struct {
|
||||
rootCmd *cobra.Command
|
||||
createAccountParams createAccountParams
|
||||
listAccountsParams listAccountsParams
|
||||
updateAccountParams updateAccountParams
|
||||
deleteAccountParams deleteAccountParams
|
||||
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.rootCmd
|
||||
return util.subCmd
|
||||
}
|
||||
|
||||
func (util *Util) BuildCmds() *cobra.Command {
|
||||
rootCmd := &cobra.Command{
|
||||
subCmd := &cobra.Command{
|
||||
Use: "account",
|
||||
Short: "\nAccount operations",
|
||||
SilenceUsage: true,
|
||||
}
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = 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,
|
||||
Use: "create name password ",
|
||||
Short: "Create account",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: util.CreateAccount,
|
||||
PreRun: util.SetAccess,
|
||||
}
|
||||
rootCmd.AddCommand(createAccountCmd)
|
||||
subCmd.AddCommand(createAccountCmd)
|
||||
|
||||
var listAccountsCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List accounts",
|
||||
Run: util.ListAccounts,
|
||||
Use: "list",
|
||||
Short: "List accounts",
|
||||
Run: util.ListAccounts,
|
||||
PreRun: util.SetAccess,
|
||||
}
|
||||
rootCmd.AddCommand(listAccountsCmd)
|
||||
subCmd.AddCommand(listAccountsCmd)
|
||||
|
||||
var updateAccountsCmd = &cobra.Command{
|
||||
Use: "update name|id",
|
||||
Short: "Update account",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.UpdateAccount,
|
||||
Use: "update name|id",
|
||||
Short: "Update account",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.UpdateAccount,
|
||||
PreRun: util.SetAccess,
|
||||
}
|
||||
rootCmd.AddCommand(updateAccountsCmd)
|
||||
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,
|
||||
Use: "delete name|id",
|
||||
Short: "Delete account",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: util.DeleteAccount,
|
||||
PreRun: util.SetAccess,
|
||||
}
|
||||
rootCmd.AddCommand(deleteAccountCmd)
|
||||
subCmd.AddCommand(deleteAccountCmd)
|
||||
|
||||
util.rootCmd = rootCmd
|
||||
return util.rootCmd
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user