Skip to content

Commit bcb80e9

Browse files
Merge pull request #367 from depot/cargo-command
2 parents 72e84cf + 3b05dfa commit bcb80e9

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

pkg/cmd/cargo/cargo.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cargo
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"os/signal"
8+
"strings"
9+
"syscall"
10+
11+
"github.com/depot/cli/pkg/helpers"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var orgID string
16+
17+
func NewCmdCargo() *cobra.Command {
18+
cmd := &cobra.Command{
19+
Use: "cargo [cargo-flags]...",
20+
Short: "Run cargo with Depot caching",
21+
DisableFlagParsing: true,
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
ctx := cmd.Context()
24+
25+
// Manual flag parsing to handle --org
26+
var cargoArgs []string
27+
i := 0
28+
for i < len(args) {
29+
arg := args[i]
30+
if arg == "--org" && i+1 < len(args) {
31+
orgID = args[i+1]
32+
i += 2
33+
} else if after, ok := strings.CutPrefix(arg, "--org="); ok {
34+
orgID = after
35+
i++
36+
} else {
37+
cargoArgs = append(cargoArgs, arg)
38+
i++
39+
}
40+
}
41+
42+
// If no org ID specified via flag, check environment variable
43+
if orgID == "" {
44+
orgID = os.Getenv("DEPOT_ORG_ID")
45+
}
46+
47+
// Check for sccache
48+
sccachePath, err := exec.LookPath("sccache")
49+
if err != nil {
50+
return fmt.Errorf("sccache not found in PATH: %w\n\nPlease install sccache: cargo install sccache", err)
51+
}
52+
53+
// Get authentication token
54+
token, err := helpers.ResolveToken(ctx, "")
55+
if err != nil {
56+
return fmt.Errorf("failed to resolve token: %w", err)
57+
}
58+
59+
// Create cargo command
60+
cargoCmd := exec.CommandContext(ctx, "cargo", cargoArgs...)
61+
cargoCmd.Stdin = os.Stdin
62+
cargoCmd.Stdout = os.Stdout
63+
cargoCmd.Stderr = os.Stderr
64+
65+
// Inherit environment and filter out existing sccache vars
66+
cargoCmd.Env = []string{}
67+
for _, env := range os.Environ() {
68+
// Skip any existing SCCACHE environment variables
69+
if !strings.HasPrefix(env, "SCCACHE_") {
70+
cargoCmd.Env = append(cargoCmd.Env, env)
71+
}
72+
}
73+
74+
// Now add our sccache vars
75+
cargoCmd.Env = append(cargoCmd.Env, fmt.Sprintf("RUSTC_WRAPPER=%s", sccachePath))
76+
cargoCmd.Env = append(cargoCmd.Env, "SCCACHE_WEBDAV_ENDPOINT=https://cache.depot.dev")
77+
78+
if orgID != "" {
79+
// Use org-specific authentication
80+
cargoCmd.Env = append(cargoCmd.Env, fmt.Sprintf("SCCACHE_WEBDAV_USERNAME=%s", orgID))
81+
cargoCmd.Env = append(cargoCmd.Env, fmt.Sprintf("SCCACHE_WEBDAV_PASSWORD=%s", token))
82+
} else {
83+
// Use token-only authentication
84+
cargoCmd.Env = append(cargoCmd.Env, fmt.Sprintf("SCCACHE_WEBDAV_TOKEN=%s", token))
85+
}
86+
87+
// Set up signal handling
88+
signalCh := make(chan os.Signal, 1)
89+
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
90+
go func() {
91+
sig := <-signalCh
92+
if cargoCmd.Process != nil {
93+
_ = cargoCmd.Process.Signal(sig)
94+
}
95+
}()
96+
97+
// Run cargo
98+
err = cargoCmd.Run()
99+
if err != nil {
100+
if exitErr, ok := err.(*exec.ExitError); ok {
101+
os.Exit(exitErr.ExitCode())
102+
}
103+
return err
104+
}
105+
106+
return nil
107+
},
108+
}
109+
110+
cmd.Flags().StringVar(&orgID, "org", "", "Organization ID")
111+
cmd.Flags().Lookup("org").Hidden = true
112+
113+
return cmd
114+
}

pkg/cmd/root/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
bakeCmd "github.com/depot/cli/pkg/cmd/bake"
99
buildCmd "github.com/depot/cli/pkg/cmd/build"
1010
cacheCmd "github.com/depot/cli/pkg/cmd/cache"
11+
cargoCmd "github.com/depot/cli/pkg/cmd/cargo"
1112
claudeCmd "github.com/depot/cli/pkg/cmd/claude"
1213
dockerCmd "github.com/depot/cli/pkg/cmd/docker"
1314
"github.com/depot/cli/pkg/cmd/exec"
@@ -59,6 +60,7 @@ func NewCmdRoot(version, buildDate string) *cobra.Command {
5960
cmd.AddCommand(bakeCmd.NewCmdBake())
6061
cmd.AddCommand(buildCmd.NewCmdBuild())
6162
cmd.AddCommand(cacheCmd.NewCmdCache())
63+
cmd.AddCommand(cargoCmd.NewCmdCargo())
6264
cmd.AddCommand(claudeCmd.NewCmdClaude())
6365
cmd.AddCommand(initCmd.NewCmdInit())
6466
cmd.AddCommand(list.NewCmdList())

0 commit comments

Comments
 (0)