@@ -7,76 +7,39 @@ import (
7
7
"strings"
8
8
9
9
"github.com/rjeczalik/notify"
10
-
11
- "github.com/sourcegraph/sourcegraph/dev/sg/internal/secrets"
12
10
)
13
11
14
12
// A BazelCommand is a command definition for sg run/start that uses
15
13
// bazel under the hood. It will handle restarting itself autonomously,
16
14
// as long as iBazel is running and watch that specific target.
17
15
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"`
31
18
// RunTarget specifies a target that should be run via `bazel run $RunTarget` instead of directly executing the binary.
32
19
RunTarget string `yaml:"runTarget"`
33
20
}
34
21
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
+ }
54
32
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 )
57
35
}
58
36
59
37
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 )
76
39
}
77
40
78
- func (bc BazelCommand ) GetExternalSecrets () map [ string ]secrets. ExternalSecret {
79
- return bc .ExternalSecrets
41
+ func (bc BazelCommand ) GetConfig () SGConfigCommandOptions {
42
+ return bc .Config
80
43
}
81
44
82
45
func (bc BazelCommand ) watchPaths () ([]string , error ) {
@@ -114,12 +77,25 @@ func (bc BazelCommand) GetExecCmd(ctx context.Context) (*exec.Cmd, error) {
114
77
}
115
78
}
116
79
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
118
81
}
119
82
120
- func outputPath ( ) ([] byte , error ) {
83
+ func binaryLocation ( target string ) (string , error ) {
121
84
// Get the output directory from Bazel, which varies depending on which OS
122
85
// 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
125
101
}
0 commit comments