Skip to content

Commit b7a0e65

Browse files
committed
Implement info command
1 parent ac84eae commit b7a0e65

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

src/cmd/backup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
var backupCmd = &cobra.Command{
1212
Use: "backup",
1313
Short: "Create DB backup",
14-
Long: `Creates a backup for the database.
14+
Long: `Creates a backup for the database. See info command for backup location.
1515
16-
Created backup can be later restored using 'kv db restore'.
16+
Created backup can be later restored using 'kv db restore'.
1717
18-
Only a single backup is kept, so creating a backup removes existing backups.
18+
Only a single backup is kept, so creating a backup removes existing backups.
1919
`,
2020
Example: ` # Backup DB
2121
kv db backup

src/cmd/implode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// implodeCmd represents the implode command
99
var implodeCmd = &cobra.Command{
1010
Use: "implode",
11-
Short: "Permanently delete all keys and history",
11+
Short: "Permanently delete all keys, history, and backups",
1212
Long: `Permanently delete all keys and their history from the store.
1313
1414
This command is not suitable to run concurrently with other commands,

src/cmd/import.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ var importCmd = &cobra.Command{
2323
The imported file must be a valid database file created with the 'export' command.
2424
This will completely replace the current database with the imported one.
2525
26-
WARNING: This operation is destructive. The current database will be backed up
27-
to <db-path>.backup before importing. You can restore from this backup using
28-
the 'kv db restore' command if needed.
26+
WARNING: This operation is destructive.
27+
28+
If --backup flag is set the current database will be backed up
29+
to <db-path>.backup before importing.
30+
You can restore from this backup using the 'kv db restore' command if needed.
31+
32+
If --backup flag is not passed, a temp backup will be created in case import fails.
33+
It will be automatically deleted if import fails.
34+
35+
For (persistent) backup location, see 'info' command.
2936
3037
Use "-" as the file path to read from stdin (useful for piping).`,
3138
Example: ` # Import database from a file

src/cmd/info.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"path/filepath"
6+
7+
"github.com/AmrSaber/kv/src/common"
8+
"github.com/spf13/cobra"
9+
"gopkg.in/yaml.v2"
10+
)
11+
12+
var infoFlags = struct{ output string }{}
13+
14+
var infoCmd = &cobra.Command{
15+
Use: "info",
16+
Short: "Displays kv info",
17+
Long: `Displays kv info and paths.
18+
Note that "backup path" does not mean there is a backup. There might be, there might not.
19+
It just displays the path where a backup would be if there were one.`,
20+
21+
Args: cobra.NoArgs,
22+
Run: func(cmd *cobra.Command, args []string) {
23+
type Info struct {
24+
DataDir string `json:"dataDir" yaml:"data-dir"`
25+
BackupPath string `json:"backupPath" yaml:"backup-path"`
26+
27+
Config common.Config `json:"config" yaml:"config"`
28+
}
29+
30+
info := Info{
31+
DataDir: filepath.Dir(common.GetDBPath()),
32+
BackupPath: common.GetDefaultBackupPath(),
33+
Config: common.ReadConfig(),
34+
}
35+
36+
switch infoFlags.output {
37+
case "yaml":
38+
output, _ := yaml.Marshal(info)
39+
common.Stdout.Println(string(output))
40+
case "json":
41+
output, _ := json.MarshalIndent(info, "", " ")
42+
common.Stdout.Println(string(output))
43+
default:
44+
common.Fail("Unsupported format %q", infoFlags.output)
45+
}
46+
},
47+
}
48+
49+
func init() {
50+
rootCmd.AddCommand(infoCmd)
51+
52+
infoCmd.Flags().StringVarP(&infoFlags.output, "output", "o", "yaml", "Print format, options: json, yaml")
53+
_ = infoCmd.RegisterFlagCompletionFunc(
54+
"output",
55+
cobra.FixedCompletions([]string{"json", "yaml"}, cobra.ShellCompDirectiveDefault),
56+
)
57+
}

src/cmd/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func init() {
190190
listCmd.Flags().BoolVarP(&listFlags.noValues, "no-values", "v", false, "Hide values")
191191
listCmd.Flags().BoolVarP(&listFlags.deleted, "deleted", "d", false, "List deleted keys")
192192
listCmd.Flags().StringVarP(&listFlags.output, "output", "o", "table", "Print format, options: json, yaml, table")
193-
listCmd.RegisterFlagCompletionFunc(
193+
_ = listCmd.RegisterFlagCompletionFunc(
194194
"output",
195195
cobra.FixedCompletions([]string{"json", "yaml", "table"}, cobra.ShellCompDirectiveDefault),
196196
)

src/common/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
)
99

1010
type Config struct {
11-
PruneHistoryAfterDays int `yaml:"prune-history-after-days,omitempty"`
12-
HistoryLength int `yaml:"history-length,omitempty"`
11+
PruneHistoryAfterDays int `json:"pruneHistoryAfterDays" yaml:"prune-history-after-days,omitempty"`
12+
HistoryLength int `json:"historyLength" yaml:"history-length,omitempty"`
1313
}
1414

1515
func (c Config) String() string {

0 commit comments

Comments
 (0)