-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
150 lines (143 loc) · 3.81 KB
/
cli.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/sypht-team/sypht-golang-client"
"github.com/urfave/cli/v2"
)
type flags struct {
recursive bool
workflowID string
uploadRate int
nThreads int
}
var client *sypht.Client
var cliFlags *flags
func initFunc() {
cliFlags = &flags{}
currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
config := getConfig(filepath.Join(currentDir, "config.json"))
client, err = sypht.NewSyphtClient(fmt.Sprintf("%s:%s", config.ClientID, config.ClientSecret), nil)
if err != nil {
log.Fatalf("Unable to start Sypht client , %v", err)
}
}
func main() {
initFunc()
app := &cli.App{
Name: "Sypht-cli",
Usage: "Upload docs to Sypht's API",
Version: "v0.1.0",
Commands: []*cli.Command{
{
Name: "scan",
Usage: "sypht-cli scan [OPTIONS] [directory]",
UsageText: "sypht-cli scan [OPTIONS] [directory]",
Description: "Scan and upload all documents in a directory to Sypht's API.",
ArgsUsage: "Full path of the directory to scan",
Action: func(ctx *cli.Context) error {
var dir string
if ctx.NArg() > 0 {
dir = ctx.Args().Get(0)
return scan(dir, ctx)
}
currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalf("Unknown error : %v", err)
return nil
}
return scan(currentDir, ctx)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "workflow",
Aliases: []string{"w"},
Value: "process",
Usage: "Sypht workflow",
Destination: &cliFlags.workflowID,
Hidden: true,
},
&cli.IntFlag{
Name: "rate-limit",
Value: 1,
Usage: "Number of files to upload per second",
Destination: &cliFlags.uploadRate,
},
&cli.BoolFlag{
Name: "recursive",
Aliases: []string{"R"},
Value: false,
Usage: "Recursively scan files in subdirectories",
Destination: &cliFlags.recursive,
},
&cli.IntFlag{
Name: "nThreads",
Value: 1,
Usage: "Number of go routines running at same time",
Destination: &cliFlags.nThreads,
Hidden: true,
},
},
},
{
Name: "watch",
Usage: "sypht-cli watch [OPTIONS] [directory]",
UsageText: "sypht-cli watch [OPTIONS] [directory]",
Description: "Watch and upload all newly added documents in a directory to Sypht's API.",
ArgsUsage: "Full path of the directory to watch",
Action: func(ctx *cli.Context) error {
var dir string
if ctx.NArg() > 0 {
dir = ctx.Args().Get(0)
return watch(dir, ctx)
}
currentDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalf("Unknown error : %v", err)
return nil
}
return watch(currentDir, ctx)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "workflow",
Aliases: []string{"w"},
Value: "process",
Usage: "Sypht workflow",
Destination: &cliFlags.workflowID,
Hidden: true,
},
&cli.IntFlag{
Name: "rate-limit",
Value: 1,
Usage: "Number of files to upload per second",
Destination: &cliFlags.uploadRate,
},
&cli.BoolFlag{
Name: "recursive",
Aliases: []string{"R"},
Value: false,
Usage: "Recursively watch files in subdirectories",
Destination: &cliFlags.recursive,
},
&cli.IntFlag{
Name: "nThreads",
Value: 1,
Usage: "Number of go routines running at same time",
Destination: &cliFlags.nThreads,
Hidden: true,
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}