summaryrefslogtreecommitdiff
path: root/cmd/cli/main.go
blob: 5cfd5e0a269be519de99c7beea00f5a6e38f62b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/lestrrat-go/jwx/v2/jwa"
	"github.com/lestrrat-go/jwx/v2/jwt"
	"github.com/urfave/cli/v2"
	"go.neonxp.dev/djson/internal/config"
)

func main() {
	app := &cli.App{
		Name: "djson cli tool",
		Commands: []*cli.Command{
			{
				Name: "token",
				Action: func(ctx *cli.Context) error {
					cfg, err := config.Parse(ctx.String("config"))
					if err != nil {
						return err
					}

					t := jwt.New()
					t.Set(jwt.SubjectKey, `djson`)
					t.Set(jwt.IssuedAtKey, time.Now())
					t.Set("allowed", []string{
						"a/b/c",
						"d/e/f",
					})
					signed, err := jwt.Sign(t, jwt.WithKey(
						jwa.KeyAlgorithmFrom(cfg.JWT.Algorithm),
						cfg.JWT.PrivateKey,
					))
					if err != nil {
						return err
					}
					fmt.Println(string(signed))
					return nil
				},
			},
		},
		Action: cli.ShowAppHelp,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "config",
				Usage: "Path to config file",
				Value: "/etc/djson/config.json",
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}