Files
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

129 lines
3.5 KiB
Go

package commands
import (
"context"
"fmt"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/cortezaproject/corteza/server/compose/service"
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/cli"
"github.com/spf13/cobra"
)
func Records(ctx context.Context, app serviceInitializer) (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "records",
Aliases: []string{"rec", "record"},
}
cmd.AddCommand(RecordsSynthetic(ctx, app))
return
}
func RecordsSynthetic(ctx context.Context, app serviceInitializer) *cobra.Command {
var (
total uint
faker = gofakeit.NewCrypto()
namespace string
module string
synth = &cobra.Command{
Use: "synthetic",
Aliases: []string{"synth"},
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if err = app.InitServices(ctx); err != nil {
return
}
if err = service.DefaultModule.ReloadDALModels(ctx); err != nil {
return
}
return
},
}
gen = &cobra.Command{
Use: "generate",
Aliases: []string{"gen"},
Short: "Create synthetic records",
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if len(namespace) == 0 || len(module) == 0 {
cli.HandleError(fmt.Errorf("specifiy ID and handle for both, module and namespace"))
}
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
ns, mod, err := resolveModule(ctx, service.DefaultNamespace, service.DefaultModule, namespace, module)
cli.HandleError(err)
_ = faker
_ = ns
_ = mod
_ = err
cmd.Printf("Generating %d compose records (module: %s) ...", total, mod.Name)
bm := time.Now()
cli.HandleError(service.DefaultRecord.CreateSynthetic(ctx, faker, mod, total))
cmd.Printf("done in %s", time.Since(bm).Round(time.Millisecond))
cmd.Println()
},
}
rem = &cobra.Command{
Use: "remove",
Aliases: []string{"rm", "d", "delete", "del"},
Short: "Remove synthetic records",
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if len(namespace) == 0 || len(module) == 0 {
cli.HandleError(fmt.Errorf("specifiy ID and handle for both, module and namespace"))
}
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
ns, mod, err := resolveModule(ctx, service.DefaultNamespace, service.DefaultModule, namespace, module)
cli.HandleError(err)
_ = ns
_ = mod
_ = err
cmd.Printf("Removing all synthetic compose records (module: %s) ...", mod.Name)
bm := time.Now()
cli.HandleError(service.DefaultRecord.RemoveSynthetic(ctx, mod))
cmd.Printf("done in %s", time.Since(bm).Round(time.Millisecond))
cmd.Println()
},
}
)
for _, cmd := range []*cobra.Command{gen, rem} {
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace IS or handle for recode generation")
cmd.Flags().StringVarP(&module, "module", "m", "", "module IS or handle for recode generation")
}
gen.Flags().UintVarP(&total, "total", "t", 1, "Number of synthetic records generated")
synth.AddCommand(gen, rem)
return synth
}
func resolveModule(ctx context.Context, nsSvc service.NamespaceService, modSvc service.ModuleService, nsIdent, modIdent string) (ns *types.Namespace, mod *types.Module, err error) {
if ns, err = nsSvc.FindByAny(ctx, nsIdent); err != nil {
return
}
mod, err = modSvc.FindByAny(ctx, ns.ID, modIdent)
return
}