Skip to content

Commit cc1fb60

Browse files
committed
interanl/util/projutil/proj_util.go: refactor command exec into ExecCmd()
1 parent 0728a54 commit cc1fb60

File tree

6 files changed

+32
-37
lines changed

6 files changed

+32
-37
lines changed

Diff for: commands/operator-sdk/cmd/build.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
158158
buildCmd := exec.Command("docker", "build", ".",
159159
"-f", buildDockerfile,
160160
"-t", baseImageName)
161-
buildCmd.Stdout = os.Stdout
162-
buildCmd.Stderr = os.Stderr
163-
err := buildCmd.Run()
161+
err := projutil.ExecCmd(buildCmd)
164162
if err != nil {
165163
if enableTests {
166164
log.Fatalf("failed to output intermediate image %s: (%v)", image, err)
@@ -215,9 +213,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
215213
"--build-arg", "TESTDIR="+testLocationBuild,
216214
"--build-arg", "BASEIMAGE="+baseImageName,
217215
"--build-arg", "NAMESPACEDMAN="+namespacedManBuild)
218-
testBuildCmd.Stdout = os.Stdout
219-
testBuildCmd.Stderr = os.Stderr
220-
err = testBuildCmd.Run()
216+
err = projutil.ExecCmd(testBuildCmd)
221217
if err != nil {
222218
log.Fatalf("failed to output test image %s: (%v)", image, err)
223219
}
@@ -289,7 +285,8 @@ func buildOperatorBinary() error {
289285
outputBinName := filepath.Join(absProjectPath, scaffold.BuildBinDir, binName)
290286

291287
cmd := exec.Command("go", "build", "-o", outputBinName, managerDir)
292-
return execGoCmd(cmd)
288+
cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
289+
return projutil.ExecCmd(cmd)
293290
}
294291

295292
func buildTestBinary() error {
@@ -298,12 +295,6 @@ func buildTestBinary() error {
298295
outputBinName := filepath.Join(absProjectPath, scaffold.BuildBinDir, binName+"-test")
299296

300297
cmd := exec.Command("go", "test", "-c", "-o", outputBinName, testLocationBuild+"/...")
301-
return execGoCmd(cmd)
302-
}
303-
304-
func execGoCmd(cmd *exec.Cmd) error {
305298
cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
306-
cmd.Stdout = os.Stdout
307-
cmd.Stderr = os.Stderr
308-
return cmd.Run()
299+
return projutil.ExecCmd(cmd)
309300
}

Diff for: commands/operator-sdk/cmd/generate/k8s.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package generate
1717
import (
1818
"fmt"
1919
"io/ioutil"
20-
"os"
2120
"os/exec"
2221
"path/filepath"
2322

@@ -74,9 +73,7 @@ func K8sCodegen() {
7473
groupVersions,
7574
}
7675
cgCmd := exec.Command(genGroupsCmd, args...)
77-
cgCmd.Stdout = os.Stdout
78-
cgCmd.Stderr = os.Stderr
79-
err = cgCmd.Run()
76+
err = projutil.ExecCmd(cgCmd)
8077
if err != nil {
8178
log.Fatalf("failed to perform code-generation: (%v)", err)
8279
}

Diff for: commands/operator-sdk/cmd/new.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ func doAnsibleScaffold() {
225225

226226
// Run galaxy init.
227227
cmd := exec.Command(filepath.Join(galaxyInit.AbsProjectPath, galaxyInit.Path))
228-
cmd.Stdout = os.Stdout
229-
cmd.Stderr = os.Stderr
230-
cmd.Run()
228+
err = projutil.ExecCmd(cmd)
229+
if err != nil {
230+
log.Fatalf("failed to run galaxy-init: (%v)", err)
231+
}
231232

232233
// Delete Galxy INIT
233234
// Mac OS tmp directory is /var/folders/_c/..... this means we have to make sure that we get the top level directory to remove
@@ -271,14 +272,12 @@ func verifyFlags() {
271272
}
272273
}
273274

274-
func execCmd(stdout *os.File, cmd string, args ...string) {
275+
func execProjectCmd(cmd string, args ...string) {
275276
dc := exec.Command(cmd, args...)
276277
dc.Dir = filepath.Join(projutil.MustGetwd(), projectName)
277-
dc.Stdout = stdout
278-
dc.Stderr = os.Stderr
279-
err := dc.Run()
278+
err := projutil.ExecCmd(dc)
280279
if err != nil {
281-
log.Fatalf("failed to exec %s %#v: (%v)", cmd, args, err)
280+
log.Fatal(err)
282281
}
283282
}
284283

@@ -288,7 +287,7 @@ func pullDep() {
288287
log.Fatalf("looking for dep in $PATH: (%v)", err)
289288
}
290289
log.Info("Run dep ensure ...")
291-
execCmd(os.Stdout, dep, ensureCmd, "-v")
290+
execProjectCmd(dep, ensureCmd, "-v")
292291
log.Info("Run dep ensure done")
293292
}
294293

@@ -297,8 +296,8 @@ func initGit() {
297296
return
298297
}
299298
log.Info("Run git init ...")
300-
execCmd(os.Stdout, "git", "init")
301-
execCmd(os.Stdout, "git", "add", "--all")
302-
execCmd(os.Stdout, "git", "commit", "-q", "-m", "INITIAL COMMIT")
299+
execProjectCmd("git", "init")
300+
execProjectCmd("git", "add", "--all")
301+
execProjectCmd("git", "commit", "-q", "-m", "INITIAL COMMIT")
303302
log.Info("Run git init done")
304303
}

Diff for: commands/operator-sdk/cmd/test/local.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,9 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
204204
dc := exec.Command("go", testArgs...)
205205
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", test.TestNamespaceEnv, tlConfig.namespace))
206206
dc.Dir = projutil.MustGetwd()
207-
dc.Stdout = os.Stdout
208-
dc.Stderr = os.Stderr
209-
err := dc.Run()
207+
err := projutil.ExecCmd(dc)
210208
if err != nil {
211-
log.Fatalf("failed to exec `go %s`: (%v)", strings.Join(testArgs, " "), err)
209+
log.Fatal(err)
212210
}
213211

214212
log.Info("Local operator test successfully completed.")

Diff for: commands/operator-sdk/cmd/up/local.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ func upLocal() {
125125
}
126126
os.Exit(0)
127127
}()
128-
dc.Stdout = os.Stdout
129-
dc.Stderr = os.Stderr
130128
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig))
131129
dc.Env = append(dc.Env, fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, namespace))
132-
err := dc.Run()
130+
err := projutil.ExecCmd(dc)
133131
if err != nil {
134132
log.Fatalf("failed to run operator locally: (%v)", err)
135133
}

Diff for: internal/util/projutil/project_util.go

+12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package projutil
1717
import (
1818
"bytes"
1919
"context"
20+
"fmt"
2021
"io/ioutil"
2122
"os"
23+
"os/exec"
2224
"path/filepath"
2325
"strings"
2426

@@ -162,3 +164,13 @@ func IsDockerfileMultistage(dockerfile string) bool {
162164
}
163165
return bytes.Count(df, []byte("FROM")) > 1
164166
}
167+
168+
func ExecCmd(cmd *exec.Cmd) error {
169+
cmd.Stdout = os.Stdout
170+
cmd.Stderr = os.Stderr
171+
err := cmd.Run()
172+
if err != nil {
173+
return fmt.Errorf("failed to exec %s %#v: %v", cmd.Path, cmd.Args, err)
174+
}
175+
return nil
176+
}

0 commit comments

Comments
 (0)