summaryrefslogtreecommitdiff
path: root/cmd/rmbook/main.go
blob: c195d85089878af5e959c2ab927b1451f1fff476 (plain)
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
// Copyright 2020 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

// rmbook removes a book from cloud storage.
package main

import (
	"flag"
	"fmt"
	"log"

	"rescribe.xyz/bookpipeline"
)

const usage = `Usage: rmbook [-dryrun] bookname

Removes a book from cloud storage.
`

// 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
}

type RmPipeliner interface {
        MinimalInit() error
        WIPStorageId() string
	DeleteObjects(bucket string, keys []string) error
	ListObjects(bucket string, prefix string) ([]string, error)
}

func main() {
	dryrun := flag.Bool("dryrun", false, "print which files would be deleted but don't delete")
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() < 1 {
		flag.Usage()
		return
	}

	var n NullWriter
	verboselog := log.New(n, "", log.LstdFlags)

	var conn RmPipeliner
	conn = &bookpipeline.AwsConn{Region: "eu-west-2", Logger: verboselog}

	fmt.Println("Setting up cloud connection")
	err := conn.MinimalInit()
	if err != nil {
		log.Fatalln("Error setting up cloud connection:", err)
	}

	bookname := flag.Arg(0)

	fmt.Println("Getting list of files for book")
	objs, err := conn.ListObjects(conn.WIPStorageId(), bookname)
	if err != nil {
		log.Fatalln("Error in listing book items:", err)
	}

	if len(objs) == 0 {
		log.Fatalln("No files found for book:", bookname)
	}

	if *dryrun {
		fmt.Printf("I would delete these files:\n")
		for _, v := range objs {
			fmt.Println(v)
		}
		return
	}

	fmt.Println("Deleting all files for book")
	err = conn.DeleteObjects(conn.WIPStorageId(), objs)
	if err != nil {
		log.Fatalln("Error deleting book files:", err)
	}

	fmt.Println("Finished deleting files")
}