1//  Copyright (c) 2017 Couchbase, Inc.
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
15package scorch
16
17import (
18	"fmt"
19	"strconv"
20
21	"github.com/spf13/cobra"
22)
23
24var ascii bool
25
26// internalCmd represents the snapshots command
27var internalCmd = &cobra.Command{
28	Use:   "internal",
29	Short: "internal prints the internal k/v pairs in a snapshot",
30	Long:  `The internal command prints the internal k/v pairs in a snapshot.`,
31	RunE: func(cmd *cobra.Command, args []string) error {
32
33		if len(args) < 2 {
34			return fmt.Errorf("snapshot epoch required")
35		} else if len(args) < 3 {
36			snapshotEpoch, err := strconv.ParseUint(args[1], 10, 64)
37			if err != nil {
38				return err
39			}
40			snapshot, err := index.LoadSnapshot(snapshotEpoch)
41			if err != nil {
42				return err
43			}
44			internal := snapshot.Internal()
45			for k, v := range internal {
46				if ascii {
47					fmt.Printf("%s %s\n", k, string(v))
48				} else {
49					fmt.Printf("%x %x\n", k, v)
50				}
51			}
52		}
53
54		return nil
55	},
56}
57
58func init() {
59	RootCmd.AddCommand(internalCmd)
60	internalCmd.Flags().BoolVarP(&ascii, "ascii", "a", false, "print key/value in ascii")
61}
62