go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv3/command/compaction_command.go (about) 1 // Copyright 2015 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package command 16 17 import ( 18 "fmt" 19 "strconv" 20 21 "github.com/coreos/etcd/clientv3" 22 "github.com/spf13/cobra" 23 ) 24 25 var compactPhysical bool 26 27 // NewCompactionCommand returns the cobra command for "compaction". 28 func NewCompactionCommand() *cobra.Command { 29 cmd := &cobra.Command{ 30 Use: "compaction [options] <revision>", 31 Short: "Compacts the event history in etcd", 32 Run: compactionCommandFunc, 33 } 34 cmd.Flags().BoolVar(&compactPhysical, "physical", false, "'true' to wait for compaction to physically remove all old revisions") 35 return cmd 36 } 37 38 // compactionCommandFunc executes the "compaction" command. 39 func compactionCommandFunc(cmd *cobra.Command, args []string) { 40 if len(args) != 1 { 41 ExitWithError(ExitBadArgs, fmt.Errorf("compaction command needs 1 argument.")) 42 } 43 44 rev, err := strconv.ParseInt(args[0], 10, 64) 45 if err != nil { 46 ExitWithError(ExitError, err) 47 } 48 49 var opts []clientv3.CompactOption 50 if compactPhysical { 51 opts = append(opts, clientv3.WithCompactPhysical()) 52 } 53 54 c := mustClientFromCmd(cmd) 55 ctx, cancel := commandCtx(cmd) 56 _, cerr := c.Compact(ctx, rev, opts...) 57 cancel() 58 if cerr != nil { 59 ExitWithError(ExitError, cerr) 60 } 61 fmt.Println("compacted revision", rev) 62 }