-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
59 lines (51 loc) · 1.08 KB
/
search.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
// Objective - Google-Search-Hotkey - Read more at github.com/krAshwin/Google-Search-Hotkey
// Author - Kumar Ashwin
// Mail - [email protected]
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/atotto/clipboard"
)
func openbrowser(url string) {
var err error
switch os := runtime.GOOS; os {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
fmt.Println("Unsupported Plaform!")
os.Exit(1)
}
}
// URL identification - basic comparision
func search(url string) {
if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
openbrowser(url)
} else {
url = "https://www.google.com/search?q=" + url
openbrowser(url)
}
}
func main() {
if len(os.Args) == 1 {
url, err := clipboard.ReadAll()
if err != nil {
log.Fatal(err)
}
search(url)
} else {
param := os.Args[1]
search(param)
}
}