Files
gmbase/cmd/mbaseadmin/util.go
T
2026-06-07 15:06:37 +02:00

200 lines
3.6 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"mbase/app/config"
"mbase/app/maindb"
"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 {
log *logger.Logger
oper *operator.Logic
db *maindb.Database
state descr.Server
sfile string
rootCmd *cobra.Command
}
func NewUtil() *Util {
return &Util{
log: logger.NewLogger("util"),
}
}
func (util *Util) GetRooCmd() *cobra.Command {
return util.rootCmd
}
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
}
err = conf.ReadEnv()
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
}
util.db = db
logicConfig := &operator.LogicConfig{
Database: util.db,
}
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,
Short: "\nAccount operations",
SilenceUsage: true,
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
accountUtil := account.NewUtil(util.oper)
rootCmd.AddCommand(accountUtil.BuildCmds())
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
}
func (util *Util) Exec(args []string) error {
var err error
util.rootCmd.SetArgs(args)
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
}