Skip to content

Commit 693fcfc

Browse files
committed
add toolexec
1 parent 2935236 commit 693fcfc

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

cmd/go-cache-plugin/commands.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"errors"
1111
"fmt"
1212
"github.com/grafana/go-cache-plugin/lib/otel"
13+
"github.com/grafana/go-cache-plugin/lib/toolexec"
1314
"io"
1415
"log"
1516
"net"
1617
"net/http"
1718
"os"
19+
"os/exec"
1820
"os/signal"
1921
"strconv"
2022
"strings"
@@ -78,7 +80,12 @@ var serveFlags struct {
7880
SumDB string `flag:"sumdb,default=$GOCACHE_SUMDB,SumDB servers to proxy for (comma-separated)"`
7981
}
8082

81-
var logger *log.Logger
83+
var toolexecFlags struct {
84+
LogFile string `flag:"log-file,default=trace.log,File used for logs"`
85+
TracingEnabled bool `flag:"tracing,default=false,Enable tracing (optional)"`
86+
TracingContext string `flag:"context,default=runId:runAttempt:jobName:stepName:stepNumber,Tracing params"`
87+
OtelCollectorAddress string `flag:"otel-collector,default,OTEL collector address (optional)"`
88+
}
8289

8390
func noopClose(context.Context) error { return nil }
8491

@@ -189,7 +196,9 @@ func runConnect(env *command.Env, plugin string) error {
189196

190197
ctx := env.Context()
191198
shutdownTracer, reportSpan, err := initTracing(ctx)
192-
199+
if err != nil {
200+
return err
201+
}
193202
addr := plugin
194203
// If the caller has not specified a host/port, then likely this is an older usage which only specifies port
195204
if !strings.Contains(plugin, ":") {
@@ -224,6 +233,27 @@ func runConnect(env *command.Env, plugin string) error {
224233
return nil
225234
}
226235

236+
func runToolexec(env *command.Env) error {
237+
ctx := env.Context()
238+
shutdownTracer, tracingContext, err := initModTracing(ctx)
239+
if err != nil {
240+
return err
241+
}
242+
defer shutdownTracer(ctx)
243+
244+
tool := os.Args[1]
245+
args := os.Args[2:]
246+
247+
pkg := toolexec.GetPackage(os.Args)
248+
249+
_, span := tracingContext.SpanWithContext(ctx, fmt.Sprintf("%s: %s", toolexec.GetTool(tool), pkg))
250+
defer span.End()
251+
cmd := exec.Command(tool, args...)
252+
cmd.Stdout = os.Stdout
253+
cmd.Stderr = os.Stderr
254+
return cmd.Run()
255+
}
256+
227257
func initTracing(ctx context.Context) (func(context.Context) error, func([]byte), error) {
228258
if !flags.TracingEnabled {
229259
return func(context.Context) error { return nil }, func([]byte) {}, nil

cmd/go-cache-plugin/go-cache-plugin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ This mode bridges stdin/stdout to a cache server (see the "serve" command)
7474
listening on the specified port.`,
7575
Run: command.Adapt(runConnect),
7676
},
77+
{
78+
Name: "toolexec",
79+
SetFlags: command.Flags(flax.MustBind, &toolexecFlags),
80+
Run: command.Adapt(runToolexec),
81+
},
7782
command.HelpCommand(helpTopics),
7883
command.VersionCommand(),
7984
},

lib/toolexec/toolexec.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package toolexec
2+
3+
import "strings"
4+
5+
const (
6+
Asm = "asm"
7+
Compile = "compile"
8+
Link = "link"
9+
)
10+
11+
func GetTool(tool string) string {
12+
parts := strings.Split(tool, "/")
13+
return parts[len(parts)-1]
14+
}
15+
16+
func GetPackage(args []string) string {
17+
for i, v := range args {
18+
if i+1 == len(args) {
19+
break
20+
}
21+
22+
if v == "-p" {
23+
return args[i+1]
24+
}
25+
}
26+
27+
return "unknown"
28+
}

0 commit comments

Comments
 (0)