working commit
This commit is contained in:
+6
-10
@@ -15,9 +15,9 @@ import (
|
|||||||
const (
|
const (
|
||||||
defaultHostname = "localhost"
|
defaultHostname = "localhost"
|
||||||
|
|
||||||
configFilename = "maind.yaml"
|
configFilename = "mbased.yaml"
|
||||||
logFilename = "maind.log"
|
logFilename = "mbased.log"
|
||||||
pidFilename = "maind.pid"
|
pidFilename = "mbased.pid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -93,12 +93,6 @@ func (conf *Config) ReadFile() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
conf.Normalize()
|
|
||||||
err = conf.Validate()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,10 +134,12 @@ func (conf *Config) String() (string, error) {
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conf *Config) Normalize() {
|
func (conf *Config) Normalize() error {
|
||||||
|
var err error
|
||||||
if conf.Service.Portnum == 0 {
|
if conf.Service.Portnum == 0 {
|
||||||
conf.Service.Portnum = client.DefaultPort
|
conf.Service.Portnum = client.DefaultPort
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conf *Config) Validate() error {
|
func (conf *Config) Validate() error {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func (hand *Handler) Authentificate(ctx context.Context) (string, error) {
|
|||||||
}
|
}
|
||||||
username := meta["username"][0]
|
username := meta["username"][0]
|
||||||
password := meta["password"][0]
|
password := meta["password"][0]
|
||||||
validated, accountID, err := hand.lg.ValidateAcount(ctx, username, password)
|
validated, accountID, err := hand.lg.ValidateAccount(ctx, username, password)
|
||||||
if !validated {
|
if !validated {
|
||||||
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
||||||
return accountID, err
|
return accountID, err
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func NewDatabase(datapath string) (*Database, error) {
|
|||||||
|
|
||||||
func (db *Database) OpenDatabase() error {
|
func (db *Database) OpenDatabase() error {
|
||||||
var err error
|
var err error
|
||||||
dbPath := filepath.Join(db.datapath, "certmanager.db")
|
dbPath := filepath.Join(db.datapath, "main.db")
|
||||||
db.db, err = sqlx.Open("sqlite3", dbPath)
|
db.db, err = sqlx.Open("sqlite3", dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"mbase/pkg/mbctl"
|
"mbase/pkg/mbctl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (lg *Logic) ValidateAcount(ctx context.Context, username, password string) (bool, string, error) {
|
func (lg *Logic) ValidateAccount(ctx context.Context, username, password string) (bool, string, error) {
|
||||||
var err error
|
var err error
|
||||||
var accountID string
|
var accountID string
|
||||||
var valid bool
|
var valid bool
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultSeedUsername = "certman"
|
defaultSeedUsername = "mbase"
|
||||||
defaultSeedPassword = "certman"
|
defaultSeedPassword = "mbase"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (lg *Logic) CleanDatabase(ctx context.Context) error {
|
func (lg *Logic) CleanDatabase(ctx context.Context) error {
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (srv *Server) Configure() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
srv.sfile = filepath.Join(srv.conf.DataDir, "certmanager.yaml")
|
srv.sfile = filepath.Join(srv.conf.DataDir, "mbase.yaml")
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"mbase/app/operator"
|
"mbase/app/operator"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -17,11 +20,12 @@ const (
|
|||||||
type Util struct {
|
type Util struct {
|
||||||
oper *operator.Logic
|
oper *operator.Logic
|
||||||
subCmd *cobra.Command
|
subCmd *cobra.Command
|
||||||
createAccountParams createAccountParams
|
createAccountParams CreateAccountParams
|
||||||
listAccountsParams listAccountsParams
|
listAccountsParams ListAccountsParams
|
||||||
updateAccountParams updateAccountParams
|
updateAccountParams UpdateAccountParams
|
||||||
deleteAccountParams deleteAccountParams
|
deleteAccountParams DeleteAccountParams
|
||||||
commonParams CommonParams
|
commonParams CommonParams
|
||||||
|
operID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUtil(oper *operator.Logic) *Util {
|
func NewUtil(oper *operator.Logic) *Util {
|
||||||
@@ -44,6 +48,7 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
Use: "account",
|
Use: "account",
|
||||||
Short: "\nAccount operations",
|
Short: "\nAccount operations",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
|
PersistentPreRunE: util.ValidateOper,
|
||||||
}
|
}
|
||||||
subCmd.CompletionOptions.DisableDefaultCmd = true
|
subCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
|
|
||||||
@@ -79,6 +84,8 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: util.UpdateAccount,
|
Run: util.UpdateAccount,
|
||||||
}
|
}
|
||||||
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Password, "pass", "P", util.updateAccountParams.Password, "New Password")
|
||||||
|
//updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Username, "user", "U", util.updateAccountParams.Username, "New Username")
|
||||||
subCmd.AddCommand(updateAccountsCmd)
|
subCmd.AddCommand(updateAccountsCmd)
|
||||||
|
|
||||||
var deleteAccountCmd = &cobra.Command{
|
var deleteAccountCmd = &cobra.Command{
|
||||||
@@ -92,3 +99,15 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
util.subCmd = subCmd
|
util.subCmd = subCmd
|
||||||
return util.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
|
||||||
|
}
|
||||||
@@ -4,50 +4,40 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) CreateAccount(cmd *cobra.Command, args []string) {
|
func (util *Util) CreateAccount(cmd *cobra.Command, args []string) {
|
||||||
//util.createAccountParams.Filename = args[0]
|
util.createAccountParams.Username = args[0]
|
||||||
res, err := util.createAccount(util.createAccountParams)
|
util.createAccountParams.Password = args[1]
|
||||||
|
res, err := util.createAccount(&util.createAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type createAccountParams struct {
|
type CreateAccountParams struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
type createAccountResult struct{}
|
type CreateAccountResult struct {
|
||||||
|
AccountID string `json:"accountId"`
|
||||||
|
}
|
||||||
|
|
||||||
func (util *Util) createAccount(params createAccountParams) (createAccountResult, error) {
|
func (util *Util) createAccount(params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||||
var err error
|
var err error
|
||||||
res := createAccountResult{}
|
res := &CreateAccountResult{}
|
||||||
/*
|
ctx := context.Background()
|
||||||
operParams := &mbctl.CreateAccountParams{
|
operParams := &mbctl.CreateAccountParams{
|
||||||
Username: params.Username,
|
Username: params.Username,
|
||||||
Password: params.Password,
|
Password: params.Password,
|
||||||
}
|
}
|
||||||
res, err = util.lg.CreateAccount(ctx, operID, params)
|
operRes, err := util.oper.CreateAccount(ctx, util.operID, operParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
*/
|
res.AccountID = operRes.AccountID
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func (util *Util) CreateAccount(ctx context.Context, operID string) (*mbctl.CreateAccountResult, error) {
|
|
||||||
var err error
|
|
||||||
res := &mbctl.CreateAccountResult{}
|
|
||||||
|
|
||||||
params := &mbctl.CreateAccountParams{
|
|
||||||
Username: util.username,
|
|
||||||
Password: util.password,
|
|
||||||
}
|
|
||||||
res, err = util.lg.CreateAccount(ctx, operID, params)
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|||||||
@@ -4,20 +4,34 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) DeleteAccount(cmd *cobra.Command, args []string) {
|
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)
|
res, err := util.deleteAccount(&util.deleteAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type deleteAccountParams struct{}
|
type DeleteAccountParams struct {
|
||||||
type deleteAccountResult 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
|
var err error
|
||||||
res := deleteAccountResult{}
|
res := &DeleteAccountResult{}
|
||||||
|
ctx := context.Background()
|
||||||
|
operParams := &mbctl.DeleteAccountParams{
|
||||||
|
Username: params.Username,
|
||||||
|
}
|
||||||
|
_, err = util.oper.DeleteAccount(ctx, util.operID, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,32 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) ListAccounts(cmd *cobra.Command, args []string) {
|
func (util *Util) ListAccounts(cmd *cobra.Command, args []string) {
|
||||||
//util.listAccountsParams.Filename = args[0]
|
res, err := util.listAccounts(&util.listAccountsParams)
|
||||||
res, err := util.listAccounts(util.listAccountsParams)
|
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type listAccountsParams struct{}
|
type ListAccountsParams struct{}
|
||||||
type listAccountsResult 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
|
var err error
|
||||||
res := listAccountsResult{}
|
ctx := context.Background()
|
||||||
|
res := &ListAccountsResult{}
|
||||||
|
operParams := &mbctl.ListAccountsParams{}
|
||||||
|
operRes, err := util.oper.ListAccounts(ctx, util.operID, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
res.Accounts = operRes.Accounts
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,38 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) {
|
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)
|
res, err := util.updateAccount(&util.updateAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateAccountParams struct{}
|
type UpdateAccountParams struct {
|
||||||
type updateAccountResult struct{}
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
type UpdateAccountResult struct {
|
||||||
|
AccountID string
|
||||||
|
}
|
||||||
|
|
||||||
func (util *Util) updateAccount(params updateAccountParams) (updateAccountResult, error) {
|
func (util *Util) updateAccount(params *UpdateAccountParams) (*UpdateAccountResult, error) {
|
||||||
var err error
|
var err error
|
||||||
res := updateAccountResult{}
|
res := &UpdateAccountResult{}
|
||||||
|
ctx := context.Background()
|
||||||
|
operParams := &mbctl.UpdateAccountParams{
|
||||||
|
Username: params.Username,
|
||||||
|
//Password: params.Password,
|
||||||
|
}
|
||||||
|
_, err = util.oper.UpdateAccount(ctx, util.operID, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
package grant
|
package grant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"mbase/app/operator"
|
"mbase/app/operator"
|
||||||
@@ -11,6 +14,7 @@ import (
|
|||||||
|
|
||||||
type Util struct {
|
type Util struct {
|
||||||
oper *operator.Logic
|
oper *operator.Logic
|
||||||
|
operID string
|
||||||
subCmd *cobra.Command
|
subCmd *cobra.Command
|
||||||
createGrantParams createGrantParams
|
createGrantParams createGrantParams
|
||||||
deleteGrantParams deleteGrantParams
|
deleteGrantParams deleteGrantParams
|
||||||
@@ -37,6 +41,7 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
Use: "grant",
|
Use: "grant",
|
||||||
Short: "\nGrant operations",
|
Short: "\nGrant operations",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
|
PersistentPreRunE: util.ValidateOper,
|
||||||
}
|
}
|
||||||
subCmd.CompletionOptions.DisableDefaultCmd = true
|
subCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
|
|
||||||
@@ -51,9 +56,6 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
util.commonParams.Username = vi.GetString("user")
|
util.commonParams.Username = vi.GetString("user")
|
||||||
util.commonParams.Password = vi.GetString("pass")
|
util.commonParams.Password = vi.GetString("pass")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var createGrantCmd = &cobra.Command{
|
var createGrantCmd = &cobra.Command{
|
||||||
Use: "create name|id password",
|
Use: "create name|id password",
|
||||||
Short: "Create grant",
|
Short: "Create grant",
|
||||||
@@ -73,3 +75,15 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
util.subCmd = subCmd
|
util.subCmd = subCmd
|
||||||
return util.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
|
||||||
|
}
|
||||||
@@ -4,18 +4,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mbase/pkg/logger"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
log := logger.NewLogger("main")
|
||||||
util := NewUtil()
|
util := NewUtil()
|
||||||
err = util.Build()
|
err = util.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("Build error: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
err = util.Exec(os.Args[1:])
|
err = util.Exec(os.Args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("Exec error: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func printResponse(res any, err error) {
|
||||||
|
type Response struct {
|
||||||
|
Error bool `json:"error" yaml:"error"`
|
||||||
|
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
||||||
|
Result any `json:"result,omitempty" yaml:"result,omitempty"`
|
||||||
|
}
|
||||||
|
resp := Response{}
|
||||||
|
if err != nil {
|
||||||
|
resp.Error = true
|
||||||
|
resp.Message = err.Error()
|
||||||
|
} else {
|
||||||
|
resp.Result = res
|
||||||
|
}
|
||||||
|
respBytes, _ := yaml.Marshal(resp)
|
||||||
|
fmt.Printf("---\n%s\n", string(respBytes))
|
||||||
|
}
|
||||||
+119
-6
@@ -4,6 +4,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -12,18 +14,27 @@ import (
|
|||||||
"mbase/app/operator"
|
"mbase/app/operator"
|
||||||
"mbase/cmd/mbaseadmin/account"
|
"mbase/cmd/mbaseadmin/account"
|
||||||
"mbase/cmd/mbaseadmin/grant"
|
"mbase/cmd/mbaseadmin/grant"
|
||||||
|
"mbase/pkg/auxtool"
|
||||||
|
"mbase/pkg/descr"
|
||||||
|
"mbase/pkg/logger"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Util struct {
|
type Util struct {
|
||||||
lg *operator.Logic
|
log *logger.Logger
|
||||||
|
oper *operator.Logic
|
||||||
db *maindb.Database
|
db *maindb.Database
|
||||||
|
state descr.Server
|
||||||
|
sfile string
|
||||||
rootCmd *cobra.Command
|
rootCmd *cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUtil() *Util {
|
func NewUtil() *Util {
|
||||||
return &Util{}
|
return &Util{
|
||||||
|
log: logger.NewLogger("util"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (util *Util) GetRooCmd() *cobra.Command {
|
func (util *Util) GetRooCmd() *cobra.Command {
|
||||||
@@ -34,6 +45,9 @@ func (util *Util) Build() error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
conf := config.NewConfig()
|
conf := config.NewConfig()
|
||||||
|
|
||||||
|
util.sfile = filepath.Join(conf.DataDir, "mbase.yaml")
|
||||||
|
|
||||||
err = conf.ReadFile()
|
err = conf.ReadFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -42,11 +56,29 @@ func (util *Util) Build() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = conf.Normalize()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = conf.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//util.log.Infof("Create %s dir", conf.DataDir)
|
||||||
|
err = os.MkdirAll(conf.DataDir, 0750)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Load state
|
||||||
|
err = util.LoadState()
|
||||||
|
if err != nil {
|
||||||
|
//return err
|
||||||
|
}
|
||||||
db, err := maindb.NewDatabase(conf.DataDir)
|
db, err := maindb.NewDatabase(conf.DataDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.OpenDatabase()
|
err = db.OpenDatabase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -55,11 +87,24 @@ func (util *Util) Build() error {
|
|||||||
logicConfig := &operator.LogicConfig{
|
logicConfig := &operator.LogicConfig{
|
||||||
Database: util.db,
|
Database: util.db,
|
||||||
}
|
}
|
||||||
util.lg, err = operator.NewLogic(logicConfig)
|
util.oper, err = operator.NewLogic(logicConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !util.state.DatabaseInitialized {
|
||||||
|
// Create schema
|
||||||
|
util.log.Infof("Init database")
|
||||||
|
err = util.db.InitDatabase()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
util.state.DatabaseInitialized = true
|
||||||
|
err = util.SaveState()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
execName := filepath.Base(os.Args[0])
|
execName := filepath.Base(os.Args[0])
|
||||||
rootCmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: execName,
|
Use: execName,
|
||||||
@@ -68,12 +113,19 @@ func (util *Util) Build() error {
|
|||||||
}
|
}
|
||||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
|
|
||||||
accountUtil := account.NewUtil(util.lg)
|
accountUtil := account.NewUtil(util.oper)
|
||||||
rootCmd.AddCommand(accountUtil.BuildCmds())
|
rootCmd.AddCommand(accountUtil.BuildCmds())
|
||||||
|
|
||||||
grantUtil := grant.NewUtil(util.lg)
|
grantUtil := grant.NewUtil(util.oper)
|
||||||
rootCmd.AddCommand(grantUtil.BuildCmds())
|
rootCmd.AddCommand(grantUtil.BuildCmds())
|
||||||
|
|
||||||
|
var deleteGrantCmd = &cobra.Command{
|
||||||
|
Use: "seed",
|
||||||
|
Short: "Create initial account",
|
||||||
|
Run: util.SeedAccount,
|
||||||
|
}
|
||||||
|
rootCmd.AddCommand(deleteGrantCmd)
|
||||||
|
|
||||||
util.rootCmd = rootCmd
|
util.rootCmd = rootCmd
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -84,3 +136,64 @@ func (util *Util) Exec(args []string) error {
|
|||||||
err = util.rootCmd.Execute()
|
err = util.rootCmd.Execute()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (util *Util) LoadState() error {
|
||||||
|
var err error
|
||||||
|
_, err = os.Stat(util.sfile)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
file, err := os.Open(util.sfile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
stateBytes, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = yaml.Unmarshal(stateBytes, &util.state)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) SaveState() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
file, err := os.OpenFile(util.sfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
util.state.UpdatedAt = auxtool.TimeNow()
|
||||||
|
if util.state.CreatedAt == "" {
|
||||||
|
util.state.CreatedAt = util.state.UpdatedAt
|
||||||
|
}
|
||||||
|
stateBytes, err := yaml.Marshal(util.state)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = file.Write(stateBytes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) SeedAccount(cmd *cobra.Command, args []string) {
|
||||||
|
err := util.seedAccount()
|
||||||
|
printResponse(nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) seedAccount() error {
|
||||||
|
// Seed accounts
|
||||||
|
ctx := context.Background()
|
||||||
|
_, err := util.oper.SeedAccount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultHostname = "localhost"
|
defaultHostname = "localhost"
|
||||||
rcFilename = ".certmanager.yaml"
|
rcFilename = ".mbase.yaml"
|
||||||
defaultPort uint32 = client.DefaultPort
|
defaultPort uint32 = client.DefaultPort
|
||||||
|
|
||||||
getHelloCmd = "getHello"
|
getHelloCmd = "getHello"
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ message getDumpResult {
|
|||||||
string dump = 1;
|
string dump = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
message restoreDumpParams {
|
message restoreDumpParams {
|
||||||
string dump = 1;
|
string dump = 1;
|
||||||
bool deleteAllRecords = 2;
|
bool deleteAllRecords = 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user