Quellcode durchsuchen

made credit/debit more flexible with account names

bmallred vor 10 Jahren
Ursprung
Commit
6b8a340eb0
3 geänderte Dateien mit 14 neuen und 18 gelöschten Zeilen
  1. 3 2
      commandCredit.go
  2. 3 2
      commandDebit.go
  3. 8 14
      helpers.go

+ 3 - 2
commandCredit.go

@ -28,9 +28,10 @@ func actionCredit(c *cli.Context) {
28 28
	ensureFileExists(pendingFile)
29 29
30 30
	args := c.Args()
31
	name, err := parseAccount(args)
31
	li := len(args) - 1
32
	name, err := parseAccount(args[:li])
32 33
	check(err)
33
	amount, err := parseValue(args, name)
34
	amount, err := parseAmount(args[li])
34 35
	check(err)
35 36
36 37
	a := Account{

+ 3 - 2
commandDebit.go

@ -28,9 +28,10 @@ func actionDebit(c *cli.Context) {
28 28
	ensureFileExists(pendingFile)
29 29
30 30
	args := c.Args()
31
	name, err := parseAccount(args)
31
	li := len(args) - 1
32
	name, err := parseAccount(args[:li])
32 33
	check(err)
33
	amount, err := parseValue(args, name)
34
	amount, err := parseAmount(args[li])
34 35
	check(err)
35 36
36 37
	a := Account{

+ 8 - 14
helpers.go

@ -42,13 +42,11 @@ func hasPendingTransaction(pendingFile string) bool {
42 42
43 43
// Parse a given string to extract an account name
44 44
func parseAccount(fields []string) (string, error) {
45
	for i := 0; i < len(fields); i++ {
46
		if strings.HasPrefix(fields[i], "#") {
47
			return fields[i], nil
48
		}
45
	if len(fields) > 0 {
46
		return strings.Join(fields, " "), nil
49 47
	}
50 48
51
	return fields[0], nil
49
	return "", errors.New("No account information found")
52 50
}
53 51
54 52
// Parse the given string to extract a proper date
@ -79,16 +77,12 @@ func parseDate(in string) (time.Time, error) {
79 77
}
80 78
81 79
// Parse the value from the arguments
80
func parseAmount(text string) (*big.Rat, error) {
81
	amount := new(big.Rat)
82 82
83
func parseValue(fields []string, account string) (*big.Rat, error) {
84
	r := new(big.Rat)
85
86
	for i := 0; i < len(fields); i++ {
87
		if fields[i] != account {
88
			r.SetString(fields[i])
89
			return r, nil
90
		}
83
	if r, _ := amount.SetString(text); r != nil {
84
		return amount, nil
91 85
	}
92 86
93
	return r, errors.New("No value found")
87
	return nil, errors.New("No value found")
94 88
}