|
| 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 | +} |
0 commit comments