Repository navigation
Expand file tree
/
Copy pathcmd.go
More file actions
227 lines (221 loc) · 5.46 KB
/
Copy pathcmd.go
File metadata and controls
227 lines (221 loc) · 5.46 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package cmd
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"github.com/context-dot-dev/context-dev-cli/internal/autocomplete"
"github.com/context-dot-dev/context-dev-cli/internal/requestflag"
docs "github.com/urfave/cli-docs/v3"
"github.com/urfave/cli/v3"
)
var (
Command *cli.Command
CommandErrorBuffer bytes.Buffer
)
func init() {
Command = &cli.Command{
Name: "context-dev",
Usage: "CLI for the context.dev API",
Suggest: true,
Version: Version,
ErrWriter: &CommandErrorBuffer,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
},
&cli.StringFlag{
Name: "base-url",
DefaultText: "url",
Usage: "Override the base URL for API requests",
Validator: func(baseURL string) error {
return ValidateBaseURL(baseURL, "--base-url")
},
},
&cli.StringFlag{
Name: "format",
Usage: "The format for displaying response data (one of: " + strings.Join(OutputFormats, ", ") + ")",
Value: "auto",
Validator: func(format string) error {
if !slices.Contains(OutputFormats, strings.ToLower(format)) {
return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", "))
}
return nil
},
},
&cli.StringFlag{
Name: "format-error",
Usage: "The format for displaying error data (one of: " + strings.Join(OutputFormats, ", ") + ")",
Value: "auto",
Validator: func(format string) error {
if !slices.Contains(OutputFormats, strings.ToLower(format)) {
return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", "))
}
return nil
},
},
&cli.StringFlag{
Name: "transform",
Usage: "The GJSON transformation for data output.",
},
&cli.StringFlag{
Name: "transform-error",
Usage: "The GJSON transformation for errors.",
},
&cli.BoolFlag{
Name: "raw-output",
Aliases: []string{"r"},
Usage: "If the result is a string, print it without JSON quotes. This can be useful for making output transforms talk to non-JSON-based systems.",
},
&requestflag.Flag[string]{
Name: "api-key",
Sources: cli.EnvVars("CONTEXT_DEV_API_KEY"),
},
},
Commands: []*cli.Command{
{
Name: "web",
Category: "API RESOURCE",
Suggest: true,
Commands: []*cli.Command{
&webExtract,
&webExtractCompetitors,
&webExtractFonts,
&webExtractStyleguide,
&webScreenshot,
&webSearch,
&webWebCrawlMd,
&webWebScrapeHTML,
&webWebScrapeImages,
&webWebScrapeMd,
&webWebScrapeSitemap,
},
},
{
Name: "ai",
Category: "API RESOURCE",
Suggest: true,
Commands: []*cli.Command{
&aiAIQuery,
&aiExtractProduct,
&aiExtractProducts,
},
},
{
Name: "brand",
Category: "API RESOURCE",
Suggest: true,
Commands: []*cli.Command{
&brandRetrieve,
&brandIdentifyFromTransaction,
&brandRetrieveByEmail,
&brandRetrieveByIsin,
&brandRetrieveByName,
&brandRetrieveByTicker,
&brandRetrieveSimplified,
},
},
{
Name: "industry",
Category: "API RESOURCE",
Suggest: true,
Commands: []*cli.Command{
&industryRetrieveNaics,
&industryRetrieveSic,
},
},
{
Name: "utility",
Category: "API RESOURCE",
Suggest: true,
Commands: []*cli.Command{
&utilityPrefetch,
&utilityPrefetchByEmail,
},
},
{
Name: "@manpages",
Usage: "Generate documentation for 'man'",
UsageText: "context-dev @manpages [-o context-dev.1] [--gzip]",
Hidden: true,
Action: generateManpages,
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "write manpages to the given folder",
Value: "man",
},
&cli.BoolFlag{
Name: "gzip",
Aliases: []string{"z"},
Usage: "output gzipped manpage files to .gz",
Value: true,
},
&cli.BoolFlag{
Name: "text",
Aliases: []string{"z"},
Usage: "output uncompressed text files",
Value: false,
},
},
},
{
Name: "__complete",
Hidden: true,
HideHelpCommand: true,
Action: autocomplete.ExecuteShellCompletion,
},
{
Name: "@completion",
Hidden: true,
HideHelpCommand: true,
Action: autocomplete.OutputCompletionScript,
},
},
HideHelpCommand: true,
}
}
func generateManpages(ctx context.Context, c *cli.Command) error {
manpage, err := docs.ToManWithSection(Command, 1)
if err != nil {
return err
}
dir := c.String("output")
err = os.MkdirAll(filepath.Join(dir, "man1"), 0755)
if err != nil {
// handle error
}
if c.Bool("text") {
file, err := os.Create(filepath.Join(dir, "man1", "context-dev.1"))
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(manpage); err != nil {
return err
}
}
if c.Bool("gzip") {
file, err := os.Create(filepath.Join(dir, "man1", "context-dev.1.gz"))
if err != nil {
return err
}
defer file.Close()
gzWriter := gzip.NewWriter(file)
defer gzWriter.Close()
_, err = gzWriter.Write([]byte(manpage))
if err != nil {
return err
}
}
fmt.Printf("Wrote manpages to %s\n", dir)
return nil
}