diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-17 20:15:41 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-17 20:15:41 +0200 |
| commit | 5e2b1d1dc902ba53fc537b31e835d82c0e55dfb6 (patch) | |
| tree | aad5c1904e2a552672606c739a9dec595b3326ef | |
| parent | 8a44dc0856d7bf2cdc2eafa8594f4a47d488c3fd (diff) | |
print json correctly
| -rw-r--r-- | cmd/flyscrape/run.go | 14 | ||||
| -rw-r--r-- | cmd/flyscrape/watch.go | 2 | ||||
| -rw-r--r-- | result.json | 79 | ||||
| -rw-r--r-- | utils.go | 17 |
4 files changed, 102 insertions, 10 deletions
diff --git a/cmd/flyscrape/run.go b/cmd/flyscrape/run.go index 2577c25..9a2a7bb 100644 --- a/cmd/flyscrape/run.go +++ b/cmd/flyscrape/run.go @@ -45,16 +45,24 @@ func (c *RunCommand) Run(args []string) error { count := 0 start := time.Now() + for result := range svc.Scrape() { + if count > 0 { + fmt.Println(",") + } + if count == 0 { + fmt.Println("[") + } if *noPrettyPrint { - flyscrape.Print(result) + fmt.Print(flyscrape.Print(result, " ")) } else { - flyscrape.PrettyPrint(result) + fmt.Print(flyscrape.PrettyPrint(result, " ")) } count++ } - log.Printf("Scraped %d websites in %v\n", count, time.Since(start)) + fmt.Println("\n]") + log.Printf("Scraped %d websites in %v\n", count, time.Since(start)) return nil } diff --git a/cmd/flyscrape/watch.go b/cmd/flyscrape/watch.go index 99fac4e..46da25e 100644 --- a/cmd/flyscrape/watch.go +++ b/cmd/flyscrape/watch.go @@ -50,7 +50,7 @@ func (c *WatchCommand) Run(args []string) error { screen.Clear() screen.MoveTopLeft() - flyscrape.PrettyPrint(result) + fmt.Println(flyscrape.PrettyPrint(result, "")) return nil }) if err != nil && err != flyscrape.StopWatch { diff --git a/result.json b/result.json new file mode 100644 index 0000000..88bdc55 --- /dev/null +++ b/result.json @@ -0,0 +1,79 @@ +[ + { + "url": "https://esbuild.github.io/plugins/", + "data": { + "body": "The plugin API allows you to inject code into various parts of the build process. Unlike the rest of the API, it's not available from the command line. You must write either JavaScript or Go code to use the plugin API. Plugins can also only be used with the build API, not with the transform API.", + "headline": "Plugins" + } + }, + { + "url": "https://esbuild.github.io/try/", + "data": { + "body": "", + "headline": "esbuild" + } + }, + { + "url": "https://esbuild.github.io/getting-started/", + "data": { + "body": "First, download and install the esbuild command locally. A prebuilt native executable can be installed using npm (which is automatically installed when you install the node JavaScript runtime):", + "headline": "Getting Started" + } + }, + { + "url": "https://esbuild.github.io/", + "data": { + "body": "Our current build tools for the web are 10-100x slower than they could be. The main goal of the esbuild bundler project is to bring about a new era of build tool performance, and create an easy-to-use modern bundler along the way.", + "headline": "esbuild" + } + }, + { + "url": "https://esbuild.github.io/analyze/", + "data": { + "body": "", + "headline": "esbuild" + } + }, + { + "url": "https://esbuild.github.io/api/", + "data": { + "body": "The API can be accessed in one of three languages: on the command line, in JavaScript, and in Go. The concepts and parameters are largely identical between the three languages so they will be presented together here instead of having separate documentation for each language. You can switch between languages using the CLI, JS, and Go tabs in the top-right corner of each code example. Some specifics for each language:", + "headline": "API" + } + }, + { + "url": "https://esbuild.github.io/faq/", + "data": { + "body": "This is a collection of common questions about esbuild. You can also ask questions on the GitHub issue tracker.", + "headline": "FAQ" + } + }, + { + "url": "https://esbuild.github.io/content-types/", + "data": { + "body": "All of the built-in content types are listed below. Each content type has an associated \"loader\" which tells esbuild how to interpret the file contents. Some file extensions already have a loader configured for them by default, although the defaults can be overridden.", + "headline": "Content Types" + } + }, + { + "url": "https://nodejs.org/api/packages.html", + "data": { + "body": "", + "headline": "Node.js v20.5.1 documentation" + } + }, + { + "url": "https://nodejs.org/api/modules.html", + "data": { + "body": "", + "headline": "Node.js v20.5.1 documentation" + } + }, + { + "url": "https://nodejs.org/api/crypto.html", + "data": { + "body": "", + "headline": "Node.js v20.5.1 documentation" + } + } +] @@ -1,19 +1,24 @@ package flyscrape import ( + "bytes" "encoding/json" - "os" + "strings" ) -func PrettyPrint(v any) { - enc := json.NewEncoder(os.Stdout) +func PrettyPrint(v any, prefix string) string { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) - enc.SetIndent("", " ") + enc.SetIndent(prefix, " ") enc.Encode(v) + return prefix + strings.TrimSuffix(buf.String(), "\n") } -func Print(v any) { - enc := json.NewEncoder(os.Stdout) +func Print(v any, prefix string) string { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) enc.Encode(v) + return prefix + strings.TrimSuffix(buf.String(), "\n") } |