Skip to content

Commit a8e5f81

Browse files
committed
internal: moved file and project utilities that are not intended to be exported
commands/operator-sdk/cmd/*: use internal/util packages and get rid of command/.../cmdutil pkg/util/*: move all pkg utilities into pkg/util
1 parent 09d91f8 commit a8e5f81

File tree

15 files changed

+58
-59
lines changed

15 files changed

+58
-59
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ 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/fileutil"
22+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2223
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2324
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2425

@@ -64,16 +65,16 @@ Example:
6465

6566
func apiRun(cmd *cobra.Command, args []string) {
6667
// Create and validate new resource
67-
cmdutil.MustInProjectRoot()
68+
projutil.MustInProjectRoot()
6869
r, err := scaffold.NewResource(apiVersion, kind)
6970
if err != nil {
7071
log.Fatal(err)
7172
}
7273

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

7576
cfg := &input.Config{
76-
Repo: cmdutil.CheckAndGetCurrPkg(),
77+
Repo: projutil.CheckAndGetCurrPkg(),
7778
AbsProjectPath: absProjectPath,
7879
}
7980

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ 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

@@ -52,7 +52,7 @@ Generated CR filename: <project-name>/deploy/crds/<group>_<version>_<kind>_cr.y
5252

5353
func crdFunc(cmd *cobra.Command, args []string) {
5454
cfg := &input.Config{
55-
AbsProjectPath: cmdutil.MustGetwd(),
55+
AbsProjectPath: projutil.MustGetwd(),
5656
}
5757
if len(args) != 0 {
5858
log.Fatal("crd command doesn't accept any arguments")

commands/operator-sdk/cmd/build.go

Lines changed: 4 additions & 4 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"
@@ -137,7 +137,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
137137
log.Fatalf("build command needs exactly 1 argument")
138138
}
139139

140-
cmdutil.MustInProjectRoot()
140+
projutil.MustInProjectRoot()
141141
goBuildEnv := append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
142142
wd, err := os.Getwd()
143143
if err != nil {
@@ -187,9 +187,9 @@ func buildFunc(cmd *cobra.Command, args []string) {
187187
_, err = os.Stat(testDockerfile)
188188
if err != nil && os.IsNotExist(err) {
189189

190-
absProjectPath := cmdutil.MustGetwd()
190+
absProjectPath := projutil.MustGetwd()
191191
cfg := &input.Config{
192-
Repo: cmdutil.CheckAndGetCurrPkg(),
192+
Repo: projutil.CheckAndGetCurrPkg(),
193193
AbsProjectPath: absProjectPath,
194194
ProjectName: filepath.Base(wd),
195195
}

commands/operator-sdk/cmd/generate/k8s.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"os/exec"
2323
"path/filepath"
2424

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

2828
"github.com/spf13/cobra"
@@ -48,8 +48,8 @@ func k8sFunc(cmd *cobra.Command, args []string) {
4848

4949
// K8sCodegen performs deepcopy code-generation for all custom resources under pkg/apis
5050
func K8sCodegen() {
51-
cmdutil.MustInProjectRoot()
52-
repoPkg := cmdutil.CheckAndGetCurrPkg()
51+
projutil.MustInProjectRoot()
52+
repoPkg := projutil.CheckAndGetCurrPkg()
5353
outputPkg := filepath.Join(repoPkg, "pkg/generated")
5454
apisPkg := filepath.Join(repoPkg, scaffold.ApisDir)
5555
groupVersions, err := parseGroupVersions()

commands/operator-sdk/cmd/new.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
26+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
27+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2728
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2829
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
2930
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
@@ -83,10 +84,10 @@ func newFunc(cmd *cobra.Command, args []string) {
8384
verifyFlags()
8485

8586
switch operatorType {
86-
case cmdutil.OperatorTypeGo:
87+
case projutil.OperatorTypeGo:
8788
doScaffold()
8889
pullDep()
89-
case cmdutil.OperatorTypeAnsible:
90+
case projutil.OperatorTypeAnsible:
9091
doAnsibleScaffold()
9192
}
9293
initGit()
@@ -105,7 +106,7 @@ func parse(args []string) {
105106
// mustBeNewProject checks if the given project exists under the current diretory.
106107
// it exits with error when the project exists.
107108
func mustBeNewProject() {
108-
fp := filepath.Join(cmdutil.MustGetwd(), projectName)
109+
fp := filepath.Join(projutil.MustGetwd(), projectName)
109110
stat, err := os.Stat(fp)
110111
if err != nil && os.IsNotExist(err) {
111112
return
@@ -120,8 +121,8 @@ func mustBeNewProject() {
120121

121122
func doScaffold() {
122123
cfg := &input.Config{
123-
Repo: filepath.Join(cmdutil.CheckAndGetCurrPkg(), projectName),
124-
AbsProjectPath: filepath.Join(cmdutil.MustGetwd(), projectName),
124+
Repo: filepath.Join(projutil.CheckAndGetCurrPkg(), projectName),
125+
AbsProjectPath: filepath.Join(projutil.MustGetwd(), projectName),
125126
ProjectName: projectName,
126127
}
127128

@@ -146,7 +147,7 @@ func doScaffold() {
146147

147148
func doAnsibleScaffold() {
148149
cfg := &input.Config{
149-
AbsProjectPath: filepath.Join(cmdutil.MustGetwd(), projectName),
150+
AbsProjectPath: filepath.Join(projutil.MustGetwd(), projectName),
150151
ProjectName: projectName,
151152
}
152153

@@ -225,8 +226,8 @@ func doAnsibleScaffold() {
225226
// repoPath field on generator is used primarily in generation of Go operator. For Ansible we will set it to cwd
226227
func repoPath() string {
227228
// We only care about GOPATH constraint checks if we are a Go operator
228-
wd := cmdutil.MustGetwd()
229-
if operatorType == cmdutil.OperatorTypeGo {
229+
wd := projutil.MustGetwd()
230+
if operatorType == projutil.OperatorTypeGo {
230231
gp := os.Getenv(gopath)
231232
if len(gp) == 0 {
232233
log.Fatal("$GOPATH env not set")
@@ -244,17 +245,17 @@ func repoPath() string {
244245
}
245246

246247
func verifyFlags() {
247-
if operatorType != cmdutil.OperatorTypeGo && operatorType != cmdutil.OperatorTypeAnsible {
248+
if operatorType != projutil.OperatorTypeGo && operatorType != projutil.OperatorTypeAnsible {
248249
log.Fatal("--type can only be `go` or `ansible`")
249250
}
250-
if operatorType != cmdutil.OperatorTypeAnsible && generatePlaybook {
251+
if operatorType != projutil.OperatorTypeAnsible && generatePlaybook {
251252
log.Fatal("--generate-playbook can only be used with --type `ansible`")
252253
}
253-
if operatorType == cmdutil.OperatorTypeGo && (len(apiVersion) != 0 || len(kind) != 0) {
254+
if operatorType == projutil.OperatorTypeGo && (len(apiVersion) != 0 || len(kind) != 0) {
254255
log.Fatal(`go type operator does not use --api-version or --kind. Please see "operator-sdk add" command after running new.`)
255256
}
256257

257-
if operatorType != cmdutil.OperatorTypeGo {
258+
if operatorType != projutil.OperatorTypeGo {
258259
if len(apiVersion) == 0 {
259260
log.Fatal("--api-version must not have empty value")
260261
}
@@ -273,7 +274,7 @@ func verifyFlags() {
273274

274275
func execCmd(stdout *os.File, cmd string, args ...string) {
275276
dc := exec.Command(cmd, args...)
276-
dc.Dir = filepath.Join(cmdutil.MustGetwd(), projectName)
277+
dc.Dir = filepath.Join(projutil.MustGetwd(), projectName)
277278
dc.Stdout = stdout
278279
dc.Stderr = os.Stderr
279280
err := dc.Run()

commands/operator-sdk/cmd/test/local.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
26+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
27+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2728
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2829
"github.com/operator-framework/operator-sdk/pkg/test"
2930

@@ -68,7 +69,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
6869
}
6970
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
7071
if tlConfig.namespacedManPath == "" {
71-
err := os.MkdirAll(deployTestDir, os.FileMode(cmdutil.DefaultDirFileMode))
72+
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
7273
if err != nil {
7374
log.Fatalf("could not create %s: %v", deployTestDir, err)
7475
}
@@ -97,7 +98,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
9798
combined = append(combined, roleBinding...)
9899
combined = append(combined, []byte("\n---\n")...)
99100
combined = append(combined, operator...)
100-
err = ioutil.WriteFile(tlConfig.namespacedManPath, combined, os.FileMode(cmdutil.DefaultFileMode))
101+
err = ioutil.WriteFile(tlConfig.namespacedManPath, combined, os.FileMode(fileutil.DefaultFileMode))
101102
if err != nil {
102103
log.Fatalf("could not create temporary namespaced manifest file: %v", err)
103104
}
@@ -109,7 +110,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
109110
}()
110111
}
111112
if tlConfig.globalManPath == "" {
112-
err := os.MkdirAll(deployTestDir, os.FileMode(cmdutil.DefaultDirFileMode))
113+
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
113114
if err != nil {
114115
log.Fatalf("could not create %s: %v", deployTestDir, err)
115116
}
@@ -133,7 +134,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
133134
combined = append(combined, fileBytes...)
134135
}
135136
}
136-
err = ioutil.WriteFile(tlConfig.globalManPath, combined, os.FileMode(cmdutil.DefaultFileMode))
137+
err = ioutil.WriteFile(tlConfig.globalManPath, combined, os.FileMode(fileutil.DefaultFileMode))
137138
if err != nil {
138139
log.Fatalf("could not create temporary global manifest file: %v", err)
139140
}
@@ -148,7 +149,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
148149
testArgs = append(testArgs, "-"+test.KubeConfigFlag, tlConfig.kubeconfig)
149150
testArgs = append(testArgs, "-"+test.NamespacedManPathFlag, tlConfig.namespacedManPath)
150151
testArgs = append(testArgs, "-"+test.GlobalManPathFlag, tlConfig.globalManPath)
151-
testArgs = append(testArgs, "-"+test.ProjRootFlag, cmdutil.MustGetwd())
152+
testArgs = append(testArgs, "-"+test.ProjRootFlag, projutil.MustGetwd())
152153
// if we do the append using an empty go flags, it inserts an empty arg, which causes
153154
// any later flags to be ignored
154155
if tlConfig.goTestFlags != "" {
@@ -159,7 +160,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
159160
}
160161
dc := exec.Command("go", testArgs...)
161162
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", test.TestNamespaceEnv, tlConfig.namespace))
162-
dc.Dir = cmdutil.MustGetwd()
163+
dc.Dir = projutil.MustGetwd()
163164
dc.Stdout = os.Stdout
164165
dc.Stderr = os.Stderr
165166
err := dc.Run()

commands/operator-sdk/cmd/up/local.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"syscall"
2828

29-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
29+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3030
ansibleOperator "github.com/operator-framework/operator-sdk/pkg/ansible/operator"
3131
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
3232
"github.com/operator-framework/operator-sdk/pkg/scaffold"
@@ -70,11 +70,11 @@ const (
7070

7171
func upLocalFunc(cmd *cobra.Command, args []string) {
7272
mustKubeConfig()
73-
switch cmdutil.GetOperatorType() {
74-
case cmdutil.OperatorTypeGo:
75-
cmdutil.MustInProjectRoot()
73+
switch projutil.GetOperatorType() {
74+
case projutil.OperatorTypeGo:
75+
projutil.MustInProjectRoot()
7676
upLocal()
77-
case cmdutil.OperatorTypeAnsible:
77+
case projutil.OperatorTypeAnsible:
7878
upLocalAnsible()
7979
default:
8080
log.Fatal("failed to determine operator type")

pkg/util/file_util.go renamed to internal/util/fileutil/file_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Modified from github.com/kubernetes-sigs/controller-tools/pkg/util/util.go
1616

17-
package util
17+
package fileutil
1818

1919
import (
2020
"fmt"

commands/operator-sdk/cmd/cmdutil/util.go renamed to internal/util/projutil/project_util.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package cmdutil
15+
package projutil
1616

1717
import (
1818
"encoding/json"
@@ -32,10 +32,15 @@ import (
3232
)
3333

3434
const (
35+
SrcDir = "src"
3536
gopkgToml = "./Gopkg.toml"
3637
buildDockerfile = "./build/Dockerfile"
3738
)
3839

40+
const (
41+
GopathEnv = "GOPATH"
42+
)
43+
3944
// OperatorType - the type of operator
4045
type OperatorType = string
4146

@@ -46,15 +51,6 @@ const (
4651
OperatorTypeAnsible OperatorType = "ansible"
4752
)
4853

49-
const (
50-
GopathEnv = "GOPATH"
51-
SrcDir = "src"
52-
53-
DefaultDirFileMode = 0750
54-
DefaultFileMode = 0644
55-
DefaultExecFileMode = 0744
56-
)
57-
5854
// MustInProjectRoot checks if the current dir is the project root and returns the current repo's import path
5955
// e.g github.com/example-inc/app-operator
6056
func MustInProjectRoot() {

pkg/scaffold/scaffold.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"strings"
2727
"text/template"
2828

29+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
2930
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
30-
"github.com/operator-framework/operator-sdk/pkg/util"
3131
"golang.org/x/tools/imports"
3232
)
3333

@@ -74,7 +74,7 @@ func (s *Scaffold) configure(cfg *input.Config) {
7474
// Execute executes scaffolding the Files
7575
func (s *Scaffold) Execute(cfg *input.Config, files ...input.File) error {
7676
if s.GetWriter == nil {
77-
s.GetWriter = (&util.FileWriter{}).WriteCloser
77+
s.GetWriter = (&fileutil.FileWriter{}).WriteCloser
7878
}
7979

8080
// Configure s using common fields from cfg.
@@ -128,9 +128,9 @@ func (s *Scaffold) doTemplate(i input.Input, e input.File, absPath string) error
128128
return err
129129
}
130130

131-
var mode os.FileMode = util.DefaultFileMode
131+
var mode os.FileMode = fileutil.DefaultFileMode
132132
if i.IsExec {
133-
mode = util.DefaultExecFileMode
133+
mode = fileutil.DefaultExecFileMode
134134
}
135135
f, err := s.GetWriter(absPath, mode)
136136
if err != nil {
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)