Add bbolt compact command to ziti CLI. Fixes #1041

This commit is contained in:
Paul Lorenz
2023-03-17 14:44:51 -04:00
parent 5966d1397d
commit 1f08a4aea7
3 changed files with 67 additions and 2 deletions
+3 -1
View File
@@ -8,7 +8,7 @@ import (
"io"
)
func NewCmdDb(_ io.Writer, errOut io.Writer) *cobra.Command {
func NewCmdDb(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := util.NewEmptyParentCmd("db", "Interact with Ziti database files")
exploreCmd := &cobra.Command{
@@ -22,5 +22,7 @@ func NewCmdDb(_ io.Writer, errOut io.Writer) *cobra.Command {
}
cmd.AddCommand(exploreCmd)
cmd.AddCommand(NewCompactAction())
return cmd
}
+63
View File
@@ -0,0 +1,63 @@
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package database
import (
"github.com/spf13/cobra"
"go.etcd.io/bbolt"
"math"
)
type CompactAction struct {
useArrayFreelists bool
}
func NewCompactAction() *cobra.Command {
action := &CompactAction{}
cmd := &cobra.Command{
Use: "compact <src> <dest>",
Short: "Compact a bbolt database",
Args: cobra.ExactArgs(2),
RunE: action.Run,
}
cmd.Flags().BoolVar(&action.useArrayFreelists, "array-freelist", false, "Use array freelist")
return cmd
}
// Run implements this command
func (o *CompactAction) Run(cmd *cobra.Command, args []string) error {
srcOptions := *bbolt.DefaultOptions
srcOptions.ReadOnly = true
srcDb, err := bbolt.Open(args[0], 0400, &srcOptions)
if err != nil {
return err
}
dstOptions := *bbolt.DefaultOptions
if !o.useArrayFreelists {
dstOptions.FreelistType = bbolt.FreelistMapType
}
dstDb, err := bbolt.Open(args[1], 0600, &dstOptions)
if err != nil {
return err
}
return bbolt.Compact(dstDb, srcDb, math.MaxUint16)
}
+1 -1
View File
@@ -24,7 +24,7 @@ import (
"os"
)
// AgentOptions contains the command line options
// LogFormatOptions contains the command line options
type LogFormatOptions struct {
CommonOptions