mirror of
https://github.com/aclindsa/ofxgo.git
synced 2025-07-01 19:58:37 -04:00
Add command-line client
This commit is contained in:
80
cmd/ofx/main.go
Normal file
80
cmd/ofx/main.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var commands = []Command{
|
||||
getAccountsCommand,
|
||||
downloadCommand,
|
||||
ccDownloadCommand,
|
||||
invDownloadCommand,
|
||||
bankTransactionsCommand,
|
||||
ccTransactionsCommand,
|
||||
invTransactionsCommand,
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Println(`The ofxgo command-line client provides a simple interface to
|
||||
query, parse, and display financial data via the OFX specification.
|
||||
|
||||
Usage:
|
||||
ofx command [arguments]
|
||||
|
||||
The commands are:`)
|
||||
|
||||
maxlen := 0
|
||||
for _, cmd := range commands {
|
||||
if len(cmd.Name) > maxlen {
|
||||
maxlen = len(cmd.Name)
|
||||
}
|
||||
}
|
||||
formatString := " %-" + strconv.Itoa(maxlen) + "s %s\n"
|
||||
|
||||
for _, cmd := range commands {
|
||||
fmt.Printf(formatString, cmd.Name, cmd.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func runCmd(c *Command) {
|
||||
err := c.Flags.Parse(os.Args[2:])
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing flags: %s\n", err)
|
||||
c.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !c.CheckFlags() {
|
||||
fmt.Println()
|
||||
c.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
c.Do()
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Error: Please supply a sub-command. Usage:\n")
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
cmdName := os.Args[1]
|
||||
for _, cmd := range commands {
|
||||
if cmd.Name == cmdName {
|
||||
runCmd(&cmd)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
switch cmdName {
|
||||
case "-h", "-help", "--help", "help":
|
||||
usage()
|
||||
default:
|
||||
fmt.Println("Error: Invalid sub-command. Usage:")
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user