This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
/
query.go
77 lines (68 loc) · 2.01 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cmd
import (
"errors"
"fmt"
"os"
"path"
homedir "github.com/mitchellh/go-homedir"
toml "github.com/pelletier/go-toml"
"github.com/spf13/cobra"
"github.com/taylorskalyo/stno/datastore"
)
var queryCmd = &cobra.Command{
Use: "query [path]",
Short: "Query entries and display the results",
Long: `Query entries and display the results
This command takes a path. This path is relative to the stno directory
(defaults to ~/.stno). By default query lists all entries.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("query accepts at most one path")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// Open notebook at the given path
dir, err := homedir.Expand(stnoDir)
if err != nil {
fmt.Printf("Could not expand path %s: %s.\n", stnoDir, err.Error())
os.Exit(1)
}
if len(args) > 0 {
dir = path.Join(dir, args[0])
}
ds := datastore.FileStore{Dir: dir}
// Combine entries into a single TOML tree and display
uids, err := ds.ListEntries("")
if err != nil {
fmt.Printf("Failed to list notebook entries: %s.\n", err.Error())
os.Exit(1)
}
tree, _ := toml.Load("")
for _, uid := range uids {
rc, err := ds.NewEntryReadCloser(uid)
if err != nil {
fmt.Printf("Failed to read notebook entry %s: %s.\n", uid, err.Error())
os.Exit(1)
}
defer rc.Close()
t, err := toml.LoadReader(rc)
if err != nil {
fmt.Printf("Invalid toml in entry %s: %s.\n", uid, err.Error())
continue
}
tree.Set(uid, "", false, t)
}
fmt.Println(tree.String())
},
}
func init() {
RootCmd.AddCommand(queryCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// queryCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// queryCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}