-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (75 loc) · 2.3 KB
/
main.go
File metadata and controls
93 lines (75 loc) · 2.3 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
// bktec fetches and runs test plans generated by Buildkite
// Test Engine.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"github.com/buildkite/test-engine-client/internal/command"
"github.com/buildkite/test-engine-client/internal/config"
"github.com/buildkite/test-engine-client/internal/debug"
"github.com/buildkite/test-engine-client/internal/git"
"github.com/buildkite/test-engine-client/internal/version"
"github.com/urfave/cli/v3"
)
var cfg = config.New()
func main() {
if err := cliCommand.Run(context.Background(), os.Args); err != nil {
logErrorAndExit(err)
}
}
func run(ctx context.Context, cmd *cli.Command) error {
debug.SetDebug(cmd.Bool("debug"))
if err := applyPlanRequestContext(cmd); err != nil {
return err
}
if err := cfg.ValidateForRun(); err != nil {
return fmt.Errorf("invalid configuration...\n%w", err)
}
return command.Run(ctx, &cfg, cmd.String("files"))
}
func plan(ctx context.Context, cmd *cli.Command) error {
debug.SetDebug(cmd.Bool("debug"))
debug.SetOutput(os.Stderr)
if err := applyPlanRequestContext(cmd); err != nil {
return err
}
if err := cfg.ValidateForPlan(); err != nil {
return fmt.Errorf("invalid configuration...\n%w", err)
}
if cmd.Bool("json") {
return command.Plan(ctx, &cfg, cmd.String("files"), command.PlanOutputJSON, "")
} else {
return command.Plan(ctx, &cfg, cmd.String("files"), command.PlanOutputPipelineUpload, cmd.String("pipeline-upload"))
}
}
func backfillCommitMetadata(ctx context.Context, cmd *cli.Command) error {
debug.SetDebug(cmd.Root().Bool("debug"))
debug.SetOutput(os.Stderr)
if err := cfg.ValidateForBackfillCommitMetadata(); err != nil {
return fmt.Errorf("invalid configuration...\n%w", err)
}
return command.BackfillCommitMetadata(ctx, &cfg, &git.ExecGitRunner{})
}
func printVersion(ctx context.Context, cmd *cli.Command, versionFlag bool) error {
// Flag will be true if called with `bktec [...] --version`
if !versionFlag {
return nil
}
fmt.Printf("bktec %s\n", version.Version)
os.Exit(0)
return nil
}
func logErrorAndExit(err error) {
fmt.Fprintln(os.Stderr, err)
exitError := new(exec.ExitError)
if errors.As(err, &exitError) {
// If error wraps an exitError exit with the specified code ...
os.Exit(exitError.ExitCode())
} else {
// otherwise exit code 16
os.Exit(16)
}
}