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
|
package cli
import (
"context"
"flag"
"fmt"
"os"
"codeberg.org/snonux/goprecords/internal/authkeys"
)
func runCreateClientKey(hostname string, args []string) error {
if hostname == "" {
return fmt.Errorf("create-client-key: hostname required")
}
fs := flag.NewFlagSet("create-client-key", flag.ExitOnError)
fs.SetOutput(os.Stderr)
statsDir := fs.String("stats-dir", "", "Uptimed stats directory (sets default auth-db path)")
authDB := fs.String("auth-db", "", "SQLite file for upload API keys (default: <stats-dir>/goprecords-auth.db)")
if err := fs.Parse(args); err != nil {
return err
}
authPath := *authDB
if authPath == "" {
if *statsDir == "" {
fmt.Fprintln(os.Stderr, "create-client-key: need -stats-dir or -auth-db")
fs.Usage()
return fmt.Errorf("missing -stats-dir or -auth-db")
}
authPath = authkeys.DefaultPath(*statsDir)
}
ctx := context.Background()
store, err := authkeys.OpenStore(ctx, authPath)
if err != nil {
return fmt.Errorf("open auth db: %w", err)
}
defer store.Close()
if err := store.EnsureSchema(ctx); err != nil {
return fmt.Errorf("schema: %w", err)
}
token, err := store.CreateKey(ctx, hostname)
if err != nil {
return fmt.Errorf("create key: %w", err)
}
fmt.Println(token)
return nil
}
|