summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2020-12-14 17:08:14 +0000
committerNick White <git@njw.name>2020-12-14 17:08:14 +0000
commit9147e57a3a634ad303e8f1e7c456988996d5c75b (patch)
tree026e914ecce14c8cfdb7539eb71c36388b77cd03
parent452d3d2b6023eb245cb0586954a5fb510df567e1 (diff)
Add rmbook tool
-rw-r--r--aws.go17
-rw-r--r--cmd/rmbook/main.go78
2 files changed, 95 insertions, 0 deletions
diff --git a/aws.go b/aws.go
index 6b707fe..40c452d 100644
--- a/aws.go
+++ b/aws.go
@@ -379,6 +379,23 @@ func (a *AwsConn) ListObjectPrefixes(bucket string) ([]string, error) {
return prefixes, err
}
+// Deletes a list of objects
+func (a *AwsConn) DeleteObjects(bucket string, keys []string) error {
+ objs := []*s3.ObjectIdentifier{}
+ for _, v := range keys {
+ o := s3.ObjectIdentifier{Key: aws.String(v)}
+ objs = append(objs, &o)
+ }
+ _, err := a.s3svc.DeleteObjects(&s3.DeleteObjectsInput{
+ Bucket: aws.String(bucket),
+ Delete: &s3.Delete{
+ Objects: objs,
+ Quiet: aws.Bool(true),
+ },
+ })
+ return err
+}
+
// CreateBucket creates a new S3 bucket
func (a *AwsConn) CreateBucket(name string) error {
_, err := a.s3svc.CreateBucket(&s3.CreateBucketInput{
diff --git a/cmd/rmbook/main.go b/cmd/rmbook/main.go
new file mode 100644
index 0000000..8d434a9
--- /dev/null
+++ b/cmd/rmbook/main.go
@@ -0,0 +1,78 @@
+// 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 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() {
+ 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)
+ }
+
+ 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")
+}