From 9147e57a3a634ad303e8f1e7c456988996d5c75b Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 14 Dec 2020 17:08:14 +0000 Subject: Add rmbook tool --- cmd/rmbook/main.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 cmd/rmbook/main.go (limited to 'cmd/rmbook') 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") +} -- cgit v1.2.1-24-ge1ad From fb1069b504e8cd37b9a2bcdccefa9699d0e1dee9 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 15 Dec 2020 12:37:43 +0000 Subject: [rmbook] Add -dryrun flag --- cmd/rmbook/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'cmd/rmbook') diff --git a/cmd/rmbook/main.go b/cmd/rmbook/main.go index 8d434a9..c195d85 100644 --- a/cmd/rmbook/main.go +++ b/cmd/rmbook/main.go @@ -13,7 +13,7 @@ import ( "rescribe.xyz/bookpipeline" ) -const usage = `Usage: rmbook bookname +const usage = `Usage: rmbook [-dryrun] bookname Removes a book from cloud storage. ` @@ -33,6 +33,7 @@ type RmPipeliner interface { } 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() @@ -68,6 +69,14 @@ func main() { 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 { -- cgit v1.2.1-24-ge1ad From 7d9f77c2f1102aec026d1af78d1fe4725ed76674 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 15 Dec 2020 12:38:36 +0000 Subject: [rmbook] Append / to end of bookname, to ensure e.g. "1" doesnt match all books starting with "1" --- cmd/rmbook/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd/rmbook') diff --git a/cmd/rmbook/main.go b/cmd/rmbook/main.go index c195d85..fcacc2e 100644 --- a/cmd/rmbook/main.go +++ b/cmd/rmbook/main.go @@ -57,7 +57,7 @@ func main() { log.Fatalln("Error setting up cloud connection:", err) } - bookname := flag.Arg(0) + bookname := flag.Arg(0) + "/" fmt.Println("Getting list of files for book") objs, err := conn.ListObjects(conn.WIPStorageId(), bookname) -- cgit v1.2.1-24-ge1ad