package main import ( "context" "crypto/rand" "encoding/base64" "github.com/alecthomas/kong" _ "github.com/alecthomas/kong" "github.com/thequux/qddns/db" "os" ) type AddToken struct { Token string `help:"The token to add access to"` Domain string `arg:"" help:"Domain to allow access to"` Note string `arg:"" help:"Comment to include in the DB"` } func (cmd *AddToken) Run() error { ctx := context.Background() if cmd.Token == "" { raw := make([]byte, 30) _, err := rand.Read(raw) if err != nil { return err } cmd.Token = base64.RawURLEncoding.EncodeToString(raw) } println("Token: ", cmd.Token) tx, err := db.Db.Begin(ctx) if err != nil { return err } _, err = tx.Exec(ctx, "INSERT INTO qddns_auth (token, domain, description) VALUES ($1, $2, $3)", cmd.Token, cmd.Domain, cmd.Note) if err != nil { return err } return nil } type Cli struct { Db string `help:"DB connection string (default: postgres:///pdns)"` Add AddToken `cmd:"" help:"Add a token"` } func (cli *Cli) Run() error { println("Root") return nil //db.Connect() } var cli Cli func main() { ctx := kong.Parse(&cli) //db.Connect(cli.Db) println(ctx.Command()) if err := ctx.Run(); err != nil { println(err.Error()) os.Exit(1) } }