/ go / app / cmd / query / query.go
query.go
 1  package query
 2  
 3  import (
 4  	"math"
 5  
 6  	"github.com/spf13/cobra"
 7  	"github.com/thunderbrewhq/binana/go/app"
 8  	"github.com/thunderbrewhq/binana/go/app/cmd/root"
 9  	"github.com/thunderbrewhq/binana/go/app/util"
10  )
11  
12  var query_cmd = cobra.Command{
13  	Use:   "q regexp",
14  	Args:  cobra.MinimumNArgs(1),
15  	Short: "query the token database for information",
16  	Run:   run_query_cmd,
17  }
18  
19  func init() {
20  	f := query_cmd.Flags()
21  	f.Bool("regex", false, "use regex to for matching token names")
22  	f.Uint32("min-build", 0, "the minimum build to return tokens for")
23  	f.Uint32("max-build", math.MaxUint32, "the maximum build to return tokens for")
24  	f.StringSlice("program", nil, "a list of programs to return tokens for")
25  	f.StringSlice("os", nil, "a list of kernel names to return tokens for (windows, darwin, linux)")
26  	f.StringSlice("arch", nil, "a list of CPU architectures to return tokens for (ppc, 386, amd64)")
27  	f.String("present", "normal", "control the way tokens are presented to console (name, sample-name)")
28  	f.Bool("quote", false, "quote strings before presenting")
29  	root.RootCmd.AddCommand(&query_cmd)
30  }
31  
32  func run_query_cmd(cmd *cobra.Command, args []string) {
33  	f := cmd.Flags()
34  	var (
35  		params            util.QueryParams
36  		err               error
37  		presentation_mode string
38  	)
39  	params.RegEx, err = f.GetBool("regex")
40  	if err != nil {
41  		app.Fatal(err)
42  	}
43  	params.MinBuild, err = f.GetUint32("min-build")
44  	if err != nil {
45  		app.Fatal(err)
46  	}
47  	params.MaxBuild, err = f.GetUint32("max-build")
48  	if err != nil {
49  		app.Fatal(err)
50  	}
51  	params.Program, err = f.GetStringSlice("program")
52  	if err != nil {
53  		app.Fatal(err)
54  	}
55  	params.OS, err = f.GetStringSlice("os")
56  	if err != nil {
57  		return
58  	}
59  	params.Arch, err = f.GetStringSlice("arch")
60  	if err != nil {
61  		return
62  	}
63  	presentation_mode, err = f.GetString("present")
64  	if err != nil {
65  		return
66  	}
67  	params.Quote, err = f.GetBool("quote")
68  	if err != nil {
69  		return
70  	}
71  	switch presentation_mode {
72  	case "normal":
73  		params.Present = util.PresentQueryNormal
74  	case "name-only":
75  		params.Present = util.PresentQueryNameOnly
76  	case "sample-name":
77  		params.Present = util.PresentQuerySampleName
78  	default:
79  		cmd.Help()
80  		return
81  	}
82  	params.Token = args[0]
83  	util.Query(&params)
84  }