commit 6b43093695d72086c057fb27dce71e1aa8ee8ade
parent 1facc999d821c917ae1997418d4f518d2bbf17a5
Author: Chris Bracken <chris@bracken.jp>
Date: Mon, 3 Jan 2022 13:15:49 -0800
Refactor into packages, add tests.
Break out package convert and add unit tests.
Diffstat:
5 files changed, 107 insertions(+), 105 deletions(-)
diff --git a/convert/convert.go b/convert/convert.go
@@ -0,0 +1,51 @@
+// CLI tool to rename all RBC statement PDFs in the current directory to
+// YYYY-MM-DD.pdf format.
+package convert
+
+import (
+ "fmt"
+ "strings"
+)
+
+// Format: ACCTNO-YYYYmmmDD-YYYYmmmDD.pdf
+func RbcDirectInvesting(fname string) (string, error) {
+ date := strings.Split(fname, "-")[2][0:9]
+ y := date[0:4]
+ m, err := getMonthNumber(strings.ToLower(date[4:7]))
+ if err != nil {
+ return "", err
+ }
+ d := date[7:9]
+ newFname := fmt.Sprintf("%s-%s-%s.pdf", y, m, d)
+ return newFname, nil
+}
+
+func getMonthNumber(m string) (string, error) {
+ switch {
+ case m == "jan":
+ return "01", nil
+ case m == "feb":
+ return "02", nil
+ case m == "mar":
+ return "03", nil
+ case m == "apr":
+ return "04", nil
+ case m == "may":
+ return "05", nil
+ case m == "jun":
+ return "06", nil
+ case m == "jul":
+ return "07", nil
+ case m == "aug":
+ return "08", nil
+ case m == "sep":
+ return "09", nil
+ case m == "oct":
+ return "10", nil
+ case m == "nov":
+ return "11", nil
+ case m == "dec":
+ return "12", nil
+ }
+ return "", fmt.Errorf("Bad month name: %s", m)
+}
diff --git a/convert/convert_test.go b/convert/convert_test.go
@@ -0,0 +1,30 @@
+package convert_test
+
+import (
+ "testing"
+
+ "git.bracken.jp/fixstmt/convert"
+)
+
+func TestRbcDirectInvesting(t *testing.T) {
+ testCases := []struct {
+ name string
+ want string
+ }{
+ {"123456789-2007Jan10-2007feb09.pdf", "2007-02-09.pdf"},
+ {"987654321-2020mAr31-2020apR30.pdf", "2020-04-30.pdf"},
+ {"123456-2021MAY01-2021mAY31.pdf", "2021-05-31.pdf"},
+ {"8905672341-2022Jun21-2022Jul20.pdf", "2022-07-20.pdf"},
+ {"2345678901-2022Aug15-2022Sep14.pdf", "2022-09-14.pdf"},
+ {"2345678901-2022Oct31-2022Nov30.pdf", "2022-11-30.pdf"},
+ {"2345678901-2022Nov29-2023Jan28.pdf", "2023-01-28.pdf"},
+ }
+ for _, tc := range testCases {
+ got, err := convert.RbcDirectInvesting(tc.name)
+ if err != nil {
+ t.Errorf("Failed to convert file %s", tc.name)
+ } else if got != tc.want {
+ t.Errorf("In(%s) = %s; want %s", tc.name, got, tc.want)
+ }
+ }
+}
diff --git a/fixrbc.go b/fixrbc.go
@@ -1,80 +0,0 @@
-// CLI tool to rename all RBC statement PDFs in the current directory to
-// YYYY-MM-DD.pdf format.
-package main
-
-import (
- "errors"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "regexp"
- "strings"
-)
-
-func IsValidFilename(n string) bool {
- r := regexp.MustCompile(`\d+-\d{4}[A-Za-z]{3}\d\d-\d{4}[A-Za-z]{3}\d\d\.pdf`)
- return r.MatchString(n)
-}
-
-// SanitizedFilename returns a filename in YYYY-mm-dd.pdf format. As input, it
-// takes a filename in default RBC format (ACCTNO-YYYYmmmDD-YYYYmmmDD.pdf).
-func SanitizedFilename(n string) (string, error) {
- if !IsValidFilename(n) {
- return "", errors.New(fmt.Sprintf("invalid filename %s", n))
- }
- date := strings.Split(n, "-")[2][0:9]
- y := date[0:4]
- m, err := getMonthNumber(strings.ToLower(date[4:7]))
- if err != nil {
- return "", err
- }
- d := date[7:9]
- return fmt.Sprintf("%s-%s-%s.pdf", y, m, d), nil
-}
-
-func main() {
- files, err := ioutil.ReadDir("./")
- if err != nil {
- log.Fatal(err)
- }
- for _, f := range files {
- if IsValidFilename(f.Name()) {
- newFname, err := SanitizedFilename(f.Name())
- if err != nil {
- log.Fatal(err)
- }
- os.Rename(f.Name(), newFname)
- }
- }
-}
-
-func getMonthNumber(m string) (string, error) {
- switch {
- case m == "jan":
- return "01", nil
- case m == "feb":
- return "02", nil
- case m == "mar":
- return "03", nil
- case m == "apr":
- return "04", nil
- case m == "may":
- return "05", nil
- case m == "jun":
- return "06", nil
- case m == "jul":
- return "07", nil
- case m == "aug":
- return "08", nil
- case m == "sep":
- return "09", nil
- case m == "oct":
- return "10", nil
- case m == "nov":
- return "11", nil
- case m == "dec":
- return "12", nil
- }
- return "", fmt.Errorf("Bad month name: %s", m)
-}
diff --git a/fixrbc_test.go b/fixrbc_test.go
@@ -1,25 +0,0 @@
-package main
-
-import "testing"
-
-func TestSanitizedFilename(t *testing.T) {
- cases := []struct {
- desc string
- in string
- expected string
- }{
- {"TestJanFeb", "123456-2021jan01-2021feB02.pdf", "2021-02-02.pdf"},
- {"TestMarApr", "123456-2021mAr01-2021aPR02.pdf", "2021-04-02.pdf"},
- {"TestMayJun", "123456-2021May01-2021JuN02.pdf", "2021-06-02.pdf"},
- {"TestJulAug", "123456-2021Jul01-2021AUG02.pdf", "2021-08-02.pdf"},
- {"TestSepOct", "123456-2021sep01-2021ocT02.pdf", "2021-10-02.pdf"},
- {"TestNovDec", "123456-2021nOv01-2021dEC02.pdf", "2021-12-02.pdf"},
- }
-
- for _, tc := range cases {
- actual, _ := SanitizedFilename(tc.in)
- if actual != tc.expected {
- t.Errorf("%s: expected: %s got %s for input: %s", tc.desc, tc.expected, actual, tc.in)
- }
- }
-}
diff --git a/main.go b/main.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "os"
+ "strings"
+
+ "git.bracken.jp/fixstmt/convert"
+)
+
+func main() {
+ files, err := ioutil.ReadDir("./")
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, f := range files {
+ if strings.HasSuffix(f.Name(), ".pdf") {
+ newFname, err := convert.RbcDirectInvesting(f.Name())
+ if err != nil {
+ log.Fatal(err)
+ }
+ os.Rename(f.Name(), newFname)
+ }
+ }
+}