Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (106 loc) · 2.64 KB
/
Copy pathmain.go
File metadata and controls
126 lines (106 loc) · 2.64 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/ldez/ghforms/internal"
"github.com/ldez/ghforms/internal/form"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v3"
"gitlab.com/greyxor/slogor"
)
//go:generate npm run build:css
var version = "dev"
func main() {
opts := []slogor.OptionFn{
slogor.SetLevel(slog.LevelInfo),
}
if !isatty.IsTerminal(os.Stdout.Fd()) {
opts = append(opts, slogor.DisableColor())
}
slog.SetDefault(slog.New(slogor.NewHandler(os.Stdout, opts...)))
app := createRootCommand()
app.Version = version
cli.VersionPrinter = func(cmd *cli.Command) {
fmt.Printf("%s version %s %s/%s\n", app.Name, cmd.Version, runtime.GOOS, runtime.GOARCH)
}
err := app.Run(context.Background(), os.Args)
if err != nil {
slog.Error("Fatal error", slog.Any("error", err))
os.Exit(1)
}
}
func createRootCommand() *cli.Command {
return &cli.Command{
Name: "ghforms",
Usage: "GitHub Forms Live Preview and Validation (Issues and Discussions)",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Usage: "HTTP listen address",
Value: ":8080",
Local: true,
},
&cli.StringFlag{
Name: "dir",
Usage: "Path to the forms directory",
Value: ".github/ISSUE_TEMPLATE",
Local: true,
},
},
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
if cmd.NArg() > 0 && cmd.Command(cmd.Args().First()) == nil {
return ctx, errors.New("unknown command")
}
return ctx, nil
},
Action: func(ctx context.Context, cmd *cli.Command) error {
return internal.Run(cmd.String("addr"), cmd.String("dir"))
},
Commands: []*cli.Command{
createVerifyCommand(),
},
}
}
func createVerifyCommand() *cli.Command {
return &cli.Command{
Name: "verify",
Usage: "Verify the forms",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Usage: "Path to the issue templates directory",
Value: ".github",
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
loader, err := form.New()
if err != nil {
return err
}
return filepath.WalkDir(cmd.String("dir"), func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.IsDir() {
return nil
}
if !strings.EqualFold(entry.Name(), "ISSUE_TEMPLATE") && !strings.EqualFold(entry.Name(), "DISCUSSION_TEMPLATE") {
return nil
}
slog.Info("Verifying forms", slog.String("dir", path))
_, err = loader.Load(path)
if err != nil {
return err
}
return nil
})
},
}
}