208 lines
3.8 KiB
Go
208 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Expense struct {
|
|
Id int `json:"id"`
|
|
Desc string `json:"string"`
|
|
Amount float64 `json:"amount"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func add(fileName string, desc string, amount float64) error {
|
|
exp, err := loadExpenses(fileName)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var e Expense
|
|
e.Desc = desc
|
|
e.Amount = amount
|
|
e.Date = time.Now()
|
|
if len(exp) == 0 {
|
|
e.Id = 1
|
|
|
|
} else {
|
|
e.Id = 1 + exp[len(exp)-1].Id
|
|
}
|
|
|
|
exp = append(exp, e)
|
|
return saveExpenses(exp, fileName)
|
|
}
|
|
|
|
func list(fileName string) error {
|
|
fmt.Println("ID Date Description Amount")
|
|
exp, err := loadExpenses(fileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, i := range exp {
|
|
y, m, d := i.Date.Date()
|
|
fmt.Printf("%d %d-%s-%d %s $%.2f\n", i.Id, y, m, d, i.Desc, math.Round(float64(i.Amount)*100)/100)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
$ expense-tracker delete --id 2
|
|
# Expense deleted successfully
|
|
|
|
takes Id and loops over loaded tasks to remove object
|
|
*/
|
|
func delete(fileName string, idx int) error {
|
|
exp, err := loadExpenses(fileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for i, value := range exp {
|
|
if value.Id == idx {
|
|
exp = slices.Delete(exp, i, i+1)
|
|
}
|
|
}
|
|
|
|
var count int = 1
|
|
for i := 0; i < len(exp); i++ {
|
|
exp[i].Id = count
|
|
count++
|
|
}
|
|
|
|
return saveExpenses(exp, fileName)
|
|
}
|
|
|
|
/*
|
|
|
|
$ expense-tracker update --id 2 --description "new desc"
|
|
# Expense updated successfully
|
|
|
|
$ expense-tracker update --id 2 --amount 10
|
|
# Expense updated successfully
|
|
|
|
$ expense-tracker update --id 2 --description "new desc" --amount 10
|
|
# Expense updated successfully
|
|
|
|
takes in an id, a string for desc (if "" then it is blank and dont update), a new amount (if -1 then it is blank and dont update)
|
|
*/
|
|
|
|
func update() {
|
|
|
|
}
|
|
|
|
/*
|
|
$ expense-tracker summary
|
|
# Total expenses: $30
|
|
|
|
$ expense-tracker summary
|
|
# Total expenses: $20
|
|
|
|
$ expense-tracker summary --month 8
|
|
# Total expenses for August: $20
|
|
*/
|
|
func summary() {
|
|
/*
|
|
|
|
1. Loads expenses to slice
|
|
2. Count all amounts from each expense
|
|
3. print expenses
|
|
3.5 If month passed in then print all expenses for that month and only count for those months
|
|
*/
|
|
|
|
}
|
|
|
|
// Saves expenses table to a json file <expenses.json>
|
|
func saveExpenses(exp []Expense, fileName string) error {
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", " ")
|
|
encoder.Encode(exp)
|
|
return nil
|
|
}
|
|
|
|
func loadExpenses(fileName string) ([]Expense, error) {
|
|
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
|
|
return []Expense{}, nil
|
|
}
|
|
file, err := os.Open(fileName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var expenses []Expense
|
|
decoder := json.NewDecoder(file)
|
|
decoder.DisallowUnknownFields()
|
|
|
|
if err := decoder.Decode(&expenses); err != nil {
|
|
return nil, err
|
|
}
|
|
return expenses, err
|
|
|
|
}
|
|
|
|
func main() {
|
|
args := os.Args[1:]
|
|
|
|
var fileName string = "expenses.json"
|
|
|
|
if args[0] == "add" {
|
|
var amount float64
|
|
var desc strings.Builder
|
|
for i := range args {
|
|
if args[i] == "--description" {
|
|
if len(args) > i {
|
|
for j := i + 1; j < len(args); j++ {
|
|
if strings.HasPrefix(args[j], "--") {
|
|
break
|
|
}
|
|
desc.WriteString(args[j])
|
|
desc.WriteString(" ")
|
|
}
|
|
}
|
|
}
|
|
if args[i] == "--amount" {
|
|
if len(args) > i {
|
|
a, err := strconv.ParseFloat(args[i+1], 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
amount = a
|
|
}
|
|
}
|
|
}
|
|
add(fileName, desc.String(), amount)
|
|
} else if args[0] == "list" {
|
|
err := list(fileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else if args[0] == "delete" {
|
|
var id int
|
|
for i := range args {
|
|
if args[i] == "--id" {
|
|
if len(args) > i {
|
|
id, _ = strconv.Atoi(args[i+1])
|
|
}
|
|
}
|
|
}
|
|
|
|
ok := delete(fileName, id)
|
|
if ok != nil {
|
|
panic(ok)
|
|
}
|
|
}
|
|
}
|