Project Amber CLI client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB

package main
import (
"flag"
"fmt"
"os"
"git.tdem.in/tdemin/amber_cli/internal/tasks"
_ "github.com/genjidb/genji/sql/driver"
)
var cmds = []cliCommand{
loginCmd{},
addCmd{},
removeCmd{},
updateCmd{},
listCmd{},
toggleCmd{},
helpCmd{},
versionCmd{},
}
func runCLI() {
// parse command-line flags and run the necessary routine
if flag.NArg() < 1 {
printUsage()
return
}
for _, cmd := range cmds {
if name := cmd.Name(); name == flag.Arg(0) {
if err := cmd.Run(flag.Args()[1:]); err != nil {
fmt.Printf("%s: %v\n", name, err)
os.Exit(1)
}
return
}
}
printUsage()
}
var db *tasks.DB
var hc *httpClient
var cfg *Config
// TODO: offline mode
var isOffline bool
func main() {
flag.Parse()
var err error
appPath := DetermineAppPath()
if _, err := os.Stat(appPath); os.IsNotExist(err) {
fmt.Printf("%s does not exist, creating it\n", appPath)
if mkdirerr := os.Mkdir(appPath, 0700); mkdirerr != nil {
fmt.Printf("cannot create app path: %v\n", mkdirerr)
os.Exit(1)
}
}
cfg, err = ReadConfig(GetConfigPath())
// if the config does not exist this just means it's to be written later
if err != nil && !os.IsNotExist(err) {
fmt.Printf("cannot read config: %v\n", err)
os.Exit(1)
}
db, err = tasks.InitDatabase(appPath + "/tasks.db")
if err != nil {
fmt.Printf("cannot open database: %v\n", err)
os.Exit(1)
}
defer db.Close()
hc = newHTTPClient(cfg.Server)
hc.Token = cfg.Token
if !isOffline {
doMerge() // making things slow since 2020
}
runCLI()
}