This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (53 loc) · 1.6 KB
/
main.go
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
package main
import (
"fmt"
"log/slog"
"os"
"strings"
"github.com/alecthomas/kong"
"github.com/hasura/ndc-rest-schema/command"
"github.com/hasura/ndc-rest-schema/version"
"github.com/lmittmann/tint"
)
var cli struct {
LogLevel string `help:"Log level." enum:"debug,info,warn,error" default:"info"`
Convert command.ConvertCommandArguments `cmd:"" help:"Convert API spec to NDC schema. For example:\n ndc-rest-schema convert -f petstore.yaml -o petstore.json"`
Json2Yaml command.Json2YamlCommandArguments `cmd:"" name:"json2yaml" help:"Convert JSON file to YAML. For example:\n ndc-rest-schema json2yaml -f petstore.json -o petstore.yaml"`
Version struct{} `cmd:"" help:"Print the CLI version."`
}
func main() {
cmd := kong.Parse(&cli, kong.UsageOnError())
logger, err := initLogger(cli.LogLevel)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
return
}
switch cmd.Command() {
case "convert":
err = command.CommandConvertToNDCSchema(&cli.Convert, logger)
case "json2yaml":
err = command.Json2Yaml(&cli.Json2Yaml, logger)
case "version":
_, _ = fmt.Print(version.BuildVersion)
default:
logger.Error(fmt.Sprintf("unknown command <%s>", cmd.Command()))
os.Exit(1)
}
if err != nil {
os.Exit(1)
}
}
func initLogger(logLevel string) (*slog.Logger, error) {
var level slog.Level
err := level.UnmarshalText([]byte(strings.ToUpper(logLevel)))
if err != nil {
return nil, err
}
logger := slog.New(tint.NewHandler(os.Stderr, &tint.Options{
Level: level,
TimeFormat: "15:04",
}))
slog.SetDefault(logger)
return logger, nil
}