From 79e2c798b859d3d090fe21fb41e46a399b7a812b Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Sat, 9 Dec 2023 13:37:27 +0100 Subject: [PATCH] Implemented more commands --- cli/commands/init.go | 17 +----- cli/commands/portfolio.go | 106 +++++++++++++++++++++++++----------- cmd/mgo/mgo.go | 8 +-- money-gopher.code-workspace | 1 + 4 files changed, 76 insertions(+), 56 deletions(-) diff --git a/cli/commands/init.go b/cli/commands/init.go index d4873511..f6634637 100644 --- a/cli/commands/init.go +++ b/cli/commands/init.go @@ -17,8 +17,6 @@ package commands import ( - "fmt" - kongcompletion "github.com/jotaen/kong-completion" ) @@ -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:""` } diff --git a/cli/commands/portfolio.go b/cli/commands/portfolio.go index d2600cf1..bef7b2e4 100644 --- a/cli/commands/portfolio.go +++ b/cli/commands/portfolio.go @@ -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{} @@ -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(), @@ -100,30 +108,24 @@ 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(), @@ -131,33 +133,43 @@ func (cmd *portfolioSnapshot) Exec(s *cli.Session, args ...string) { 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( @@ -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{}, +) diff --git a/cmd/mgo/mgo.go b/cmd/mgo/mgo.go index 7fd57f17..5c373731 100644 --- a/cmd/mgo/mgo.go +++ b/cmd/mgo/mgo.go @@ -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() { @@ -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:]) @@ -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"), -) diff --git a/money-gopher.code-workspace b/money-gopher.code-workspace index 8ec85b0b..b3b307dd 100644 --- a/money-gopher.code-workspace +++ b/money-gopher.code-workspace @@ -19,6 +19,7 @@ "headlessui", "heroicons", "ISIN", + "kongcompletion", "modernc", "moneygopher", "oxisto",