From 838fa8302ad5882e1486d4977bf187d69ede04fa Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 17 Sep 2019 21:18:45 +0200 Subject: [PATCH] Add import/export commands --- compose/commands/exporter.go | 119 +++++++++++++++++++++++++++++++++++ compose/commands/importer.go | 69 ++++++++++++++++++++ system/commands/importer.go | 61 ++++++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 compose/commands/exporter.go create mode 100644 compose/commands/importer.go create mode 100644 system/commands/importer.go diff --git a/compose/commands/exporter.go b/compose/commands/exporter.go new file mode 100644 index 000000000..25fb1c433 --- /dev/null +++ b/compose/commands/exporter.go @@ -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 +} diff --git a/compose/commands/importer.go b/compose/commands/importer.go new file mode 100644 index 000000000..e219d79b2 --- /dev/null +++ b/compose/commands/importer.go @@ -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 +} diff --git a/system/commands/importer.go b/system/commands/importer.go new file mode 100644 index 000000000..043a55a03 --- /dev/null +++ b/system/commands/importer.go @@ -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 +}