Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 2606bc0

Browse files
SG Docker Commands (#61140)
* first pass at watchable docker commands * extracted options to common struct * added repo root as field in options * added docker specific config and tests * merged in config overrides for linux * cleanup * Update dev/sg/internal/run/command.go Co-authored-by: William Bezuidenhout <[email protected]> * addressed comments * gofmt --------- Co-authored-by: William Bezuidenhout <[email protected]>
1 parent c326671 commit 2606bc0

17 files changed

+911
-269
lines changed

dev/sg/internal/run/BUILD.bazel

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
srcs = [
77
"bazel_command.go",
88
"command.go",
9+
"docker_commmand.go",
910
"helpers.go",
1011
"ibazel.go",
1112
"installer.go",
@@ -14,6 +15,7 @@ go_library(
1415
"prefix_suffix_saver.go",
1516
"run.go",
1617
"sgconfig_command.go",
18+
"sgconfig_command_options.go",
1719
],
1820
importpath = "github.com/sourcegraph/sourcegraph/dev/sg/internal/run",
1921
visibility = ["//dev/sg:__subpackages__"],
@@ -37,7 +39,14 @@ go_library(
3739
go_test(
3840
name = "run_test",
3941
timeout = "short",
40-
srcs = ["logger_test.go"],
42+
srcs = [
43+
"docker_command_test.go",
44+
"logger_test.go",
45+
],
4146
embed = [":run"],
42-
deps = ["@com_github_stretchr_testify//assert"],
47+
deps = [
48+
"@com_github_google_go_cmp//cmp",
49+
"@com_github_stretchr_testify//assert",
50+
"@in_gopkg_yaml_v2//:yaml_v2",
51+
],
4352
)

dev/sg/internal/run/bazel_command.go

+34-58
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,39 @@ import (
77
"strings"
88

99
"github.com/rjeczalik/notify"
10-
11-
"github.com/sourcegraph/sourcegraph/dev/sg/internal/secrets"
1210
)
1311

1412
// A BazelCommand is a command definition for sg run/start that uses
1513
// bazel under the hood. It will handle restarting itself autonomously,
1614
// as long as iBazel is running and watch that specific target.
1715
type BazelCommand struct {
18-
Name string
19-
Description string `yaml:"description"`
20-
Target string `yaml:"target"`
21-
Args string `yaml:"args"`
22-
PreCmd string `yaml:"precmd"`
23-
Env map[string]string `yaml:"env"`
24-
IgnoreStdout bool `yaml:"ignoreStdout"`
25-
IgnoreStderr bool `yaml:"ignoreStderr"`
26-
ContinueWatchOnExit bool `yaml:"continueWatchOnExit"`
27-
// Preamble is a short and visible message, displayed when the command is launched.
28-
Preamble string `yaml:"preamble"`
29-
ExternalSecrets map[string]secrets.ExternalSecret `yaml:"external_secrets"`
30-
16+
Config SGConfigCommandOptions
17+
Target string `yaml:"target"`
3118
// RunTarget specifies a target that should be run via `bazel run $RunTarget` instead of directly executing the binary.
3219
RunTarget string `yaml:"runTarget"`
3320
}
3421

35-
func (bc BazelCommand) GetName() string {
36-
return bc.Name
37-
}
38-
39-
func (bc BazelCommand) GetContinueWatchOnExit() bool {
40-
return bc.ContinueWatchOnExit
41-
}
42-
43-
func (bc BazelCommand) GetEnv() map[string]string {
44-
return bc.Env
45-
}
46-
47-
func (bc BazelCommand) GetIgnoreStdout() bool {
48-
return bc.IgnoreStdout
49-
}
50-
51-
func (bc BazelCommand) GetIgnoreStderr() bool {
52-
return bc.IgnoreStderr
53-
}
22+
// UnmarshalYAML implements the Unmarshaler interface for BazelCommand.
23+
// This allows us to parse the flat YAML configuration into nested struct.
24+
func (bc *BazelCommand) UnmarshalYAML(unmarshal func(any) error) error {
25+
// In order to not recurse infinitely (calling UnmarshalYAML over and over) we create a
26+
// temporary type alias.
27+
// First parse the BazelCommand specific options
28+
type rawBazel BazelCommand
29+
if err := unmarshal((*rawBazel)(bc)); err != nil {
30+
return err
31+
}
5432

55-
func (bc BazelCommand) GetPreamble() string {
56-
return bc.Preamble
33+
// Then parse the common options from the same list into a nested struct
34+
return unmarshal(&bc.Config)
5735
}
5836

5937
func (bc BazelCommand) GetBinaryLocation() (string, error) {
60-
baseOutput, err := outputPath()
61-
if err != nil {
62-
return "", err
63-
}
64-
// Trim "bazel-out" because the next bazel query will include it.
65-
outputPath := strings.TrimSuffix(strings.TrimSpace(string(baseOutput)), "bazel-out")
66-
67-
// Get the binary from the specific target.
68-
cmd := exec.Command("bazel", "cquery", bc.Target, "--output=files")
69-
baseOutput, err = cmd.Output()
70-
if err != nil {
71-
return "", err
72-
}
73-
binPath := strings.TrimSpace(string(baseOutput))
74-
75-
return fmt.Sprintf("%s%s", outputPath, binPath), nil
38+
return binaryLocation(bc.Target)
7639
}
7740

78-
func (bc BazelCommand) GetExternalSecrets() map[string]secrets.ExternalSecret {
79-
return bc.ExternalSecrets
41+
func (bc BazelCommand) GetConfig() SGConfigCommandOptions {
42+
return bc.Config
8043
}
8144

8245
func (bc BazelCommand) watchPaths() ([]string, error) {
@@ -114,12 +77,25 @@ func (bc BazelCommand) GetExecCmd(ctx context.Context) (*exec.Cmd, error) {
11477
}
11578
}
11679

117-
return exec.CommandContext(ctx, "bash", "-c", fmt.Sprintf("%s\n%s", bc.PreCmd, cmd)), nil
80+
return exec.CommandContext(ctx, "bash", "-c", fmt.Sprintf("%s\n%s", bc.Config.PreCmd, cmd)), nil
11881
}
11982

120-
func outputPath() ([]byte, error) {
83+
func binaryLocation(target string) (string, error) {
12184
// Get the output directory from Bazel, which varies depending on which OS
12285
// we're running against.
123-
cmd := exec.Command("bazel", "info", "output_path")
124-
return cmd.Output()
86+
baseOutput, err := exec.Command("bazel", "info", "output_path").Output()
87+
if err != nil {
88+
return "", err
89+
}
90+
// Trim "bazel-out" because the next bazel query will include it.
91+
outputPath := strings.TrimSuffix(strings.TrimSpace(string(baseOutput)), "bazel-out")
92+
93+
// Get the binary from the specific target.
94+
bin, err := exec.Command("bazel", "cquery", target, "--output=files").Output()
95+
if err != nil {
96+
return "", err
97+
}
98+
binPath := strings.TrimSpace(string(bin))
99+
100+
return fmt.Sprintf("%s%s", outputPath, binPath), nil
125101
}

0 commit comments

Comments
 (0)