Skip to content

Commit

Permalink
Merge pull request #184 from darcys22/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
darcys22 authored Jan 13, 2022
2 parents 6984a11 + 7ea85f3 commit 2a8b56d
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 30 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
run: sudo apt-get install devscripts
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Push PPA
shell: 'script -q -e -c "bash {0}"'
env:
Expand All @@ -39,7 +41,7 @@ jobs:
strategy:
fail-fast: true
matrix:
go-version: [1.14.x]
go-version: [1.15.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
environment: "PPA Signing Environment"
Expand All @@ -50,8 +52,13 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
path: go/src/github.com/darcys22/godbledger
- name: Push Github Release
env:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
GOPATH: /home/runner/work/godbledger/godbledger/go
run: |
cd go/src/github.com/darcys22/godbledger
make github
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ bazel-*

/tmp
*/**/*un~
*/**/*.test
*un~
.DS_Store
*/**/.DS_Store
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ endif
GDBL_DATA_DIR ?= $(DEFAULT_DATA_DIR)

GODIST = ./build/dist
GO ?= latest
#GO ?= latest
GO ?= go-1.15.x
GORUN = env GO111MODULE=on go run

xtarget = $(strip $(subst build-,,$@)) # e.g. 'build-linux-amd64' -> 'linux-amd64'
Expand Down Expand Up @@ -43,6 +44,9 @@ build-native:
lint:
$(GORUN) utils/ci.go lint

gofmt:
gofmt -l -s -w .

# our tests include an integration test which expects the local
# GOOS-based build output to be in the ./build/bin folder
test: build-native
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ go 1.15
require (
github.com/BurntSushi/toml v0.3.1
github.com/Songmu/retry v0.1.0
github.com/cespare/cp v1.1.1 // indirect
github.com/cespare/cp v1.1.1
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.1.0 // indirect
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7
Expand All @@ -16,6 +15,8 @@ require (
github.com/mattn/go-sqlite3 v1.14.7
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.17.0 // indirect
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.3.0
github.com/sirupsen/logrus v1.8.1
Expand All @@ -24,10 +25,11 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
golang.org/x/text v0.3.7
google.golang.org/genproto v0.0.0-20210611144927-798beca9d670 // indirect
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.4.0 // indirect
src.techknowlogick.com/xgo v1.4.1-0.20220106204436-795b29d5ed5b // indirect
)
56 changes: 43 additions & 13 deletions go.sum

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions internal/build/local.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,96 @@
package build

import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"
)

func compress(src string, buf io.Writer) error {
// tar > gzip > buf
zr := gzip.NewWriter(buf)
tw := tar.NewWriter(zr)

// is file a folder?
fi, err := os.Stat(src)
if err != nil {
return err
}
mode := fi.Mode()
if mode.IsRegular() {
// get header
header, err := tar.FileInfoHeader(fi, src)
if err != nil {
return err
}
// write header
if err := tw.WriteHeader(header); err != nil {
return err
}
// get content
data, err := os.Open(src)
if err != nil {
return err
}
if _, err := io.Copy(tw, data); err != nil {
return err
}
} else if mode.IsDir() { // folder

// walk through every file in the folder
filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
// generate tar header
header, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}

// must provide real name
// (see https://golang.org/src/archive/tar/common.go?#L626)
_, outFile := filepath.Split(file)
header.Name = outFile

// write header
if err := tw.WriteHeader(header); err != nil {
return err
}
// if not a dir, write file content
if !fi.IsDir() {
data, err := os.Open(file)
if err != nil {
return err
}
if _, err := io.Copy(tw, data); err != nil {
return err
}
}
return nil
})
} else {
return fmt.Errorf("error: file type not supported")
}

// produce tar
if err := tw.Close(); err != nil {
return err
}
// produce gzip
if err := zr.Close(); err != nil {
return err
}
//
return nil
}

// LocalAssets contains the local objects to be uploaded
func LocalAssets(path string) ([]string, error) {
if path == "" {
Expand Down Expand Up @@ -37,6 +118,23 @@ func LocalAssets(path string) ([]string, error) {
}

assets := make([]string, 0, len(files))
for _, f := range files {
if fi, _ := os.Stat(f); fi.IsDir() {
var buf bytes.Buffer
if err := compress(f, &buf); err != nil {
fmt.Println(err)
os.Exit(1)
}
// write file to disk
pre, outFile := filepath.Split(f)
outFile = outFile + ".tar.gz"
if err = ioutil.WriteFile(pre+outFile, buf.Bytes(), 0600); err != nil {
fmt.Println(err)
os.Exit(1)
}
assets = append(assets, pre+outFile)
}
}
for _, f := range files {

// Exclude directory.
Expand Down
11 changes: 9 additions & 2 deletions ledger-cli/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ var commandFile = &cli.Command{
Description: `
Loads a file in the ledger cli format
`,
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "currency",
Aliases: []string{"c"},
Value: "USD",
Usage: "Specify the currency that the ledger file will be in, default to USD",
},
},
Action: func(ctx *cli.Context) error {
err, cfg := cmd.MakeConfig(ctx)
if err != nil {
Expand All @@ -60,7 +67,7 @@ var commandFile = &cli.Command{
return fmt.Errorf("Could not read file %s (%v)", ledgerFileName, err)
}

generalLedger, parseError := ParseLedger(ledgerFileReader)
generalLedger, parseError := ParseLedger(ledgerFileReader, ctx.String("currency"))
if parseError != nil {
return fmt.Errorf("Could not parse file (%v)", parseError)
}
Expand Down
10 changes: 4 additions & 6 deletions ledger-cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
// ParseLedger parses a ledger file and returns a list of Transactions.
//
// Transactions are sorted by date.
func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err error) {
parseLedger(ledgerReader, func(t *Transaction, e error) (stop bool) {
func ParseLedger(ledgerReader io.Reader, currency string) (generalLedger []*Transaction, err error) {
parseLedger(ledgerReader, currency, func(t *Transaction, e error) (stop bool) {
if e != nil {
err = e
stop = true
Expand All @@ -55,7 +55,7 @@ func ParseLedger(ledgerReader io.Reader) (generalLedger []*Transaction, err erro

var accountToAmountSpace = regexp.MustCompile(" {2,}|\t+")

func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error) (stop bool)) {
func parseLedger(ledgerReader io.Reader, currency string, callback func(t *Transaction, err error) (stop bool)) {
var trans *Transaction
scanner := bufio.NewScanner(ledgerReader)
var line string
Expand Down Expand Up @@ -122,7 +122,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error
}
lastIndex := len(nonEmptyWords) - 1
balErr, rationalNum := getBalance(strings.Trim(nonEmptyWords[lastIndex], whitespace))
accChange.Currency = "USD"
accChange.Currency = currency
if !balErr {
// Assuming no balance and whole line is account name
accChange.Name = strings.Join(nonEmptyWords, " ")
Expand All @@ -148,11 +148,9 @@ func getBalance(balance string) (bool, *big.Rat) {
rationalNum := new(big.Rat)
if strings.Contains(balance, "(") {
rationalNum.SetFloat64(calc.Solve(balance))
rationalNum.Mul(rationalNum, big.NewRat(100, 1))
return true, rationalNum
}
_, isValid := rationalNum.SetString(balance)
rationalNum.Mul(rationalNum, big.NewRat(100, 1))
return isValid, rationalNum
}

Expand Down
5 changes: 5 additions & 0 deletions reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ var (
Name: "json",
Usage: "output json instead of human-readable format",
}
formattingFlag = &cli.BoolFlag{
Name: "unformatted",
Aliases: []string{"u"},
Usage: "will display the dollar amounts without commas and dollar sign symbols",
}
)

func main() {
Expand Down
12 changes: 11 additions & 1 deletion reporter/transactionlisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"strconv"
"time"

"golang.org/x/text/language"
"golang.org/x/text/message"

"encoding/csv"
"encoding/json"

Expand Down Expand Up @@ -41,6 +44,7 @@ If you want to see all the transactions in the database, or export to CSV/JSON
Flags: []cli.Flag{
csvFlag,
jsonFlag,
formattingFlag,
},
Action: func(ctx *cli.Context) error {
//Check if keyfile path given and make sure it doesn't already exist.
Expand All @@ -56,6 +60,7 @@ If you want to see all the transactions in the database, or export to CSV/JSON
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "ID", "Account", "Description", "Currency", "Amount"})
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)

queryDateStart := time.Now().Add(time.Hour * 24 * 365 * -100)
queryDateEnd := time.Now().Add(time.Hour * 24 * 365 * 100)
Expand Down Expand Up @@ -115,7 +120,12 @@ If you want to see all the transactions in the database, or export to CSV/JSON
if err != nil {
return fmt.Errorf("Could not process the amount as a float (%v)", err)
}
t.Amount = fmt.Sprintf("%.2f", centsAmount/math.Pow(10, decimals))
if ctx.Bool("unformatted") {
t.Amount = fmt.Sprintf("%.2f", centsAmount/math.Pow(10, decimals))
} else {
p := message.NewPrinter(language.English)
t.Amount = p.Sprintf("$%.2f", centsAmount/math.Pow(10, decimals))
}
output.Data = append(output.Data, t)
table.Append([]string{t.Date, t.ID, t.Account, t.Description, t.Currency, t.Amount})
}
Expand Down
12 changes: 11 additions & 1 deletion reporter/trialbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"strconv"
"time"

"golang.org/x/text/language"
"golang.org/x/text/message"

"encoding/csv"
"encoding/json"

Expand Down Expand Up @@ -37,6 +40,7 @@ If you want to see all the transactions in the database, or export to CSV
Flags: []cli.Flag{
csvFlag,
jsonFlag,
formattingFlag,
},
Action: func(ctx *cli.Context) error {
//Check if keyfile path given and make sure it doesn't already exist.
Expand All @@ -54,6 +58,7 @@ If you want to see all the transactions in the database, or export to CSV
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Account", fmt.Sprintf("Balance at %s", queryDate.Format("02 January 2006"))})
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)

queryDB := `
SELECT split_accounts.account_id,
Expand Down Expand Up @@ -95,7 +100,12 @@ If you want to see all the transactions in the database, or export to CSV
if err != nil {
return fmt.Errorf("Could not process the amount as a float (%v)", err)
}
t.Amount = fmt.Sprintf("%.2f", centsAmount/math.Pow(10, decimals))
if ctx.Bool("unformatted") {
t.Amount = fmt.Sprintf("%.2f", centsAmount/math.Pow(10, decimals))
} else {
p := message.NewPrinter(language.English)
t.Amount = p.Sprintf("$%.2f", centsAmount/math.Pow(10, decimals))
}
tboutput.Data = append(tboutput.Data, t)
table.Append([]string{t.Account, t.Amount})
}
Expand Down
1 change: 0 additions & 1 deletion utils/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,6 @@ func doXgo(cmdline []string) {
xgoArgs := append(buildFlags(env), flag.Args()...)
xgoArgs = append(xgoArgs, []string{"--targets", *xtarget}...)
xgoArgs = append(xgoArgs, []string{"--dest", outDir}...)
xgoArgs = append(xgoArgs, []string{"--go", "go-1.15.x"}...)
xgoArgs = append(xgoArgs, "-v")
xgoArgs = append(xgoArgs, "./"+cmd) // relative package name (assumes we are inside GOPATH)
xgo := xgoTool(xgoArgs)
Expand Down
3 changes: 3 additions & 0 deletions utils/ledgercli-files/transaction-codes-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2009/10/29 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking
15 changes: 15 additions & 0 deletions utils/ledgercli-files/transaction-codes-2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2009/10/29 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking

2009/10/30 (DEP) Pay day!
Assets:Checking 20.00
Income

2009/10/30 (XFER) Panera Bread
Expenses:Food 4.50
Assets:Checking

2009/10/31 (559385768438A8D7) Panera Bread
Expenses:Food 4.50
Liabilities:Credit Card

0 comments on commit 2a8b56d

Please sign in to comment.