Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial try at CLI #198

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
- changed-files:
- any-glob-to-any-file: "finance/**/*"

"repl":
"cli":
- changed-files:
- any-glob-to-any-file: "repl/**/*"
- any-glob-to-any-file: "cli/**/*"

"ui":
- changed-files:
Expand Down
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"mode": "auto",
"program": "${workspaceFolder}/cmd/mgo",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"args": [
"list-portfolios"
]
},
{
"name": "Launch cmd/moneyd",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/moneyd",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
Expand Down
27 changes: 22 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
"tasks": [
{
"type": "go",
"label": "go: build cmd/mgo",
"label": "go: build cmd/moneyd",
"command": "build",
"args": [
"${workspaceFolder}/cmd/moneyd"
],
"problemMatcher": [
"$go"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "cd /Users/oxisto/Repositories/money-gopher; go build ${workspaceFolder}/cmd/moneyd"
},
{
"type": "shell",
"label": "go: install cmd/mgo",
"command": "go",
"args": [
"install",
"${workspaceFolder}/cmd/mgo"
],
"problemMatcher": [
Expand All @@ -15,20 +32,20 @@
"kind": "build",
"isDefault": true
},
"detail": "cd /Users/oxisto/Repositories/money-gopher; go build ${workspaceFolder}/cmd/mgo"
"detail": "cd /Users/oxisto/Repositories/money-gopher; go build ${workspaceFolder}/cmd/moneyd"
},
{
"type": "shell",
"label": "go: generate cmd/mgo",
"command": "go generate",
"label": "go: generate ./...",
"command": "go generate ./...",
"problemMatcher": [
"$go"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "cd /Users/oxisto/Repositories/money-gopher; go generate ${workspaceFolder}/cmd/mgo"
"detail": "cd /Users/oxisto/Repositories/money-gopher; go generate ./..."
}
]
}
58 changes: 58 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2023 Christian Banse
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is part of The Money Gopher.

// repl provides a simple Read-Eval-Print-Loop (REPL) to issue commands to an
// integrated client.
package cli

import (
"fmt"
"os"
)

var cmdMap map[string]Command = make(map[string]Command)

// AddCommand adds a command using the specific symbol.
func AddCommand(symbol string, cmd Command) {
cmdMap[symbol] = cmd
}

// Session holds all necessary information about the current CLI session.
type Session struct {
}

// Run runs our CLI command, based on the args. We keep it very simple for now
// without any extra package, so we just take the first arg and see if if
// matches any of our commands
func Run(args []string) {
var (
cmd Command
ok bool
s *Session
)

// Create a new session. TODO(oxisto): We do not yet have auth, but in the
// future we need to fetch a token here
s = new(Session)

// Try to look up command in our command map
cmd, ok = cmdMap[args[1]]
if ok {
cmd.Exec(s, os.Args[1:]...)
} else {
fmt.Print("Command not found.\n")
}
}
15 changes: 2 additions & 13 deletions repl/commands.go → cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,9 @@
//
// This file is part of The Money Gopher.

package repl
package cli

// Command is a command executed by the REPL.
type Command interface {
Exec(r *REPL, args ...string)
}

type quitCmd struct{}

// Exec implements [Command].
func (*quitCmd) Exec(r *REPL, args ...string) {
r.done = true
}

func init() {
AddCommand("quit", &quitCmd{})
Exec(s *Session, args ...string)
}
41 changes: 41 additions & 0 deletions cli/commands/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Christian Banse
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is part of The Money Gopher.

package commands

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

/*func init() {
cli.AddCommand("list-securities", &listSecuritiesCmd{})
cli.AddCommand("update-quote", &triggerQuoteUpdate{})
cli.AddCommand("update-all-quotes", &triggerQuoteUpdateAll{})

cli.AddCommand("create-portfolio", &createPortfolio{})
cli.AddCommand("list-portfolios", &listPortfolio{})
cli.AddCommand("portfolio-snapshot", &portfolioSnapshot{})
cli.AddCommand("import-transactions", &importTransactions{})
}*/

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

Security SecurityCmd `cmd:"" help:"Security commands."`
Portfolio PortfolioCmd `cmd:"" help:"Portfolio commands."`

Completion kongcompletion.Completion `cmd:"" help:"Outputs shell code for initializing tab completions" hidden:""`
}
Loading