Skip to content

Commit 5a9f7d1

Browse files
authored
Merge pull request #649 from estroz/util-refactor
Util dir refactor
2 parents 5ba2b6e + 62eb94b commit 5a9f7d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+503
-609
lines changed

commands/operator-sdk/cmd/add/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package add
1717
import (
1818
"log"
1919

20-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
2120
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/generate"
21+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2222
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2323
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2424

@@ -64,16 +64,16 @@ Example:
6464

6565
func apiRun(cmd *cobra.Command, args []string) {
6666
// Create and validate new resource
67-
cmdutil.MustInProjectRoot()
67+
projutil.MustInProjectRoot()
6868
r, err := scaffold.NewResource(apiVersion, kind)
6969
if err != nil {
7070
log.Fatal(err)
7171
}
7272

73-
absProjectPath := cmdutil.MustGetwd()
73+
absProjectPath := projutil.MustGetwd()
7474

7575
cfg := &input.Config{
76-
Repo: cmdutil.CheckAndGetCurrPkg(),
76+
Repo: projutil.CheckAndGetCurrPkg(),
7777
AbsProjectPath: absProjectPath,
7878
}
7979

@@ -91,7 +91,7 @@ func apiRun(cmd *cobra.Command, args []string) {
9191
}
9292

9393
// update deploy/role.yaml for the given resource r.
94-
if err := cmdutil.UpdateRoleForResource(r, absProjectPath); err != nil {
94+
if err := scaffold.UpdateRoleForResource(r, absProjectPath); err != nil {
9595
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", r.APIVersion, r.Kind, err)
9696
}
9797

commands/operator-sdk/cmd/add/controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package add
1717
import (
1818
"log"
1919

20-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
20+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2121
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2222
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2323

@@ -57,16 +57,16 @@ Example:
5757
}
5858

5959
func controllerRun(cmd *cobra.Command, args []string) {
60-
cmdutil.MustInProjectRoot()
60+
projutil.MustInProjectRoot()
6161
// Create and validate new resource
6262
r, err := scaffold.NewResource(apiVersion, kind)
6363
if err != nil {
6464
log.Fatal(err)
6565
}
6666

6767
cfg := &input.Config{
68-
Repo: cmdutil.CheckAndGetCurrPkg(),
69-
AbsProjectPath: cmdutil.MustGetwd(),
68+
Repo: projutil.CheckAndGetCurrPkg(),
69+
AbsProjectPath: projutil.MustGetwd(),
7070
}
7171

7272
s := &scaffold.Scaffold{}

commands/operator-sdk/cmd/add/crd.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,13 @@ import (
2121
"path/filepath"
2222
"strings"
2323

24-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
24+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2525
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2626
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2727

2828
"github.com/spf13/cobra"
2929
)
3030

31-
const (
32-
goDir = "GOPATH"
33-
deployCrdDir = "deploy"
34-
)
35-
3631
// NewAddCrdCmd - add crd command
3732
func NewAddCrdCmd() *cobra.Command {
3833
crdCmd := &cobra.Command{
@@ -57,7 +52,7 @@ Generated CR filename: <project-name>/deploy/crds/<group>_<version>_<kind>_cr.y
5752

5853
func crdFunc(cmd *cobra.Command, args []string) {
5954
cfg := &input.Config{
60-
AbsProjectPath: cmdutil.MustGetwd(),
55+
AbsProjectPath: projutil.MustGetwd(),
6156
}
6257
if len(args) != 0 {
6358
log.Fatal("crd command doesn't accept any arguments")
@@ -83,7 +78,7 @@ func crdFunc(cmd *cobra.Command, args []string) {
8378
}
8479

8580
// update deploy/role.yaml for the given resource r.
86-
if err := cmdutil.UpdateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
81+
if err := scaffold.UpdateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
8782
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", resource.APIVersion, resource.Kind, err)
8883
}
8984
}
@@ -111,8 +106,8 @@ func verifyCrdDeployPath() {
111106
log.Fatalf("failed to determine the full path of the current directory: %v", err)
112107
}
113108
// check if the deploy sub-directory exist
114-
_, err = os.Stat(filepath.Join(wd, deployCrdDir))
109+
_, err = os.Stat(filepath.Join(wd, scaffold.DeployDir))
115110
if err != nil {
116-
log.Fatalf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir)
111+
log.Fatalf("the path (./%v) does not exist. run this command in your project directory", scaffold.DeployDir)
117112
}
118113
}

commands/operator-sdk/cmd/build.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"os/exec"
2525
"path/filepath"
2626

27-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
27+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2828
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2929
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
3030
"github.com/operator-framework/operator-sdk/pkg/test"
@@ -132,16 +132,12 @@ func verifyTestManifest(image string) {
132132
}
133133
}
134134

135-
const (
136-
mainGo = "./cmd/manager/main.go"
137-
)
138-
139135
func buildFunc(cmd *cobra.Command, args []string) {
140136
if len(args) != 1 {
141137
log.Fatalf("build command needs exactly 1 argument")
142138
}
143139

144-
cmdutil.MustInProjectRoot()
140+
projutil.MustInProjectRoot()
145141
goBuildEnv := append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
146142
wd, err := os.Getwd()
147143
if err != nil {
@@ -150,8 +146,8 @@ func buildFunc(cmd *cobra.Command, args []string) {
150146

151147
// Don't need to buld go code if Ansible Operator
152148
if mainExists() {
153-
managerDir := filepath.Join(cmdutil.CheckAndGetCurrPkg(), "cmd/manager")
154-
outputBinName := filepath.Join(wd, "build/_output/bin", filepath.Base(wd))
149+
managerDir := filepath.Join(projutil.CheckAndGetCurrPkg(), scaffold.ManagerDir)
150+
outputBinName := filepath.Join(wd, scaffold.BuildBinDir, filepath.Base(wd))
155151
buildCmd := exec.Command("go", "build", "-o", outputBinName, managerDir)
156152
buildCmd.Env = goBuildEnv
157153
o, err := buildCmd.CombinedOutput()
@@ -178,20 +174,22 @@ func buildFunc(cmd *cobra.Command, args []string) {
178174
fmt.Fprintln(os.Stdout, string(o))
179175

180176
if enableTests {
181-
buildTestCmd := exec.Command("go", "test", "-c", "-o", filepath.Join(wd, "build/_output/bin", filepath.Base(wd)+"-test"), testLocationBuild+"/...")
177+
testBinary := filepath.Join(wd, scaffold.BuildBinDir, filepath.Base(wd)+"-test")
178+
buildTestCmd := exec.Command("go", "test", "-c", "-o", testBinary, testLocationBuild+"/...")
182179
buildTestCmd.Env = goBuildEnv
183180
o, err := buildTestCmd.CombinedOutput()
184181
if err != nil {
185182
log.Fatalf("failed to build test binary: %v (%v)", err, string(o))
186183
}
187184
fmt.Fprintln(os.Stdout, string(o))
188185
// if a user is using an older sdk repo as their library, make sure they have required build files
189-
_, err = os.Stat("build/test-framework/Dockerfile")
186+
testDockerfile := filepath.Join(scaffold.BuildTestDir, scaffold.DockerfileFile)
187+
_, err = os.Stat(testDockerfile)
190188
if err != nil && os.IsNotExist(err) {
191189

192-
absProjectPath := cmdutil.MustGetwd()
190+
absProjectPath := projutil.MustGetwd()
193191
cfg := &input.Config{
194-
Repo: cmdutil.CheckAndGetCurrPkg(),
192+
Repo: projutil.CheckAndGetCurrPkg(),
195193
AbsProjectPath: absProjectPath,
196194
ProjectName: filepath.Base(wd),
197195
}
@@ -207,7 +205,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
207205
}
208206
}
209207

210-
testDbcmd := exec.Command("docker", "build", ".", "-f", "build/test-framework/Dockerfile", "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
208+
testDbcmd := exec.Command("docker", "build", ".", "-f", testDockerfile, "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
211209
o, err = testDbcmd.CombinedOutput()
212210
if err != nil {
213211
log.Fatalf("failed to output build image %s: %v (%s)", image, err, string(o))
@@ -219,8 +217,6 @@ func buildFunc(cmd *cobra.Command, args []string) {
219217
}
220218

221219
func mainExists() bool {
222-
if _, err := os.Stat(mainGo); err == nil {
223-
return true
224-
}
225-
return false
220+
_, err := os.Stat(filepath.Join(scaffold.ManagerDir, scaffold.CmdFile))
221+
return err == nil
226222
}

0 commit comments

Comments
 (0)