summaryrefslogtreecommitdiff
path: root/booktopipeline/main.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-08-28 18:12:20 +0100
committerNick White <git@njw.name>2019-08-28 18:12:20 +0100
commit48d894897894a010db1661259fa375e1be36e659 (patch)
treeb44f1c9f40577b8acc00d4fa865ebfa0ccc267de /booktopipeline/main.go
parent74b89d5f2cd968e58be9a28f1dbce7a1ebda581e (diff)
Move booktopipeline and mkpipeline into bookpipeline/cmd
Diffstat (limited to 'booktopipeline/main.go')
-rw-r--r--booktopipeline/main.go116
1 files changed, 0 insertions, 116 deletions
diff --git a/booktopipeline/main.go b/booktopipeline/main.go
deleted file mode 100644
index 7c4e004..0000000
--- a/booktopipeline/main.go
+++ /dev/null
@@ -1,116 +0,0 @@
-package main
-// TODO: have logs go somewhere useful, like email
-
-import (
- "flag"
- "log"
- "os"
- "path/filepath"
-
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/aws/session"
- "github.com/aws/aws-sdk-go/service/s3/s3manager"
- "github.com/aws/aws-sdk-go/service/sqs"
-)
-
-// null writer to enable non-verbose logging to be discarded
-type NullWriter bool
-func (w NullWriter) Write(p []byte) (n int, err error) {
- return len(p), nil
-}
-var verboselog *log.Logger
-
-type fileWalk chan string
-
-func (f fileWalk) Walk(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- f <- path
- }
- return nil
-}
-
-func main() {
- verbose := flag.Bool("v", false, "Verbose")
- flag.Parse()
-
- if flag.NArg() < 1 {
- log.Fatal("Usage: booktopipeline [-v] bookdir [bookname]\n\nUploads the book in bookdir to the S3 'inprogress' bucket and adds it to the 'preprocess' SQS queue\nIf bookname is omitted the last part of the bookdir is used\n")
- }
-
- bookdir := flag.Arg(0)
- var bookname string
- if flag.NArg() > 2 {
- bookname = flag.Arg(1)
- } else {
- bookname = filepath.Base(bookdir)
- }
-
- if *verbose {
- verboselog = log.New(os.Stdout, "", log.LstdFlags)
- } else {
- var n NullWriter
- verboselog = log.New(n, "", log.LstdFlags)
- }
-
- verboselog.Println("Setting up AWS session")
- sess, err := session.NewSession(&aws.Config{
- Region: aws.String("eu-west-2"),
- })
- if err != nil {
- log.Fatalln("Error: failed to set up aws session:", err)
- }
- sqssvc := sqs.New(sess)
- uploader := s3manager.NewUploader(sess)
-
- qname := "rescribepreprocess"
- verboselog.Println("Getting Queue URL for", qname)
- result, err := sqssvc.GetQueueUrl(&sqs.GetQueueUrlInput{
- QueueName: aws.String(qname),
- })
- if err != nil {
- log.Fatalln("Error getting queue URL for", qname, ":", err)
- }
- qurl := *result.QueueUrl
-
- // concurrent walking upload based on example at
- // https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/sdk-utilities.html
- verboselog.Println("Walking", bookdir)
- walker := make(fileWalk)
- go func() {
- err = filepath.Walk(bookdir, walker.Walk)
- if err != nil {
- log.Fatalln("Filesystem walk failed:", err)
- }
- close(walker)
- }()
-
- for path := range walker {
- verboselog.Println("Uploading", path)
- name := filepath.Base(path)
- file, err := os.Open(path)
- if err != nil {
- log.Fatalln("Open file", path, "failed:", err)
- }
- defer file.Close()
- _, err = uploader.Upload(&s3manager.UploadInput{
- Bucket: aws.String("rescribeinprogress"),
- Key: aws.String(filepath.Join(bookname, name)),
- Body: file,
- })
- if err != nil {
- log.Fatalln("Failed to upload", path, err)
- }
- }
-
- verboselog.Println("Sending message", bookname, "to queue", qurl)
- _, err = sqssvc.SendMessage(&sqs.SendMessageInput{
- MessageBody: aws.String(bookname),
- QueueUrl: &qurl,
- })
- if err != nil {
- log.Fatalln("Error adding book to queue:", err)
- }
-}