-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (151 loc) · 4.38 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
159
160
161
162
163
164
165
package main
import (
"errors"
"log/slog"
"os"
"time"
"github.com/be9/tbc/cmd"
"github.com/urfave/cli/v2"
)
const (
VerboseFlag = "verbose"
SummaryFlag = "summary"
defaultCacheTimeout = 30 * time.Second
)
func main() {
var (
opts cmd.Options
certFile, keyFile string
logger = slog.Default()
)
app := &cli.App{
Name: "tbc",
Usage: "TurboRepo <--> Bazel Remote Cache Proxy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
EnvVars: []string{"TBC_HOST"},
Usage: "Remote cache server `HOST`",
Required: true,
Aliases: []string{"H"},
Destination: &opts.RemoteCacheHost,
},
&cli.StringFlag{
Name: "addr",
EnvVars: []string{"TBC_ADDR"},
Usage: "Address to bind to",
Value: "127.0.0.1:8080",
Destination: &opts.BindAddr,
},
&cli.StringFlag{
Name: "tls_client_certificate",
EnvVars: []string{"TBC_CLIENT_CERT"},
Usage: "TLS certificate `FILE`",
TakesFile: true,
Destination: &certFile,
},
&cli.StringFlag{
Name: "tls_client_key",
EnvVars: []string{"TBC_CLIENT_KEY"},
Usage: "TLS key `FILE`",
TakesFile: true,
Destination: &keyFile,
},
&cli.DurationFlag{
Name: "timeout",
EnvVars: []string{"TBC_CLIENT_TIMEOUT"},
Usage: "Cache ops timeout",
Value: defaultCacheTimeout,
Destination: &opts.RemoteCacheTimeout,
},
&cli.BoolFlag{
Name: "auto-env",
EnvVars: []string{"TBC_AUTO_ENV"},
Usage: "Set up environment for turbo",
Value: true,
Destination: &opts.AutoEnv,
},
&cli.BoolFlag{
Name: "ignore-failures",
EnvVars: []string{"TBC_IGNORE_FAILURES"},
Usage: "Just run turbo without the proxy if proxy fails",
Destination: &opts.IgnoreFailures,
},
&cli.BoolFlag{
Name: "disable",
EnvVars: []string{"TBC_DISABLE"},
Usage: "Run turbo without the proxy",
Destination: &opts.Disabled,
},
&cli.BoolFlag{
Name: VerboseFlag,
EnvVars: []string{"TBC_VERBOSE"},
Aliases: []string{"v"},
Usage: "Be more verbose",
},
&cli.BoolFlag{
Name: SummaryFlag,
EnvVars: []string{"TBC_SUMMARY"},
Aliases: []string{"s"},
Usage: "Print server summary when the wrapped command exits",
},
},
Before: func(c *cli.Context) error {
if c.Bool(VerboseFlag) {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
if (certFile != "") != (keyFile != "") {
return cli.Exit(errors.New("--tls-cert and --tls-key must be provided together"), 1)
}
if certFile != "" {
certPEMBlock, err := os.ReadFile(certFile)
if err != nil {
return cli.Exit(err, 1)
}
keyPEMBlock, err := os.ReadFile(keyFile)
if err != nil {
return cli.Exit(err, 1)
}
opts.RemoteCacheTLS = &cmd.TLSCerts{CertPEM: certPEMBlock, KeyPEM: keyPEMBlock}
}
opts.Command = c.Args().First()
opts.Args = c.Args().Tail()
return nil
},
Action: func(c *cli.Context) error {
exitCode, stats, errorsIgnored, err := cmd.Main(logger, opts)
if err != nil {
return cli.Exit(err, exitCode)
}
if c.Bool(SummaryFlag) && !opts.Disabled && !errorsIgnored {
logger.Info("server stats", stats.SlogArgs()...)
}
os.Exit(exitCode)
return nil
},
HideHelpCommand: true,
ArgsUsage: "command <command arguments>",
Description: `Spin up a Turborepo-compatible remote cache server that forwards requests to a Bazel-compatible remote cache server
and execute the provided command.
Examples:
# Check the server with curl (by default, the server binds to 127.0.0.1:8080)
tbc --host bazel-cache-host:port curl http://localhost:8080/v8/artifacts/status
# Run 'turbo build' with auto-set variables; if cache doesn't work, run the command ignoring the cache:
env TURBO_REMOTE_CACHE_SIGNATURE_KEY=super_secret \
tbc --host bazel-cache-host:port --auto-env --ignore-failures --summary \
pnpm turbo build
# Run 'turbo build' with manually set vars:
env TURBO_REMOTE_CACHE_SIGNATURE_KEY=super_secret \
TURBO_API=http://localhost:8080 \
TURBO_TOKEN=any \ # this is not actually used, but required to be set by turbo
TURBO_TEAM=any \
tbc --host bazel-cache-host:port \
--summary \
--auto-env=false \
pnpm turbo build
`,
}
if err := app.Run(os.Args); err != nil {
slog.Error(err.Error())
}
}