working commit

This commit is contained in:
2026-06-07 15:06:37 +02:00
parent 52dd2cef20
commit c656b562d8
17 changed files with 291 additions and 85 deletions
+119 -6
View File
@@ -4,6 +4,8 @@
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
@@ -12,18 +14,27 @@ import (
"mbase/app/operator"
"mbase/cmd/mbaseadmin/account"
"mbase/cmd/mbaseadmin/grant"
"mbase/pkg/auxtool"
"mbase/pkg/descr"
"mbase/pkg/logger"
"github.com/spf13/cobra"
"go.yaml.in/yaml/v4"
)
type Util struct {
lg *operator.Logic
log *logger.Logger
oper *operator.Logic
db *maindb.Database
state descr.Server
sfile string
rootCmd *cobra.Command
}
func NewUtil() *Util {
return &Util{}
return &Util{
log: logger.NewLogger("util"),
}
}
func (util *Util) GetRooCmd() *cobra.Command {
@@ -34,6 +45,9 @@ func (util *Util) Build() error {
var err error
conf := config.NewConfig()
util.sfile = filepath.Join(conf.DataDir, "mbase.yaml")
err = conf.ReadFile()
if err != nil {
return err
@@ -42,11 +56,29 @@ func (util *Util) Build() error {
if err != nil {
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)
if err != nil {
return err
}
err = db.OpenDatabase()
if err != nil {
return err
@@ -55,11 +87,24 @@ func (util *Util) Build() error {
logicConfig := &operator.LogicConfig{
Database: util.db,
}
util.lg, err = operator.NewLogic(logicConfig)
util.oper, err = operator.NewLogic(logicConfig)
if err != nil {
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])
rootCmd := &cobra.Command{
Use: execName,
@@ -68,12 +113,19 @@ func (util *Util) Build() error {
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
accountUtil := account.NewUtil(util.lg)
accountUtil := account.NewUtil(util.oper)
rootCmd.AddCommand(accountUtil.BuildCmds())
grantUtil := grant.NewUtil(util.lg)
grantUtil := grant.NewUtil(util.oper)
rootCmd.AddCommand(grantUtil.BuildCmds())
var deleteGrantCmd = &cobra.Command{
Use: "seed",
Short: "Create initial account",
Run: util.SeedAccount,
}
rootCmd.AddCommand(deleteGrantCmd)
util.rootCmd = rootCmd
return err
}
@@ -84,3 +136,64 @@ func (util *Util) Exec(args []string) error {
err = util.rootCmd.Execute()
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
}