-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandDebit.go
More file actions
48 lines (41 loc) · 879 Bytes
/
commandDebit.go
File metadata and controls
48 lines (41 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
)
// Command line subcommand for "debit"
var commandDebit = cli.Command{
Name: "debit",
ShortName: "dr",
Usage: "",
Action: actionDebit,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file",
Value: "general.ledger",
Usage: "The ledger to store the debit",
},
},
}
// Add a debit to the pending transaction
func actionDebit(c *cli.Context) {
pendingFile := fmt.Sprintf(".%s", c.String("file"))
ensureFileExists(pendingFile)
args := c.Args()
li := len(args) - 1
name, err := parseAccount(args[:li])
check(err)
amount, err := parseAmount(args[li])
check(err)
a := Account{
Name: name,
Debit: true,
Amount: amount,
}
f, err := os.OpenFile(pendingFile, os.O_APPEND|os.O_WRONLY, 0666)
check(err)
defer f.Close()
_, err = f.WriteString(a.ToString())
check(err)
}