-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
74 lines (64 loc) · 1.17 KB
/
main.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
package main
import (
"flag"
"net/url"
"os"
"strings"
"github.com/toqueteos/webbrowser"
)
// sample url
// https://www.google.co.jp/search?q=test&ie=utf-8&oe=utf-8&hl=ja
type Ggr struct {
q string
ie string
oe string
hl string
tbm string
}
func NewGgr() Ggr {
g := Ggr{
q: "",
ie: "utf-8",
oe: "utf-8",
hl: "ja",
tbm: "",
}
return g
}
func (p Ggr) getSearchURL() string {
values := url.Values{}
values.Add("q", p.q)
values.Add("ie", p.ie)
values.Add("oe", p.oe)
values.Add("hl", p.hl)
values.Add("tbm", p.tbm)
u := url.URL{}
u.Scheme = "https"
u.Host = "www.google.co.jp"
u.Path = "search"
u.RawQuery = values.Encode()
return u.String()
}
func main() {
var (
imageFlag bool
newsFlag bool
shopFlag bool
)
f := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
f.BoolVar(&imageFlag, "i", false, "image flag")
f.BoolVar(&newsFlag, "n", false, "new flag")
f.BoolVar(&shopFlag, "s", false, "shop flag")
f.Parse(os.Args[1:])
args := f.Args()
g := NewGgr()
g.q = strings.Join(args, " ")
if imageFlag {
g.tbm = "isch"
} else if newsFlag {
g.tbm = "nws"
} else if shopFlag {
g.tbm = "shop"
}
webbrowser.Open(g.getSearchURL())
}