From d5bc61e39d48211f508f9a2db780a2be9e4887f7 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Fri, 29 Nov 2024 14:37:57 +0100 Subject: [PATCH 1/2] OAK Compact command --- cmd/aem/oak.go | 32 ++++++++++++++++++++++++++++++++ pkg/oak.go | 8 ++++++++ pkg/oak_run.go | 24 +++++++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cmd/aem/oak.go b/cmd/aem/oak.go index 04d0bd53..38ff136e 100644 --- a/cmd/aem/oak.go +++ b/cmd/aem/oak.go @@ -14,6 +14,7 @@ func (c *CLI) oakCmd() *cobra.Command { Short: "Manages OAK repository", } cmd.AddCommand(c.oakIndexCmd()) + cmd.AddCommand(c.oakCompactCmd()) return cmd } @@ -195,3 +196,34 @@ func oakIndexByFlags(cmd *cobra.Command, i pkg.Instance) (*pkg.OAKIndex, error) } return nil, fmt.Errorf("flag 'name' is required") } + +func (c *CLI) oakCompactCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "compact", + Short: "Compact OAK store", + Run: func(cmd *cobra.Command, args []string) { + instances, err := c.aem.InstanceManager().Some() + if err != nil { + c.Error(err) + return + } + compacted, err := pkg.InstanceProcess(c.aem, instances, func(instance pkg.Instance) (map[string]any, error) { + if err := instance.OAK().Compact(); err != nil { + return nil, err + } + return map[string]any{ + OutputChanged: true, + "instance": instance, + }, nil + }) + if err != nil { + c.Error(err) + return + } + c.SetOutput("compacted", compacted) + c.Changed("store compacted") + }, + } + oakIndexDefineFlags(cmd) + return cmd +} diff --git a/pkg/oak.go b/pkg/oak.go index 77aec21a..d95ef040 100644 --- a/pkg/oak.go +++ b/pkg/oak.go @@ -18,3 +18,11 @@ func NewOAK(instance *Instance) *OAK { func (o *OAK) IndexManager() *OAKIndexManager { return o.indexManager } + +func (o *OAK) oakRun() *OakRun { + return o.instance.manager.aem.vendorManager.oakRun +} + +func (o *OAK) Compact() error { + return o.instance.manager.aem.vendorManager.oakRun.Compact(o.instance.local.Dir()) +} diff --git a/pkg/oak_run.go b/pkg/oak_run.go index 1ca19343..5b5cb7e7 100644 --- a/pkg/oak_run.go +++ b/pkg/oak_run.go @@ -97,8 +97,12 @@ func (or OakRun) SetPassword(instanceDir string, user string, password string) e return nil } +func (or OakRun) StoreDir(instanceDir string) string { + return fmt.Sprintf("%s/%s", instanceDir, or.StorePath) +} + func (or OakRun) RunScript(instanceDir string, scriptFile string) error { - storeDir := fmt.Sprintf("%s/%s", instanceDir, or.StorePath) + storeDir := or.StoreDir(instanceDir) cmd, err := or.vendorManager.javaManager.Command( "-Djava.io.tmpdir="+pathx.Canonical(or.vendorManager.aem.baseOpts.TmpDir), "-jar", or.JarFile(), @@ -114,3 +118,21 @@ func (or OakRun) RunScript(instanceDir string, scriptFile string) error { } return nil } + +func (or OakRun) Compact(instanceDir string) error { + storeDir := or.StoreDir(instanceDir) + cmd, err := or.vendorManager.javaManager.Command( + "-Djava.io.tmpdir="+pathx.Canonical(or.vendorManager.aem.baseOpts.TmpDir), + "-jar", or.JarFile(), + "compact", storeDir, + ) + if err != nil { + return err + } + bytes, err := cmd.CombinedOutput() + if err != nil { + log.Error(string(bytes)) + return fmt.Errorf("cannot run Oak Run compact command for instance dir '%s': %w", instanceDir, err) + } + return nil +} From fe2c8d1edc7df1bcbfd5546a25c10e095716b90e Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Fri, 29 Nov 2024 14:43:55 +0100 Subject: [PATCH 2/2] Compact works --- cmd/aem/oak.go | 1 - pkg/oak_run.go | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/aem/oak.go b/cmd/aem/oak.go index 38ff136e..8ae98201 100644 --- a/cmd/aem/oak.go +++ b/cmd/aem/oak.go @@ -224,6 +224,5 @@ func (c *CLI) oakCompactCmd() *cobra.Command { c.Changed("store compacted") }, } - oakIndexDefineFlags(cmd) return cmd } diff --git a/pkg/oak_run.go b/pkg/oak_run.go index 5b5cb7e7..01f6bbc7 100644 --- a/pkg/oak_run.go +++ b/pkg/oak_run.go @@ -129,9 +129,8 @@ func (or OakRun) Compact(instanceDir string) error { if err != nil { return err } - bytes, err := cmd.CombinedOutput() - if err != nil { - log.Error(string(bytes)) + or.vendorManager.aem.CommandOutput(cmd) + if err := cmd.Run(); err != nil { return fmt.Errorf("cannot run Oak Run compact command for instance dir '%s': %w", instanceDir, err) } return nil