Skip to content

Commit

Permalink
Implemented more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 9, 2023
1 parent 6bc9769 commit 79e2c79
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 56 deletions.
17 changes: 1 addition & 16 deletions cli/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package commands

import (
"fmt"

kongcompletion "github.com/jotaen/kong-completion"
)

Expand All @@ -33,24 +31,11 @@ import (
cli.AddCommand("import-transactions", &importTransactions{})
}*/

type RmCmd struct {
Force bool `help:"Force removal."`
Recursive bool `help:"Recursively remove files."`

Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
}

func (r *RmCmd) Run() error {
fmt.Println("rm", r.Paths)
return nil
}

var CLI struct {
Debug bool `help:"Enable debug mode."`

Rm RmCmd `cmd:"" help:"Remove files."`
Security SecurityCmd `cmd:"" help:"Security commands."`
Portfolio PortfolioCmd `cmd:"" help:"Portfolio commands."`

Completion kongcompletion.Completion `cmd:"" help:"Outputs shell code for initialising tab completions"`
Completion kongcompletion.Completion `cmd:"" help:"Outputs shell code for initializing tab completions" hidden:""`
}
106 changes: 73 additions & 33 deletions cli/commands/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ import (
"strings"

"github.com/fatih/color"
kongcompletion "github.com/jotaen/kong-completion"
"github.com/oxisto/money-gopher/cli"
portfoliov1 "github.com/oxisto/money-gopher/gen"
"github.com/oxisto/money-gopher/gen/portfoliov1connect"
"github.com/posener/complete"
"google.golang.org/protobuf/types/known/timestamppb"

"connectrpc.com/connect"
"google.golang.org/protobuf/types/known/timestamppb"
)

type PortfolioCmd struct {
List ListPortfolioCmd `cmd:"" help:"Lists all portfolios."`
Create CreatePortfolioCmd `cmd:"" help:"Creates a new portfolio."`
List ListPortfolioCmd `cmd:"" help:"Lists all portfolios."`
Show ShowPortfolioCmd `cmd:"" help:"Shows details about one portfolio."`
ImportTransactions ImportTransactionsCmd `cmd:"" help:"Imports transactions from CSV."`
}

type ListPortfolioCmd struct{}
Expand Down Expand Up @@ -89,9 +94,12 @@ func (l *ListPortfolioCmd) Run(s *cli.Session) error {
return nil
}

type createPortfolio struct{}
type CreatePortfolioCmd struct {
Name string `help:"The identifier of the portfolio, e.g. mybank/myportfolio" required:""`
DisplayName string `help:"The display name of the portfolio"`
}

func (cmd *createPortfolio) Exec(s *cli.Session, args ...string) {
func (cmd *CreatePortfolioCmd) Run(s *cli.Session) error {
client := portfoliov1connect.NewPortfolioServiceClient(
http.DefaultClient, "http://localhost:8080",
connect.WithHTTPGet(),
Expand All @@ -100,64 +108,68 @@ func (cmd *createPortfolio) Exec(s *cli.Session, args ...string) {
context.Background(),
connect.NewRequest(&portfoliov1.CreatePortfolioRequest{
Portfolio: &portfoliov1.Portfolio{
Name: args[1],
DisplayName: args[2],
Name: cmd.Name,
DisplayName: cmd.DisplayName,
},
}),
)
if err != nil {
log.Println(err)
} else {
log.Println(res)
return err
}
}

func greenOrRed(f float32) string {
if f < 0 {
return color.RedString("%.02f", f)
} else {
return color.GreenString("%.02f", f)
}
log.Println(res.Msg)
return nil
}

type portfolioSnapshot struct{}
type ShowPortfolioCmd struct {
PortfolioName string `help:"The identifier of the portfolio, e.g. mybank/myportfolio" required:"" predictor:"portfolio"`
}

// Exec implements [cli.Command]
func (cmd *portfolioSnapshot) Exec(s *cli.Session, args ...string) {
func (cmd *ShowPortfolioCmd) Run(s *cli.Session) error {
client := portfoliov1connect.NewPortfolioServiceClient(
http.DefaultClient, "http://localhost:8080",
connect.WithHTTPGet(),
)
res, err := client.GetPortfolioSnapshot(
context.Background(),
connect.NewRequest(&portfoliov1.GetPortfolioSnapshotRequest{
PortfolioName: "My Portfolio",
PortfolioName: cmd.PortfolioName,
Time: timestamppb.Now(),
}),
)
if err != nil {
log.Println(err)
return err
}

log.Println(res.Msg)
return nil
}

func greenOrRed(f float32) string {
if f < 0 {
return color.RedString("%.02f", f)
} else {
log.Println(res.Msg)
return color.GreenString("%.02f", f)
}
}

type importTransactions struct{}
type ImportTransactionsCmd struct {
PortfolioName string `required:"" predictor:"portfolio"`
CsvFile string `arg:"" help:"The path to the CSV file to import"`
}

// Exec implements [cli.Command]
func (cmd *importTransactions) Exec(s *cli.Session, args ...string) {
func (cmd *ImportTransactionsCmd) Run(s *cli.Session) error {
// Read from args[1]
f, err := os.Open(args[1])
f, err := os.Open(cmd.CsvFile)
if err != nil {
log.Println(err)
return
return err
}
defer f.Close()

b, err := io.ReadAll(f)
if err != nil {
log.Println(err)
return
return err
}

client := portfoliov1connect.NewPortfolioServiceClient(
Expand All @@ -167,13 +179,41 @@ func (cmd *importTransactions) Exec(s *cli.Session, args ...string) {
res, err := client.ImportTransactions(
context.Background(),
connect.NewRequest(&portfoliov1.ImportTransactionsRequest{
PortfolioName: args[0],
PortfolioName: cmd.PortfolioName,
FromCsv: string(b),
}),
)
if err != nil {
log.Println(err)
} else {
log.Println(res.Msg)
return err
}

log.Println(res.Msg)
return nil
}

type PortfolioPredictor struct{}

func (p *PortfolioPredictor) Predict(complete.Args) (names []string) {
client := portfoliov1connect.NewPortfolioServiceClient(
http.DefaultClient, "http://localhost:8080",
connect.WithHTTPGet(),
)
res, err := client.ListPortfolios(
context.Background(),
connect.NewRequest(&portfoliov1.ListPortfoliosRequest{}),
)
if err != nil {
return nil
}

for _, p := range res.Msg.Portfolios {
names = append(names, p.Name)
}

return
}

var PredictPortfolios = kongcompletion.WithPredictor(
"portfolio",
&PortfolioPredictor{},
)
8 changes: 1 addition & 7 deletions cmd/mgo/mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
kongcompletion "github.com/jotaen/kong-completion"
"github.com/oxisto/money-gopher/cli"
"github.com/oxisto/money-gopher/cli/commands"
"github.com/posener/complete"
)

func main() {
Expand All @@ -33,7 +32,7 @@ func main() {
kong.UsageOnError(),
)

kongcompletion.Register(parser, predictNames)
kongcompletion.Register(parser, commands.PredictPortfolios)

// Proceed as normal after kongplete.Complete.
ctx, err := parser.Parse(os.Args[1:])
Expand All @@ -42,8 +41,3 @@ func main() {
err = ctx.Run(&cli.Session{})
parser.FatalIfErrorf(err)
}

var predictNames = kongcompletion.WithPredictor(
"name",
complete.PredictSet("Ben", "Liz", "Mark", "Sarah"),
)
1 change: 1 addition & 0 deletions money-gopher.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"headlessui",
"heroicons",
"ISIN",
"kongcompletion",
"modernc",
"moneygopher",
"oxisto",
Expand Down

0 comments on commit 79e2c79

Please sign in to comment.