Skip to content

Commit

Permalink
Adding time + amount flags to commit command.
Browse files Browse the repository at this point in the history
  • Loading branch information
marstr committed Jan 28, 2019
1 parent f2fe86f commit d41ef19
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package cmd

import (
"context"
"errors"
"fmt"
"path/filepath"
"time"

Expand All @@ -29,6 +31,13 @@ import (
"github.com/marstr/baronial/internal/index"
)

const (
amountFlag = "amount"
amountShorthand = "a"
amountDefault = "USD 0.00"
amountUsage = "The magnitude of the transaction that should be displayed in logs."
)

const (
merchantFlag = "merchant"
merchantShorthand = "m"
Expand All @@ -55,11 +64,32 @@ var commitConfig = viper.New()
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Create a transaction with the current impacts in the index.",
Args: cobra.NoArgs,
Args: func(cmd *cobra.Command, args []string) error {
if commitConfig.IsSet(timeFlag) {
if currentValue := commitConfig.GetString(timeFlag); currentValue == timeDefault {
commitConfig.SetDefault(timeFlag, time.Now())
}
}

if finalTimeValue := commitConfig.GetTime(timeFlag); finalTimeValue.Equal(time.Time{}) {
return fmt.Errorf("unable to parse time from %q", commitConfig.GetString(timeFlag))
}

if !commitConfig.IsSet(amountFlag) {
return errors.New(`missing flag "` + amountFlag + `"`)
}

return cobra.NoArgs(cmd, args)
},
Run: func(_ *cobra.Command, _ []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

amount, err := envelopes.ParseBalance(commitConfig.GetString(amountFlag))
if err != nil {
logrus.Fatal(err)
}

targetDir, err := index.RootDirectory(".")
if err != nil {
logrus.Fatal(err)
Expand Down Expand Up @@ -91,6 +121,7 @@ var commitCmd = &cobra.Command{
}

currentTransaction := envelopes.Transaction{
Amount: amount,
Merchant: commitConfig.GetString(merchantFlag),
Comment: commitConfig.GetString(commentFlag),
State: &envelopes.State{
Expand All @@ -116,11 +147,10 @@ var commitCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(commitCmd)

commitConfig.SetDefault(timeFlag, time.Now().String())

commitCmd.PersistentFlags().StringP(merchantFlag, merchantShorthand, merchantDefault, merchantUsage)
commitCmd.PersistentFlags().StringP(commentFlag, commentShorthand, commentDefault, commentUsage)
commitCmd.PersistentFlags().StringP(timeFlag, timeShorthand, timeDefault, timeUsage)
commitCmd.PersistentFlags().StringP(amountFlag, amountShorthand, amountDefault, amountUsage)

err := commitConfig.BindPFlags(commitCmd.PersistentFlags())
if err != nil {
Expand Down

0 comments on commit d41ef19

Please sign in to comment.