Adding optional start and end dates for extrapolation
This commit is contained in:
9
Makefile
Normal file
9
Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
BINARY_NAME=cashflow
|
||||
|
||||
default: build
|
||||
|
||||
build:
|
||||
go build -o $(BINARY_NAME) -ldflags "-s -w"
|
||||
|
||||
clean:
|
||||
go clean
|
||||
11
go.mod
11
go.mod
@@ -1,5 +1,10 @@
|
||||
module cashflow
|
||||
module github.com/samuelattwood/cashflow
|
||||
|
||||
go 1.17
|
||||
go 1.19
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
require (
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
|
||||
17
go.sum
17
go.sum
@@ -1,4 +1,17 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
35
main.go
35
main.go
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -39,6 +41,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigYaml struct {
|
||||
Start string
|
||||
End string
|
||||
Accounts []Account
|
||||
Credit []TransactionYaml
|
||||
Transactions []TransactionYaml
|
||||
@@ -118,7 +122,7 @@ func parseDate(date string) (time.Time, bool) {
|
||||
if date == "first" {
|
||||
formalDate = current
|
||||
} else if date == "last" {
|
||||
formalDate = current.AddDate(0, 1, -current.Day())
|
||||
formalDate = lastOfMonth(current)
|
||||
} else {
|
||||
dateString = fmt.Sprintf("%d-%d-%s",
|
||||
time.Now().Year(), int(time.Now().Month()), date)
|
||||
@@ -219,6 +223,14 @@ func executeTransaction(transactionLog *TransactionLog, accounts []Account, tran
|
||||
return executed
|
||||
}
|
||||
|
||||
func firstOfMonth(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func lastOfMonth(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func calculateFrequency(frequency string, startDate time.Time, endDate time.Time, hardDate bool) []time.Time {
|
||||
base := time.Now()
|
||||
current := time.Date(base.Year(),
|
||||
@@ -237,7 +249,11 @@ func calculateFrequency(frequency string, startDate time.Time, endDate time.Time
|
||||
} else if frequency == "biweekly" {
|
||||
initDate = initDate.AddDate(0, 0, 14)
|
||||
} else if frequency == "monthly" {
|
||||
if initDate == lastOfMonth(initDate) {
|
||||
initDate = lastOfMonth(firstOfMonth(initDate).AddDate(0, 1, 0))
|
||||
} else {
|
||||
initDate = initDate.AddDate(0, 1, 0)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
@@ -260,7 +276,7 @@ func printOutput(date time.Time, initialBalances []Account, executedCharges []Tr
|
||||
divider2 := delim2 + "|"
|
||||
|
||||
fmt.Println(divider)
|
||||
output := fmt.Sprintf(date.Format("Jan 02 2006"))
|
||||
output := fmt.Sprint(date.Format("Jan 02 2006"))
|
||||
rear_spacer := strings.Repeat(" ", 65-len(output))
|
||||
fmt.Printf("%s%s |\n", output, rear_spacer)
|
||||
for _, v := range initialBalances {
|
||||
@@ -325,6 +341,21 @@ func main() {
|
||||
config := parseConfig(&configYaml)
|
||||
preFetchDates(config.Credit, &transactionLog)
|
||||
preFetchDates(config.Transactions, &transactionLog)
|
||||
|
||||
if configYaml.Start != "" {
|
||||
transactionLog.StartDate, err = time.Parse("2006-1-2", configYaml.Start)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
if configYaml.End != "" {
|
||||
transactionLog.EndDate, err = time.Parse("2006-1-2", configYaml.End)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
collectTransactions(config.Credit, &transactionLog)
|
||||
collectTransactions(config.Transactions, &transactionLog)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user