forked from jippi/scm-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (145 loc) · 4.04 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//go:build !generate
// +build !generate
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/jippi/scm-engine/cmd"
"github.com/jippi/scm-engine/pkg/state"
"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: "[email protected]",
},
},
Before: func(cCtx *cli.Context) error {
// Setup global state
cCtx.Context = tui.NewContext(cCtx.Context, cCtx.App.Writer, cCtx.App.ErrWriter)
cCtx.Context = slogctx.With(cCtx.Context, "scm_engine_version", version)
// Write global flags to context
cCtx.Context = state.WithDryRun(cCtx.Context, cCtx.Bool(cmd.FlagDryRun))
cCtx.Context = state.WithRandomSeed(cCtx.Context, time.Now().UnixNano()) // weak seed since only used for codeowner selection
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.BoolFlag{
Name: cmd.FlagDryRun,
Usage: "Dry run, don't actually _do_ actions, just print them",
Value: false,
EnvVars: []string{
"SCM_ENGINE_DRY_RUN",
},
},
},
Commands: []*cli.Command{
cmd.GitLab,
cmd.GitHub,
// DEPRECATED COMMANDS
{
Name: "evaluate",
Usage: "Evaluate a Merge Request",
Hidden: true, // DEPRECATED
Args: true,
ArgsUsage: " [mr_id, mr_id, ...]",
Action: cmd.Evaluate,
Before: func(cCtx *cli.Context) error {
cCtx.Context = state.WithBaseURL(cCtx.Context, cCtx.String(cmd.FlagSCMBaseURL))
cCtx.Context = state.WithProvider(cCtx.Context, "gitlab")
cCtx.Context = state.WithToken(cCtx.Context, cCtx.String(cmd.FlagAPIToken))
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: cmd.FlagAPIToken,
Usage: "GitLab API token",
EnvVars: []string{
"SCM_ENGINE_TOKEN", // SCM Engine Native
},
},
&cli.StringFlag{
Name: cmd.FlagSCMBaseURL,
Usage: "Base URL for the SCM instance",
Value: "https://gitlab.com/",
EnvVars: []string{
"SCM_ENGINE_BASE_URL", // SCM Engine Native
"CI_SERVER_URL", // GitLab CI
},
},
&cli.BoolFlag{
Name: cmd.FlagUpdatePipeline,
Usage: "Update the CI pipeline status with progress",
Value: false,
EnvVars: []string{
"SCM_ENGINE_UPDATE_PIPELINE",
},
},
&cli.StringFlag{
Name: cmd.FlagSCMProject,
Usage: "GitLab project (example: 'gitlab-org/gitlab')",
Required: true,
EnvVars: []string{
"GITLAB_PROJECT",
"CI_PROJECT_PATH", // GitLab CI
},
},
&cli.StringFlag{
Name: cmd.FlagMergeRequestID,
Usage: "The pull/merge ID to process, if not provided as a CLI flag",
EnvVars: []string{
"CI_MERGE_REQUEST_IID", // GitLab CI
},
},
&cli.StringFlag{
Name: cmd.FlagCommitSHA,
Usage: "The git commit sha",
EnvVars: []string{
"CI_COMMIT_SHA", // GitLab CI
},
},
},
},
},
}
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)
}
}