Skip to content

Commit

Permalink
feat(input): support multiple lines with pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
riza committed Oct 28, 2023
1 parent c7ddbce commit cf965e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
wb
.idea/*
.idea/*
test.txt
92 changes: 47 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"encoding/json"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"log"
"time"

"io"
"net/http"
"os"
"time"
)

const (
version = "0.0.4"
version = "0.0.5"

wbSnapshotApiURL = "https://web.archive.org/cdx/search/xd?output=json&url=%s&fl=timestamp,original&collapse=digest&gzip=false&filter=statuscode:200"
wbFileURL = "https://web.archive.org/web/%sid_/%s"
Expand Down Expand Up @@ -43,69 +44,70 @@ func main() {
os.Exit(0)
}

var url string
var urls []string
if flag.NArg() > 0 {
url = flag.Arg(0)
urls[0] = flag.Arg(0)
} else {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
url = sc.Text()
}

if err := sc.Err(); err != nil {
log.Fatalf("failed to read input: %s\n", err)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
}

client := http.Client{
Timeout: time.Second * 5,
}

snapshots, err := getSnapshots(client, url)
if err != nil {
log.Fatalf("failed to snapshots: %s\n", err)
}
for _, url := range urls {
if len(urls) > 1 {
fmt.Println("// Snapshots for", url)
}
snapshots, err := getSnapshots(client, url)
if err != nil {
log.Printf("failed to snapshots: %s\n", err)
}

if *flagSnapshots {
fmt.Println("Snapshots for", url)
for _, s := range snapshots {
parsedTime, err := time.Parse(parseTimeLayout, s[0])
if err != nil {
log.Fatalf("failed to parse time: %s\n", err)
if *flagSnapshots {
fmt.Println("Snapshots for", url)
for _, s := range snapshots {
parsedTime, err := time.Parse(parseTimeLayout, s[0])
if err != nil {
log.Fatalf("failed to parse time: %s\n", err)
}
fmt.Printf("* %s | %s | %s\n", s[0], parsedTime.Format(viewTimeLayout), s[1])
}
fmt.Printf("* %s | %s | %s\n", s[0], parsedTime.Format(viewTimeLayout), s[1])
os.Exit(0)
}
os.Exit(0)
}

selectedSnapshot := snapshots[len(snapshots)-1]
if *flagDate != "" {
for _, s := range snapshots {
if s[0] == *flagDate {
selectedSnapshot = s
break
selectedSnapshot := snapshots[len(snapshots)-1]
if *flagDate != "" {
for _, s := range snapshots {
if s[0] == *flagDate {
selectedSnapshot = s
break
}
}
}
}

if *flagGetAllSnapshots {
for _, s := range snapshots {
snapshotContent, err := getSnapshotContent(client, s[0], s[1])
if err != nil {
log.Fatalf("failed to read input: %s\n", err)
if *flagGetAllSnapshots {
for _, s := range snapshots {
snapshotContent, err := getSnapshotContent(client, s[0], s[1])
if err != nil {
log.Fatalf("failed to read input: %s\n", err)
}

io.Copy(os.Stdout, snapshotContent)
}
continue
}

io.Copy(os.Stdout, snapshotContent)
snapshotContent, err := getSnapshotContent(client, selectedSnapshot[0], selectedSnapshot[1])
if err != nil {
log.Fatalf("failed to read input: %s\n", err)
}
os.Exit(0)
}

snapshotContent, err := getSnapshotContent(client, selectedSnapshot[0], selectedSnapshot[1])
if err != nil {
log.Fatalf("failed to read input: %s\n", err)
io.Copy(os.Stdout, snapshotContent)
}

io.Copy(os.Stdout, snapshotContent)
}

func getSnapshots(c http.Client, url string) ([][]string, error) {
Expand Down

0 comments on commit cf965e1

Please sign in to comment.