Skip to content

Commit

Permalink
added better formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Sep 23, 2018
1 parent 788223a commit adae3a7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/radium/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ and replace queries with solutions as they come in!

rootCmd.PersistentFlags().BoolP("ugly", "u", false, "Print raw output as yaml or json")
rootCmd.PersistentFlags().Bool("json", false, "Print output as JSON")
rootCmd.PersistentFlags().StringSlice("sources", nil, "Enable sources")
rootCmd.PersistentFlags().StringSliceP("sources", "s", nil, "Enable sources")
rootCmd.PersistentFlags().String("strategy", "1st", "Default strategy to use")

rootCmd.AddCommand(newServeCmd(cfg))
rootCmd.AddCommand(newQueryCmd(cfg))
Expand Down
7 changes: 5 additions & 2 deletions cmd/radium/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ func newQueryCmd(cfg *config) *cobra.Command {
}

var attribs []string
var strategy string
cmd.Flags().StringSliceVarP(&attribs, "attr", "a", []string{}, "Attributes to narrow the search scope")
cmd.Flags().StringVarP(&strategy, "strategy", "s", "1st", "Strategy to use for executing sources")

cmd.Run = func(_ *cobra.Command, args []string) {
query := radium.Query{}
Expand All @@ -41,6 +39,11 @@ func newQueryCmd(cfg *config) *cobra.Command {
}
}

strategy, err := cmd.Flags().GetString("strategy")
if err != nil {
strategy = "1st"
}

ctx := context.Background()
ins := getNewRadiumInstance(*cfg)
rs, err := ins.Search(ctx, query, strategy)
Expand Down
6 changes: 5 additions & 1 deletion cmd/radium/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ by passing '--clipboard' or '-C' option and setting '--addr' blank.
ins := getNewRadiumInstance(*cfg)

if addr != "" {
srv := radium.NewServer(ins)
ds, err := cmd.Flags().GetString("strategy")
if err != nil {
ds = "1st"
}
srv := radium.NewServer(ins, ds)
wg.Add(1)
go func(wg *sync.WaitGroup) {
ins.Infof("starting server on '%s'...", addr)
Expand Down
8 changes: 7 additions & 1 deletion cmd/radium/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/shivylp/radium"
"github.com/spf13/cobra"
Expand All @@ -29,7 +30,12 @@ func tryPrettyPrint(v interface{}) {
if len(results) == 1 {
tryPrettyPrint(results[0])
} else {
rawDump(results, false)
for num, res := range results {
fmt.Printf("%d. %s [source=%s]\n", num+1, strings.TrimSpace(res.Title), res.Source)
fmt.Println(strings.Repeat("-", 20))
fmt.Println(res.Content)
fmt.Println(strings.Repeat("=", 20))
}
}
case error:
fmt.Printf("error: %s\n", v)
Expand Down
10 changes: 6 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

// NewServer initializes the http API server with given
// instance of Radium
func NewServer(ins *Instance) *Server {
func NewServer(ins *Instance, defaultStrategy string) *Server {
srv := &Server{}

srv.ins = ins
srv.defStrategy = defaultStrategy
srv.router = mux.NewRouter()
srv.router.HandleFunc("/search", srv.handleSearch)
srv.router.HandleFunc("/sources", srv.handleSources)
Expand All @@ -23,8 +24,9 @@ func NewServer(ins *Instance) *Server {

// Server represents an instance of HTTP API server
type Server struct {
ins *Instance
router *mux.Router
ins *Instance
router *mux.Router
defStrategy string
}

func (srv Server) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
Expand All @@ -37,7 +39,7 @@ func (srv Server) handleSearch(wr http.ResponseWriter, req *http.Request) {
query := Query{}
strategy := req.FormValue("strategy")
if strategy == "" {
strategy = "1st"
strategy = srv.defStrategy
}

query.Text = req.FormValue("q")
Expand Down

0 comments on commit adae3a7

Please sign in to comment.