Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: ci.yaml

on:
pull_request:
branches: [ main ]

jobs:
lint-and-test:
name: Lint and Test
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.24'

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.4.0

- name: Run unit tests
run: go test -v -race ./...
170 changes: 170 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
version: "2"

linters:
default: none
enable:
# enabled by default by golangci-lint
- errcheck # Errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases.
- govet # Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes.
- ineffassign # Detects when assignments to existing variables are not used.
- staticcheck # It's the set of rules from staticcheck.
- unused # Checks Go code for unused constants, variables, functions and types.
# extra enabled by us
- arangolint # Opinionated best practices for arangodb client.
- asasalint # Check for pass []any as any in variadic func(...any).
- asciicheck # Checks that all code identifiers does not have non-ASCII symbols in the name.
- bidichk # Checks for dangerous unicode character sequences.
- bodyclose # Checks whether HTTP response body is closed successfully.
- canonicalheader # Canonicalheader checks whether net/http.Header uses canonical header.
- containedctx # Containedctx is a linter that detects struct contained context.Context field.
- contextcheck # Check whether the function uses a non-inherited context.
- copyloopvar # A linter detects places where loop variables are copied.
- cyclop # Checks function and package cyclomatic complexity.
- decorder # Check declaration order and count of types, constants, variables and functions.
- depguard # Go linter that checks if package imports are in a list of acceptable packages.
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, #= f()).
- dupl # Detects duplicate fragments of code.
- dupword # Checks for duplicate words in the source code.
- durationcheck # Check for two durations multiplied together.
- embeddedstructfieldcheck # Embedded types should be at the top of the field list of a struct, and there must be an empty line separating embedded fields from regular fields.
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occurrences where the check for the returned error can be omitted.
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
- errorlint # Find code that can cause problems with the error wrapping scheme introduced in Go 1.13.
- exhaustive # Check exhaustiveness of enum switch statements.
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions.
- fatcontext # Detects nested contexts in loops and function literals.
- forbidigo # Forbids identifiers.
- forcetypeassert # Find forced type assertions.
- funcorder # Checks the order of functions, methods, and constructors.
- funlen # Checks for long functions.
- ginkgolinter # Enforces standards of using ginkgo and gomega.
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go #) are valid.
- gochecksumtype # Run exhaustiveness checks on Go "sum types".
- gocognit # Computes and checks the cognitive complexity of functions.
- goconst # Finds repeated strings that could be replaced by a constant.
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
- godot # Check if comments end in a period.
- goheader # Check if file header matches to pattern.
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
- gomodguard # Allow and blocklist linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
- goprintffuncname # Checks that printf-like functions are named with `f` at the end.
- gosec # Inspects source code for security problems.
- gosmopolitan # Report certain i18n/l10n anti-patterns in your Go codebase.
- grouper # Analyze expression groups.
- iface # Detect the incorrect use of interfaces, helping developers avoid interface pollution.
- importas # Enforces consistent import aliases.
- inamedparam # Reports interfaces with unnamed method parameters.
- interfacebloat # A linter that checks the number of methods inside an interface.
- intrange # Intrange is a linter to find places where for loops could make use of an integer range.
- ireturn # Accept Interfaces, Return Concrete Types.
- loggercheck # Checks key value pairs for common logger libraries (kitlog,klog,logr,slog,zap).
- maintidx # Maintidx measures the maintainability index of each function.
- makezero # Find slice declarations with non-zero initial length.
- mirror # Reports wrong mirror patterns of bytes/strings usage.
- misspell # Finds commonly misspelled English words.
- mnd # An analyzer to detect magic numbers.
- musttag # Enforce field tags in (un)marshaled structs.
- nakedret # Checks that functions with naked returns are not longer than a maximum size (can be zero).
- nestif # Reports deeply nested if statements.
- nilerr # Find the code that returns nil even if it checks that the error is not nil.
- nilnesserr # Reports constructs that checks for err != nil, but returns a different nil value error.
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
- nlreturn # Checks for a new line before return and branch statements to increase code clarity.
- nolintlint # Reports ill-formed or insufficient nolint directives.
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL.
- paralleltest # Detects missing usage of t.Parallel() method in your Go test.
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative.
- prealloc # Find slice declarations that could potentially be pre-allocated.
- predeclared # Find code that shadows one of Go's predeclared identifiers.
- promlinter # Check Prometheus metrics naming via promlint.
- protogetter # Reports direct reads from proto message fields when getters should be used.
- reassign # Checks that package variables are not reassigned.
- recvcheck # Checks for receiver type consistency.
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
- rowserrcheck # Checks whether Rows.Err of rows is checked successfully.
- sloglint # Ensure consistent code style when using log/slog.
- spancheck # Checks for mistakes with OpenTelemetry/Census spans.
- sqlclosecheck # Checks that sql.Rows, sql.Stmt, sqlx.NamedStmt, pgx.Query are closed.
- tagalign # Check that struct tags are well aligned.
- tagliatelle # Checks the struct tags.
- testableexamples # Linter checks if examples are testable (have an expected output).
- testifylint # Checks usage of github.com/stretchr/testify.
- testpackage # Linter that makes you use a separate _test package.
- thelper # Thelper detects tests helpers which is not start with t.Helper() method.
- tparallel # Tparallel detects inappropriate usage of t.Parallel() method in your Go test codes.
- unconvert # Remove unnecessary type conversions.
- unparam # Reports unused function parameters.
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library.
- usetesting # Reports uses of functions with replacement inside the testing package.
- wastedassign # Finds wasted assignment statements.
- whitespace # Whitespace is a linter that checks for unnecessary newlines at the start and end of functions, if, for, etc.
- wsl_v5 # Add or remove empty lines.
- zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg`.

exclusions:
rules:
- path: _test\.go
linters:
- cyclop # Checks function and package cyclomatic complexity.
- errcheck # Errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases.
- dupl # Detects duplicate fragments of code.
- funlen # Checks for long functions.
- gosec # Inspects source code for security problems.
- nestif # Reports deeply nested if statements.
- paralleltest # Detects missing usage of t.Parallel() method in your Go test.
- testpackage # Linter that makes you use a separate _test package.

settings:
cyclop:
# The maximal code complexity to report.
# Default: 10
max-complexity: 15
depguard:
rules:
main:
deny:
- pkg: "math/rand$"
desc: use math/rand/v2
- pkg: "github.com/sirupsen/logrus"
desc: no longer maintained
- pkg: "github.com/pkg/errors"
desc: Should be replaced by standard lib errors package
forbidigo:
# Forbid the following identifiers (list of regexp).
# Default: ["^(fmt\\.Print(|f|ln)|print|println)$"]
forbid:
- pattern: ^(fmt\\.Print(|f|ln)|print|println)$
- pattern: http\.NotFound.*
msg: return RFC 7807 problem details instead
- pattern: http\.Error.*
msg: return RFC 7807 problem details instead
funlen:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
# Default: 60
lines: 100
nestif:
# Minimal complexity of if statements to report.
# Default: 5
min-complexity: 6

formatters:
enable:
- gofmt # checks if the code is formatted according to 'gofmt' command
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
- golines # checks if code is formatted, and fixes long lines.

output:
formats:
text:
print-linter-name: true
print-issued-lines: true
colors: true

run:
# Timeout for analysis.
timeout: 5m

# Modules download mode (do not modify go.mod)
modules-download-mode: readonly
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ Give clarity in your bank expenses.

## Run

Import csv into sqlite DB.
Generate report from csv

```shell
go mod init
go mod tidy

go test ./... -v

go build -o expenses cmd/main.go

./expenses \
--csv=testdata/transactions.csv \
--yaml=testdata/transactiontypes.yaml \
--period=3y


```

## Development setup

```shell

go mod init
go mod tidy

go test ./... -v
golangci-lint run # Install golangci-lint from https://golangci-lint.run/usage/install

go build -ldflags="-X 'main.Version=$(git describe --tags --always)'" -o expenses cmd/main.go

```

## CSV Columns
Expand All @@ -40,7 +48,8 @@ Boekdatum Rekeningnummer Bedrag Debet / Credit Naam tegenrekening Tegenrekening
### v2.1

- [x] report: all untyped transactions
- [ ] CLI
- [x] CLI
- [x] linting

### v2.2*

Expand Down
31 changes: 26 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Package main creates the CLI.
package main

import (
"flag"
"fmt"
"log"
"os"

Expand All @@ -11,15 +11,36 @@ import (
"github.com/dirc/expenses/internal/reports"
)

var Version = "dev" // Default value, overwrite during build.

func main() {
// Define CLI flags
csvPath := flag.String("csv", "", "Path to the CSV file containing transactions")
yamlPath := flag.String("yaml", "", "Path to the YAML file containing transaction types")
reportPeriod := flag.String("period", "", "Report period (e.g., '3m' for 3 months, '4y' for 4 years)")
versionFlag := flag.Bool("version", false, "Print the version and exit")
csvPath := flag.String(
"csv",
"",
"Path to the CSV file containing transactions",
)
yamlPath := flag.String(
"yaml",
"",
"Path to the YAML file containing transaction types",
)
reportPeriod := flag.String(
"period",
"",
"Report period (e.g., '3m' for 3 months, '4y' for 4 years)",
)

// Parse flags
flag.Parse()

// Handle version flag
if *versionFlag {
log.Printf("expenses version: %s\n", Version)
os.Exit(0)
}

// Validate required flags
if *csvPath == "" || *yamlPath == "" || *reportPeriod == "" {
flag.Usage()
Expand Down Expand Up @@ -48,6 +69,6 @@ func main() {
}

// Print the report
reports.PrintPeriodicReport(report, fmt.Sprintf("Report for %s", *reportPeriod))
reports.PrintPeriodicReport(report, "Report for "+*reportPeriod)
reports.GenerateUntypedReport(transactions)
}
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
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/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package config contains configuration methods.
package config
15 changes: 12 additions & 3 deletions internal/etl/extract/extract.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package extract extracts data into memory.
package extract

import (
"encoding/csv"
"log"
"os"
"strconv"
"strings"
Expand All @@ -10,15 +12,21 @@ import (
"github.com/dirc/expenses/internal/models"
)

// ReadCSV read csv file containing transactions.
func ReadCSV(filePath string) ([]models.Transaction, error) {
file, err := os.Open(filePath)
file, err := os.Open(filePath) // #nosec G304
if err != nil {
return nil, err
}
defer file.Close()

defer func() {
if err := file.Close(); err != nil {
log.Printf("Failed to close file: %v", err)
}
}()

reader := csv.NewReader(file)
reader.Comma = ',' // CSV is comma-separated
reader.Comma = ','

// Read and discard header
_, err = reader.Read()
Expand All @@ -27,6 +35,7 @@ func ReadCSV(filePath string) ([]models.Transaction, error) {
}

var transactions []models.Transaction

for {
record, err := reader.Read()
if err != nil {
Expand Down
Loading