-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
144 lines (135 loc) · 3.49 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/jippi/scm-engine/cmd"
"github.com/jippi/scm-engine/pkg/tui"
"github.com/urfave/cli/v2"
slogctx "github.com/veqryn/slog-context"
)
// nolint: gochecknoglobals
var (
commit = "unknown"
date = "unknown"
version = "dev"
)
func main() {
spew.Config.DisableMethods = true
app := &cli.App{
Name: "scm-engine",
Usage: "GitHub/GitLab automation",
Copyright: "Christian Winther",
EnableBashCompletion: true,
Suggest: true,
Version: fmt.Sprintf("%s (date: %s; commit: %s)", version, date, commit),
Authors: []*cli.Author{
{
Name: "Christian Winther",
Email: "scm-engine@jippi.dev",
},
},
Before: func(cCtx *cli.Context) error {
cCtx.Context = tui.NewContext(cCtx.Context, cCtx.App.Writer, cCtx.App.ErrWriter)
cCtx.Context = slogctx.With(cCtx.Context, "scm_engine_version", version)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: cmd.FlagConfigFile,
Usage: "Path to the scm-engine config file",
Value: ".scm-engine.yml",
TakesFile: true,
EnvVars: []string{
"SCM_ENGINE_CONFIG_FILE",
},
},
&cli.StringFlag{
Name: cmd.FlagAPIToken,
Usage: "GitHub/GitLab API token",
Required: true,
EnvVars: []string{
"SCM_ENGINE_TOKEN",
},
},
&cli.StringFlag{
Name: cmd.FlagSCMBaseURL,
Usage: "Base URL for the SCM instance",
Value: "https://gitlab.com/",
EnvVars: []string{
"GITLAB_BASEURL",
"CI_SERVER_URL",
},
},
&cli.BoolFlag{
Name: cmd.FlagDryRun,
Usage: "Dry run, don't actually _do_ actions, just print them",
},
},
Commands: []*cli.Command{
{
Name: "evaluate",
Usage: "Evaluate a Merge Request",
Args: true,
ArgsUsage: " [id, id, ...]",
Action: cmd.Evaluate,
Flags: []cli.Flag{
&cli.StringFlag{
Name: cmd.FlagSCMProject,
Usage: "GitLab project (example: 'gitlab-org/gitlab')",
Required: true,
EnvVars: []string{
"GITLAB_PROJECT",
"CI_PROJECT_PATH",
},
},
&cli.StringFlag{
Name: cmd.FlagMergeRequestID,
Usage: "The pull/merge to process, if not provided as a CLI flag",
Aliases: []string{
"merge-request-id", // GitLab naming
"pull-request-id", // GitHub naming
},
EnvVars: []string{
"CI_MERGE_REQUEST_IID", // GitLab CI
},
},
},
},
{
Name: "server",
Usage: "Start HTTP server for webhook event driven usage",
Action: cmd.Server,
Flags: []cli.Flag{
&cli.StringFlag{
Name: cmd.FlagWebhookSecret,
Usage: "Used to validate received payloads. Sent with the request in the X-Gitlab-Token HTTP header",
EnvVars: []string{
"SCM_ENGINE_WEBHOOK_SECRET",
},
},
&cli.StringFlag{
Name: cmd.FlagServerListen,
Usage: "IP + Port that the HTTP server should listen on",
Value: "0.0.0.0:3000",
EnvVars: []string{
"SCM_ENGINE_LISTEN",
},
},
},
},
},
}
origHelpPrinterCustom := cli.HelpPrinterCustom
cli.HelpPrinterCustom = func(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
origHelpPrinterCustom(out, templ, data, customFuncs)
if data != app {
origHelpPrinterCustom(app.Writer, cmd.GlobalOptionsTemplate, app, nil)
}
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}