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

Commit d5b72e8

Browse files
jamesmcnamaraburmudarbobheadxi
authored
[sg] share runner implementation between run and start (#61161)
* 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 * share runner implementation between sg run and sg start * Update dev/sg/sg_start.go Co-authored-by: Robert Lin <[email protected]> * small changes * fixed compile error --------- Co-authored-by: William Bezuidenhout <[email protected]> Co-authored-by: Robert Lin <[email protected]>
1 parent d144209 commit d5b72e8

10 files changed

+219
-130
lines changed

Diff for: dev/sg/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ go_library(
111111
"@com_github_opsgenie_opsgenie_go_sdk_v2//alert",
112112
"@com_github_opsgenie_opsgenie_go_sdk_v2//client",
113113
"@com_github_slack_go_slack//:slack",
114-
"@com_github_sourcegraph_conc//pool",
115114
"@com_github_sourcegraph_log//:log",
116115
"@com_github_sourcegraph_run//:run",
117116
"@com_github_urfave_cli_v2//:cli",

Diff for: dev/sg/internal/run/bazel_command.go

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func (bc BazelCommand) GetConfig() SGConfigCommandOptions {
4242
return bc.Config
4343
}
4444

45+
func (bc BazelCommand) UpdateConfig(f func(*SGConfigCommandOptions)) SGConfigCommand {
46+
f(&bc.Config)
47+
return bc
48+
}
49+
50+
func (bc BazelCommand) GetBazelTarget() string {
51+
return bc.Target
52+
}
4553
func (bc BazelCommand) watchPaths() ([]string, error) {
4654
// If no target is defined, there is nothing to be built and watched
4755
if bc.Target == "" {

Diff for: dev/sg/internal/run/command.go

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func (cmd Command) GetConfig() SGConfigCommandOptions {
5555
return cmd.Config
5656
}
5757

58+
func (cmd Command) UpdateConfig(f func(*SGConfigCommandOptions)) SGConfigCommand {
59+
f(&cmd.Config)
60+
return cmd
61+
}
62+
5863
func (cmd Command) GetName() string {
5964
return cmd.Config.Name
6065
}
@@ -66,6 +71,10 @@ func (cmd Command) GetBinaryLocation() (string, error) {
6671
return "", noBinaryError{name: cmd.Config.Name}
6772
}
6873

74+
func (cmd Command) GetBazelTarget() string {
75+
return ""
76+
}
77+
6978
func (cmd Command) GetExecCmd(ctx context.Context) (*exec.Cmd, error) {
7079
return exec.CommandContext(ctx, "bash", "-c", cmd.Cmd), nil
7180
}

Diff for: dev/sg/internal/run/docker_commmand.go

+9
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ func (dc DockerCommand) GetBinaryLocation() (string, error) {
8383
return binaryLocation(dc.Target)
8484
}
8585

86+
func (dc DockerCommand) GetBazelTarget() string {
87+
return dc.Target
88+
}
89+
90+
func (dc DockerCommand) UpdateConfig(f func(*SGConfigCommandOptions)) SGConfigCommand {
91+
f(&dc.Config)
92+
return dc
93+
}
94+
8695
func (dc DockerCommand) StartWatch(ctx context.Context) (<-chan struct{}, error) {
8796
if watchPaths, err := dc.watchPaths(); err != nil {
8897
return nil, err

Diff for: dev/sg/internal/run/ibazel.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ type IBazel struct {
3434
events *iBazelEventHandler
3535
logsDir string
3636
logFile *os.File
37-
dir string
3837
proc *startedCmd
3938
logs chan<- output.FancyLine
4039
}
4140

4241
// returns a runner to interact with ibazel.
43-
func NewIBazel(targets []string, dir string) (*IBazel, error) {
42+
func NewIBazel(targets []string) (*IBazel, error) {
4443
logsDir, err := initLogsDir()
4544
if err != nil {
4645
return nil, err
@@ -56,7 +55,6 @@ func NewIBazel(targets []string, dir string) (*IBazel, error) {
5655
events: newIBazelEventHandler(profileEventsPath(logsDir)),
5756
logsDir: logsDir,
5857
logFile: logFile,
59-
dir: dir,
6058
}, nil
6159
}
6260

@@ -146,11 +144,15 @@ func (ib *IBazel) WaitForInitialBuild(ctx context.Context) error {
146144
return nil
147145
}
148146

149-
func (ib *IBazel) getCommandOptions(ctx context.Context) commandOptions {
147+
func (ib *IBazel) getCommandOptions(ctx context.Context) (commandOptions, error) {
148+
dir, err := root.RepositoryRoot()
149+
if err != nil {
150+
return commandOptions{}, err
151+
}
150152
return commandOptions{
151153
name: "iBazel",
152154
exec: ib.GetExecCmd(ctx),
153-
dir: ib.dir,
155+
dir: dir,
154156
// Don't output iBazel logs (which are all on stderr) until
155157
// initial build is complete as it will break the progress bar
156158
stderr: outputOptions{
@@ -159,13 +161,17 @@ func (ib *IBazel) getCommandOptions(ctx context.Context) commandOptions {
159161
ib.logFile,
160162
&patternMatcher{regex: watchErrorRegex, callback: ib.logWatchError},
161163
}},
162-
}
164+
}, nil
163165
}
164166

165167
// Build starts an ibazel process to build the targets provided in the constructor
166168
// It runs perpetually, watching for file changes
167-
func (ib *IBazel) build(ctx context.Context) (err error) {
168-
ib.proc, err = startCmd(ctx, ib.getCommandOptions(ctx))
169+
func (ib *IBazel) build(ctx context.Context) error {
170+
opts, err := ib.getCommandOptions(ctx)
171+
if err != nil {
172+
return err
173+
}
174+
ib.proc, err = startCmd(ctx, opts)
169175
return err
170176
}
171177

Diff for: dev/sg/internal/run/installer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type InstallManager struct {
5050
stats *installAnalytics
5151
}
5252

53-
func Install(ctx context.Context, env map[string]string, verbose bool, cmds ...Installer) error {
53+
func Install(ctx context.Context, env map[string]string, verbose bool, cmds []Installer) error {
5454
installer := newInstallManager(cmds, std.Out, env, verbose)
5555

5656
installer.start(ctx)

Diff for: dev/sg/internal/run/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type cmdRunner struct {
2424
verbose bool
2525
}
2626

27-
func Commands(ctx context.Context, parentEnv map[string]string, verbose bool, cmds ...SGConfigCommand) (err error) {
27+
func Commands(ctx context.Context, parentEnv map[string]string, verbose bool, cmds []SGConfigCommand) (err error) {
2828
if len(cmds) == 0 {
2929
// Exit early if there are no commands to run.
3030
return nil

Diff for: dev/sg/internal/run/sgconfig_command.go

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type SGConfigCommand interface {
1313
GetConfig() SGConfigCommandOptions
1414
GetBinaryLocation() (string, error)
1515
GetExecCmd(context.Context) (*exec.Cmd, error)
16+
UpdateConfig(func(*SGConfigCommandOptions)) SGConfigCommand
17+
18+
// Optionally returns a bazel target associated with this command
19+
GetBazelTarget() string
1620

1721
// Start a file watcher on the relevant filesystem sub-tree for this command
1822
StartWatch(context.Context) (<-chan struct{}, error)

Diff for: dev/sg/sg_run.go

+12-37
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76
"sort"
87
"strings"
98

109
"github.com/urfave/cli/v2"
1110
"gopkg.in/yaml.v3"
1211

13-
"github.com/sourcegraph/conc/pool"
14-
1512
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
16-
"github.com/sourcegraph/sourcegraph/dev/sg/internal/run"
1713
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
1814
"github.com/sourcegraph/sourcegraph/dev/sg/interrupt"
1915
"github.com/sourcegraph/sourcegraph/lib/cliutil/completions"
20-
"github.com/sourcegraph/sourcegraph/lib/output"
2116
)
2217

18+
var deprecationNotice = "sg run is deprecated. Use 'sg start -cmd' instead.\n"
19+
2320
func init() {
2421
postInitHooks = append(postInitHooks,
2522
func(cmd *cli.Context) {
@@ -39,9 +36,9 @@ func init() {
3936

4037
var runCommand = &cli.Command{
4138
Name: "run",
42-
Usage: "Run the given commands",
39+
Usage: deprecationNotice,
4340
ArgsUsage: "[command]",
44-
UsageText: `
41+
UsageText: deprecationNotice + `
4542
# Run specific commands
4643
sg run gitserver
4744
sg run frontend
@@ -56,17 +53,13 @@ sg run gitserver frontend repo-updater
5653
sg run -describe jaeger
5754
`,
5855
Category: category.Dev,
56+
Action: runExec,
5957
Flags: []cli.Flag{
6058
&cli.BoolFlag{
6159
Name: "describe",
6260
Usage: "Print details about selected run target",
6361
},
64-
&cli.BoolFlag{
65-
Name: "legacy",
66-
Usage: "Force run to pick the non-bazel variant of the command",
67-
},
6862
},
69-
Action: runExec,
7063
BashComplete: completions.CompleteArgs(func() (options []string) {
7164
config, _ := getConfig()
7265
if config == nil {
@@ -84,28 +77,13 @@ func runExec(ctx *cli.Context) error {
8477
if err != nil {
8578
return err
8679
}
87-
legacy := ctx.Bool("legacy")
88-
89-
args := ctx.Args().Slice()
90-
if len(args) == 0 {
91-
std.Out.WriteLine(output.Styled(output.StyleWarning, "No command specified"))
92-
return flag.ErrHelp
93-
}
94-
95-
cmds := make([]run.SGConfigCommand, 0, len(args))
96-
for _, arg := range args {
97-
if bazelCmd, ok := config.BazelCommands[arg]; ok && !legacy {
98-
cmds = append(cmds, bazelCmd)
99-
} else if cmd, ok := config.Commands[arg]; ok {
100-
cmds = append(cmds, cmd)
101-
} else {
102-
std.Out.WriteLine(output.Styledf(output.StyleWarning, "ERROR: command %q not found :(", arg))
103-
return flag.ErrHelp
104-
}
80+
cmds, err := listToCommands(config, ctx.Args().Slice())
81+
if err != nil {
82+
return err
10583
}
10684

10785
if ctx.Bool("describe") {
108-
for _, cmd := range cmds {
86+
for _, cmd := range cmds.commands {
10987
out, err := yaml.Marshal(cmd)
11088
if err != nil {
11189
return err
@@ -118,17 +96,14 @@ func runExec(ctx *cli.Context) error {
11896
return nil
11997
}
12098

121-
p := pool.New().WithContext(ctx.Context).WithCancelOnError()
122-
p.Go(func(ctx context.Context) error {
123-
return run.Commands(ctx, config.Env, verbose, cmds...)
124-
})
125-
126-
return p.Wait()
99+
return cmds.start(ctx.Context)
127100
}
128101

129102
func constructRunCmdLongHelp() string {
130103
var out strings.Builder
131104

105+
fmt.Fprint(&out, deprecationNotice)
106+
132107
fmt.Fprintf(&out, "Runs the given command. If given a whitespace-separated list of commands it runs the set of commands.\n")
133108

134109
config, err := getConfig()

0 commit comments

Comments
 (0)