Add import/export commands

This commit is contained in:
Denis Arh
2019-09-30 10:20:39 +02:00
parent 9f91a3ae0e
commit 838fa8302a
3 changed files with 249 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
package commands
import (
"context"
"regexp"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/cortezaproject/corteza-server/compose/service"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/internal/auth"
"github.com/cortezaproject/corteza-server/pkg/cli"
"github.com/cortezaproject/corteza-server/pkg/handle"
)
func Exporter(ctx context.Context, c *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export",
Run: func(cmd *cobra.Command, args []string) {
c.InitServices(ctx, c)
ctx = auth.SetSuperUserContext(ctx)
mm, _, err := service.DefaultModule.Find(types.ModuleFilter{NamespaceID: 88714882739863655})
cli.HandleError(err)
y := yaml.NewEncoder(cmd.OutOrStdout())
out := Compose{
Modules: expModules(mm),
}
cli.HandleError(y.Encode(out))
},
}
return cmd
}
type (
Compose struct {
Modules []Module
}
Module struct {
Name string
Handle string
Meta string
Fields map[string]Field
}
Field struct {
Label string
Kind string
Options string
Private bool
Required bool
Visible bool
Multi bool
// DefaultValue string
}
)
func expModules(mm types.ModuleSet) (o []Module) {
o = make([]Module, len(mm))
for i, m := range mm {
o[i] = Module{
Name: m.Name,
Handle: makeHandle(m.Handle, m.Name),
Meta: m.Meta.String(),
Fields: expModuleFields(m.Fields),
}
if o[i].Handle == "" {
h := regexp.MustCompile(`^[^A-Za-z][^0-9A-Za-z_\-.][^A-Za-z0-9]$`).ReplaceAllString(o[i].Name, "")
if handle.IsValid(h) {
o[i].Handle = h
}
}
}
return
}
func expModuleFields(ff types.ModuleFieldSet) (o map[string]Field) {
o = make(map[string]Field)
for _, f := range ff {
o[f.Name] = Field{
Label: f.Label,
Kind: f.Kind,
Options: f.Options.String(),
Private: f.Private,
Required: f.Required,
Visible: f.Visible,
Multi: f.Multi,
}
}
return
}
func makeHandle(h, n string) string {
if h == "" {
h = regexp.MustCompile(`^[^A-Za-z][^0-9A-Za-z_\-.][^A-Za-z0-9]$`).ReplaceAllString(n, "")
if handle.IsValid(h) {
return h
}
}
return n
}
+69
View File
@@ -0,0 +1,69 @@
package commands
import (
"context"
"io"
"os"
"github.com/spf13/cobra"
"github.com/cortezaproject/corteza-server/compose/importer"
"github.com/cortezaproject/corteza-server/compose/service"
"github.com/cortezaproject/corteza-server/internal/auth"
"github.com/cortezaproject/corteza-server/internal/permissions"
"github.com/cortezaproject/corteza-server/pkg/cli"
)
func Importer(ctx context.Context, c *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "import",
Short: "Import",
Run: func(cmd *cobra.Command, args []string) {
c.InitServices(ctx, c)
var (
ff []io.Reader
err error
)
ctx = auth.SetSuperUserContext(ctx)
if len(args) > 0 {
ff = make([]io.Reader, len(args))
for a, arg := range args {
ff[a], err = os.Open(arg)
cli.HandleError(err)
}
} else {
args = []string{"STDIN"}
ff = []io.Reader{os.Stdin}
}
for i, f := range ff {
cmd.Printf("Importing from %s\n", args[i])
imp := importer.NewImporter(
service.DefaultNamespace.With(ctx),
service.DefaultModule.With(ctx),
service.DefaultChart.With(ctx),
service.DefaultPage.With(ctx),
permissions.NewImporter(service.DefaultAccessControl.Whitelist()),
)
cli.HandleError(imp.YAML(f))
cli.HandleError(imp.Store(
ctx,
service.DefaultNamespace.With(ctx),
service.DefaultModule.With(ctx),
service.DefaultChart.With(ctx),
service.DefaultPage.With(ctx),
service.DefaultAccessControl,
))
}
},
}
return cmd
}
+61
View File
@@ -0,0 +1,61 @@
package commands
import (
"context"
"io"
"os"
"github.com/spf13/cobra"
"github.com/cortezaproject/corteza-server/internal/auth"
"github.com/cortezaproject/corteza-server/internal/permissions"
"github.com/cortezaproject/corteza-server/pkg/cli"
"github.com/cortezaproject/corteza-server/system/importer"
"github.com/cortezaproject/corteza-server/system/service"
)
func Importer(ctx context.Context, c *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "import",
Short: "Import",
Run: func(cmd *cobra.Command, args []string) {
c.InitServices(ctx, c)
var (
ff []io.Reader
err error
)
ctx = auth.SetSuperUserContext(ctx)
if len(args) > 0 {
ff = make([]io.Reader, len(args))
for a, arg := range args {
ff[a], err = os.Open(arg)
cli.HandleError(err)
}
} else {
args = []string{"STDIN"}
ff = []io.Reader{os.Stdin}
}
roles := service.DefaultRole.With(ctx)
for i, f := range ff {
cmd.Printf("Importing from %s\n", args[i])
imp := importer.NewImporter(
roles,
permissions.NewImporter(service.DefaultAccessControl.Whitelist()),
)
cli.HandleError(imp.YAML(f))
cli.HandleError(imp.Store(ctx, roles, service.DefaultAccessControl))
}
},
}
return cmd
}