|
2 | 2 | package main |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "os" |
| 8 | + "path/filepath" |
7 | 9 | "regexp" |
8 | 10 | "runtime" |
9 | 11 | "strings" |
@@ -50,11 +52,13 @@ func main() { |
50 | 52 | } |
51 | 53 |
|
52 | 54 | var summary, showVersion, update bool |
| 55 | + var jsonFile string |
53 | 56 | flag.BoolVarP(&summary, "summary", "s", false, "print results when all are found (default false)") |
54 | 57 | flag.BoolVarP(&options.CaseSensitive, "case-sensitive", "c", false, "case sensitive match (default false)") |
55 | 58 | flag.IntVarP(&options.Threads, "threads", "t", options.Cores, "threads") |
56 | 59 | flag.IntVarP(&options.LimitResults, "limit", "l", 1, "limit results to n (exists after)") |
57 | 60 | flag.StringVarP(&options.Timeout, "timeout", "T", "", "quit after n minutes (allowed suffixes: s/m/h) (default \"\")") |
| 61 | + flag.StringVarP(&jsonFile, "json", "j", "", "write results to JSON file") |
58 | 62 | flag.BoolVarP(&showVersion, "version", "v", false, "show app version") |
59 | 63 | flag.BoolVarP(&update, "update", "u", false, "update to latest release") |
60 | 64 |
|
@@ -174,15 +178,37 @@ func main() { |
174 | 178 | } |
175 | 179 |
|
176 | 180 | fmt.Printf("\nPress Ctrl-c to cancel\n\n") |
177 | | - if !summary { |
| 181 | + |
| 182 | + var results []keygen.Pair |
| 183 | + if !summary && jsonFile == "" { |
178 | 184 | c.Find(func(match keygen.Pair) { |
179 | 185 | fmt.Printf("private: %s public: %s\n", match.Private, match.Public) |
180 | 186 | }) |
181 | 187 | } else { |
182 | | - for _, match := range c.CollectToSlice() { |
| 188 | + results = c.CollectToSlice() |
| 189 | + for _, match := range results { |
183 | 190 | fmt.Printf("private: %s public: %s\n", match.Private, match.Public) |
184 | 191 | } |
185 | 192 | } |
| 193 | + |
| 194 | + if jsonFile != "" { |
| 195 | + jsonFile = filepath.Clean(jsonFile) |
| 196 | + if results == nil { |
| 197 | + results = []keygen.Pair{} |
| 198 | + } |
| 199 | + data, err := json.MarshalIndent(struct { |
| 200 | + Results []keygen.Pair `json:"results"` |
| 201 | + }{Results: results}, "", " ") |
| 202 | + if err != nil { |
| 203 | + fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err) |
| 204 | + os.Exit(1) |
| 205 | + } |
| 206 | + if err := os.WriteFile(jsonFile, data, 0644); err != nil { |
| 207 | + fmt.Fprintf(os.Stderr, "Error writing JSON file: %v\n", err) |
| 208 | + os.Exit(1) |
| 209 | + } |
| 210 | + fmt.Printf("\nResults written to %s\n", jsonFile) |
| 211 | + } |
186 | 212 | } |
187 | 213 |
|
188 | 214 | // parseTimeout parses the timeout string to a time.Duration. If the input is |
|
0 commit comments