Something went wrong. Try again.
churn, baby, churn!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183package cmd
import ( "fmt" "os" "strconv"
"github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra"
"ysun.co/churn/internal/db" "ysun.co/churn/internal/lib" "ysun.co/churn/internal/ui" "ysun.co/churn/internal/ui/form" "ysun.co/churn/schema")
// churn account --var accountCmd = &cobra.Command{ Use: "account", Short: "Manage accounts (add, delete, edit)", Long: "Open an interactive TUI to manage account, add, delete, edit, etc.", Run: func(cmd *cobra.Command, args []string) { var err error db := db.Query()
accounts := make([]*schema.Account, 0) err = db.SelectFrom("account").Do(&accounts) if err != nil { fmt.Println("Failed to fetch accounts") os.Exit(1) }
products := make([]*schema.Product, 0) err = db.SelectFrom("product").Do(&products) if err != nil { fmt.Println("Failed to fetch products") os.Exit(1) }
columns := []table.Column{ {Title: "ID", Width: 5}, {Title: "Product", Width: 25}, {Title: "Limit", Width: 15}, {Title: "Opened", Width: 15}, {Title: "Closed", Width: 15}, }
rows := make([]table.Row, 0) for _, account := range accounts { product := schema.Product{} for _, p := range products { if p.ID == account.ProductID { product = *p break } }
var opened string if account.Opened.Valid { opened = account.Opened.Time.Format("2006-01-02") } else { opened = "N/A" }
var closed string if account.Closed.Valid { closed = account.Closed.Time.Format("2006-01-02") } else { closed = "N/A" }
rows = append(rows, table.Row{ strconv.Itoa(account.ID), product.ProductName, fmt.Sprintf("$%.2f", account.CL), opened, closed, }) }
t := table.New( table.WithColumns(columns), table.WithRows(rows), table.WithFocused(true), )
m := ui.ModelTable{Table: t} tea.NewProgram(m).Run() },}
// churn account add --var accountAddCmd = &cobra.Command{ Use: "add", Short: "Add an account", Long: "Add an account interactively, this will create a new account, and link it to the product.", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { account := &schema.Account{}
err := form.FormAccountAdd(account) if err != nil { fmt.Println("Failed to add account") os.Exit(1) }
db, _ := db.Connect() account.Add(db) },}
// churn account edit --var accountEditCmd = &cobra.Command{ Use: "edit <account id>", Short: "Edit an account", Long: "Edit an account by its id, this will update the account and its associated data.", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { q := db.Query() accounts := make([]*schema.Account, 0) err := q.SelectFrom("account").Do(&accounts) if err != nil { fmt.Println("Failed to fetch accounts") os.Exit(1) }
id, _ := strconv.Atoi(args[0]) account := &schema.Account{} for _, a := range accounts { if a.ID == id { account = a break } }
err = form.FormAccountAdd(account) if err != nil { fmt.Println("Failed to edit account") os.Exit(1) }
conn, _ := db.Connect() account.Update(conn) },}
// churn account delete --var forceAccountDeletion boolvar accountDeleteCmd = &cobra.Command{ Use: "delete <account id>", Short: "Delete an account", // by account id Long: "Delete an account by its id, this will delete the account and all its associated data.", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { if !forceAccountDeletion { fmt.Println("This will delete the account and all its associated data.") confirm := lib.Confirm() if !confirm { return } }
id, _ := strconv.Atoi(args[0]) account := &schema.Account{ ID: id, }
db, _ := db.Connect() account.Delete(db) },}
func init() { accountDeleteCmd.Flags().BoolVarP(&forceAccountDeletion, "force", "f", false, "Force delete the account and all its associated data")
rootCmd.AddCommand(accountCmd) accountCmd.AddCommand(accountAddCmd) accountCmd.AddCommand(accountEditCmd) accountCmd.AddCommand(accountDeleteCmd)}