1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package main
- import (
- "flag"
- "os"
- "git.sr.ht/~blallo/papero/cli"
- "git.sr.ht/~blallo/papero/config"
- "github.com/withmandala/go-log"
- )
- var Session struct {
- Info cli.ProgramInfo
- Config *config.AccountData
- }
- var Log *log.Logger
- func init() {
- get := GetCmd{}
- set := SetCmd{}
- add := AddCmd{}
- // del := DelCmd{}
- Session.Info.Commands = cli.CommandMap{
- "get": get,
- "set": set,
- "add": add,
- // "del": del,
- }
- Session.Info.Name = "papero"
- flag.Usage = func() { cli.Usage(os.Stdout, Session.Info) }
- Log = log.New(os.Stdout)
- }
- func main() {
- var configPath string
- flag.StringVar(&Session.Info.Opts.ConfigPath, "config", "", "path to the configuration")
- flag.BoolVar(&Session.Info.Opts.Debug, "debug", false, "execute in debug mode")
- flag.StringVar(&Session.Info.Opts.ConfigStanza, "account", "", "specify which account to use")
- flag.Parse()
- args := flag.Args()
- if len(args) == 0 {
- cli.Usage(os.Stderr, Session.Info)
- os.Exit(1)
- }
- if Session.Info.Opts.Debug {
- Log.WithDebug()
- Log.Debugf(
- "opts -> config: %s\tdebug: %v\taccount: %s\n",
- Session.Info.Opts.ConfigPath,
- Session.Info.Opts.Debug,
- Session.Info.Opts.ConfigStanza,
- )
- }
- if Session.Info.Opts.ConfigPath == "" {
- var err error
- configPath, err = config.Find()
- if err != nil {
- Log.Fatal(err)
- }
- } else {
- configPath = Session.Info.Opts.ConfigPath
- }
- fullConfig, err := config.Parse(configPath)
- if err != nil {
- Log.Error(err)
- os.Exit(2)
- }
- var ok bool
- var account string
- if Session.Info.Opts.ConfigStanza != "" {
- account = Session.Info.Opts.ConfigStanza
- Session.Config, ok = fullConfig.Accounts[account]
- } else {
- account = fullConfig.Default
- Session.Config, ok = fullConfig.Accounts[account]
- }
- if !ok {
- Log.Error("Account not found in config:", account)
- os.Exit(2)
- }
- err = Session.Info.Commands[args[0]].Func(args)
- if err != nil {
- Log.Error(err)
- os.Exit(-1)
- }
- }
|