diff --git a/cmd/aem/oak.go b/cmd/aem/oak.go index 04d0bd5..8ae9820 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,33 @@ 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") + }, + } + return cmd +} diff --git a/pkg/oak.go b/pkg/oak.go index 77aec21..d95ef04 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 1ca1934..01f6bbc 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,20 @@ 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 + } + 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 +}