-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
103 lines (81 loc) · 2.09 KB
/
main.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
package main
import (
"fmt"
"os"
"context"
"os/exec"
"path"
"net/url"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/jirwin/ipfs-archive/scraper"
"github.com/jirwin/ipfs-archive/version"
"github.com/pborman/uuid"
"go.uber.org/zap"
cli "gopkg.in/urfave/cli.v1"
)
func main() {
app := cli.NewApp()
app.Name = "ipfs-archive"
app.Usage = "Use to archive url to ipfs"
app.Version = version.Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url",
Usage: "Archive `URL` to ipfs",
},
}
app.Action = run
app.Run(os.Args)
}
func run(cliCtx *cli.Context) error {
ctx := context.Background()
logger, err := zap.NewProduction()
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
ctx = context.WithValue(ctx, "logger", logger)
if !cliCtx.IsSet("url") {
cli.ShowAppHelpAndExit(cliCtx, -1)
}
seedUrl := cliCtx.String("url")
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.FinalMSG = fmt.Sprintf("Fetched %s\n", seedUrl)
s.Prefix = fmt.Sprintf("Fetching %s ", seedUrl)
s.Writer = os.Stderr
s.Start()
scraper := scraper.NewScraper(ctx, logger, uuid.New(), seedUrl)
err = scraper.Scrape()
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
s.Stop()
s = spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.FinalMSG = fmt.Sprintf("Archived %s\n", seedUrl)
s.Prefix = fmt.Sprintf("Archiving %s ", seedUrl)
s.Start()
cmd := exec.Command("ipfs", "add", "-r", path.Join(scraper.SnapshotDir, scraper.Id))
output, err := cmd.Output()
if err != nil {
logger.Error("Error running ipfs command", zap.Error(err))
return cli.NewExitError("", -1)
}
outputLines := strings.Split(string(output), "\n")
parts := strings.Split(outputLines[len(outputLines)-2], " ")
if len(parts) < 2 {
return cli.NewExitError("Unexpected ipfs output.", -1)
}
archiveUrl := &url.URL{
Scheme: "https",
Host: "ipfs.io",
Path: path.Join("ipfs", parts[1]),
}
s.Stop()
fmt.Printf("%s is archived at %s\n", seedUrl, archiveUrl.String())
err = scraper.Cleanup()
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
return nil
}