-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
76 lines (66 loc) · 2.41 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go/command"
"github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go/version"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var cli struct {
LogLevel string `help:"Log level." enum:"debug,info,warn,error,DEBUG,INFO,WARN,ERROR" env:"HASURA_PLUGIN_LOG_LEVEL" default:"info"`
New command.NewArguments `cmd:"" help:"Initialize an NDC connector boilerplate. For example:\n hasura-ndc-go new -n example -m github.com/foo/example"`
Update command.UpdateArguments `cmd:"" help:"Generate schema and implementation for the connector from functions."`
Generate struct {
Snapshots command.GenTestSnapshotArguments `cmd:"" help:"Generate test snapshots."`
} `cmd:"" help:"Generator helpers."`
Version struct{} `cmd:"" help:"Print the CLI version."`
}
func main() {
cmd := kong.Parse(&cli, kong.UsageOnError())
start := time.Now()
setupGlobalLogger(cli.LogLevel)
switch cmd.Command() {
case "new":
if cli.New.Version == "" {
cli.New.Version = version.BuildVersion
}
log.Info().
Str("name", cli.New.Name).
Str("module", cli.New.Module).
Str("output", cli.New.Output).
Str("version", cli.New.Version).
Msg("generating the NDC boilerplate...")
if err := command.GenerateNewProject(&cli.New, false); err != nil {
log.Fatal().Err(err).Msg("failed to generate new project")
}
log.Info().Str("exec_time", time.Since(start).Round(time.Second).String()).
Msg("generated successfully")
case "update":
command.UpdateConnectorSchema(cli.Update, start)
case "generate snapshots":
log.Info().
Str("endpoint", cli.Generate.Snapshots.Endpoint).
Str("path", cli.Generate.Snapshots.Schema).
Interface("dir", cli.Generate.Snapshots.Dir).
Msg("generating test snapshots...")
if err := command.GenTestSnapshots(&cli.Generate.Snapshots); err != nil {
log.Fatal().Err(err).Msg("failed to generate test snapshots")
}
case "version":
_, _ = fmt.Fprint(os.Stderr, version.BuildVersion)
default:
log.Fatal().Msgf("unknown command <%s>", cmd.Command())
}
}
func setupGlobalLogger(level string) {
logLevel, err := zerolog.ParseLevel(strings.ToLower(level))
if err != nil {
log.Fatal().Err(err).Msgf("failed to parse log level: %s", level)
}
zerolog.SetGlobalLevel(logLevel)
log.Logger = log.Level(logLevel).Output(zerolog.ConsoleWriter{Out: os.Stderr})
}