-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
160 lines (136 loc) · 2.93 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"os"
"github.com/eiannone/keyboard"
"github.com/pkg/browser"
"github.com/urfave/cli"
)
import . "github.com/visionmedia/go-debug"
var debug = Debug("imgops")
var authors = []cli.Author{
{
Name: "Doğan Çelik",
Email: "dogancelik.com",
},
}
var Version string
func cliSelect() string {
err := keyboard.Open()
if err != nil {
panic(err)
}
defer keyboard.Close()
fmt.Println(genSelectText(false))
ret := ""
for {
char, key, err := keyboard.GetKey()
if err != nil {
panic(err)
} else if key == keyboard.KeyEsc {
break
}
m := getKeyToNameTargets(availableTargets)
target, mapOk := m[char]
if mapOk {
return target
} else if char == 'i' {
return defaultTarget
}
}
return ret
}
func cliSearch(c *cli.Context) error {
if c.NArg() == 0 {
return cli.NewExitError("No file or URL is given", 1)
}
srcPath := c.Args().First()
targets := c.String("targets")
debug("Path: %s", srcPath)
debug("Targets: %s", targets)
var urls []string
var errUpload error
// Select flag
if c.Bool("select") {
targets = cliSelect()
if targets == "" {
return cli.NewExitError("Upload cancelled", 4)
}
}
// Input flag
if c.Bool("input") {
fmt.Print(genSelectText(true))
fmt.Scanln(&targets)
targets = initialsToTargets(targets)
if targets == "" {
return cli.NewExitError("No target is specified", 5)
}
}
// Upload
if isUrl(srcPath) == true {
debug("Start URL upload")
urls, errUpload = UploadURL(srcPath, targets)
} else {
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
return cli.NewExitError("File doesn't exist: "+srcPath, 2)
}
debug("Start file upload")
urls, errUpload = UploadFile(srcPath, targets)
}
// Upload result
if errUpload != nil && len(urls) == 0 {
return cli.NewExitError("Error during upload: "+errUpload.Error(), 3)
} else {
if errUpload != nil {
fmt.Fprintf(os.Stderr, "Unknown targets '%s', will open default page instead", targets)
}
for _, url := range urls {
if c.Bool("return") {
fmt.Println(url)
} else {
browser.OpenURL(url)
}
}
}
return nil
}
func cliMain(c *cli.Context) error {
cli.ShowAppHelp(c)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "ImgOps CLI"
app.Usage = "Reverse search images"
app.Version = Version
app.Authors = authors
app.Action = cliMain
app.Commands = []cli.Command{
{
Name: "search",
Aliases: []string{"a"},
Usage: "Search a file or a URL",
Action: cliSearch,
Flags: []cli.Flag{
cli.StringFlag{
Name: "targets, t",
Value: defaultTarget,
Usage: "Target website to search at (e.g. Google)",
},
cli.BoolFlag{
Name: "select, s",
Usage: "Show a list of targets to select from",
},
cli.BoolFlag{
Name: "input, i",
Usage: "Type the targets you want to open",
},
cli.BoolFlag{
Name: "return, r",
Usage: "Output the result URL",
},
},
},
}
app.Run(os.Args)
}