wi/main.go

82 lines
2 KiB
Go
Raw Normal View History

2016-07-21 19:40:19 +00:00
/**
* @file main.go
* @author Mikhail Klementyev jollheef<AT>riseup.net
* @license GNU GPLv3
* @date July, 2016
* @brief Tiny non-interactive cli browser
*/
2016-07-19 22:08:19 +00:00
package main
import (
2016-07-30 08:05:50 +00:00
"strings"
2016-07-25 15:35:09 +00:00
"github.com/jollheef/wi/commands"
2016-07-22 12:44:12 +00:00
"github.com/jollheef/wi/storage"
2016-07-22 12:43:23 +00:00
2016-07-19 22:08:19 +00:00
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
2016-07-30 08:05:50 +00:00
type searchList []string
func (l *searchList) Set(value string) (err error) {
*l = append(*l, value)
return
}
func (l *searchList) String() (s string) {
return ""
}
func (l *searchList) IsCumulative() bool {
return true
}
func SearchList(settings kingpin.Settings) (target *[]string) {
target = new([]string)
settings.SetValue((*searchList)(target))
return
}
2016-07-19 22:08:19 +00:00
var (
2016-07-24 08:01:55 +00:00
get = kingpin.Command("get", "Get url")
getUrl = get.Arg("url", "Url").Required().String()
2016-08-01 00:11:48 +00:00
post = kingpin.Command("post", "Fill post form")
postID = post.Arg("id", "Form ID").Required().Int64()
postArgs = SearchList(post.Arg("args", "Post form arguments"))
2016-07-25 15:41:12 +00:00
link = kingpin.Command("link", "Get link")
linkNo = link.Arg("no", "Number").Required().Int64()
linkFromHistory = link.Flag("history", "Item from history").Bool()
2016-07-24 08:01:55 +00:00
historyList = kingpin.Command("history", "List history")
historyListItems = historyList.Arg("items", "Amount of items").Int64()
2016-07-24 08:23:24 +00:00
historyListAll = historyList.Flag("all", "Show all items").Bool()
2016-07-30 08:05:50 +00:00
search = kingpin.Command("search", "Search engine (google by default)")
searchArgs = SearchList(search.Arg("string", "String for search"))
2016-07-19 22:08:19 +00:00
)
2016-07-22 12:43:23 +00:00
func main() {
db, err := storage.OpenDB("/tmp/wi.db")
if err != nil {
panic(err)
}
2016-07-24 08:01:55 +00:00
defer db.Close()
switch kingpin.Parse() {
case "get":
2016-07-25 15:35:09 +00:00
commands.Get(db, *getUrl)
2016-08-01 00:11:48 +00:00
case "post":
commands.Post(db, *postID, *postArgs)
2016-07-24 08:01:55 +00:00
case "link":
2016-07-25 15:41:12 +00:00
commands.Link(db, *linkNo, *linkFromHistory)
2016-07-24 08:01:55 +00:00
case "history":
2016-07-25 15:35:09 +00:00
commands.History(db, *historyListItems, 20, *historyListAll)
2016-07-30 08:05:50 +00:00
case "search":
// FIXME: currenlty supports only Google
commands.Get(db, "google.com/search?q="+strings.Join(*searchArgs, "+"))
2016-07-22 12:43:23 +00:00
}
}