From eb6748cd96a38e909e82c31073582ebfba2c258b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Wed, 6 Dec 2023 19:15:33 +0100 Subject: [PATCH] Remove unneeded Printer interface --- .../internal/codegen/command/client/cmd.go | 7 ++--- .../internal/codegen/command/helpers/cmd.go | 7 ++--- .../internal/codegen/command/openapi/cmd.go | 7 ++--- .../code-generator/pkg/codegen/client/gen.go | 5 +--- .../pkg/codegen/helpers/deepcopy.go | 25 ++++++++--------- .../code-generator/pkg/codegen/helpers/gen.go | 5 +--- .../code-generator/pkg/codegen/openapi/gen.go | 5 +--- .../pkg/osbin/golang/packageof.go | 2 ++ .../code-generator/pkg/printer/printer.go | 28 ------------------- 9 files changed, 26 insertions(+), 65 deletions(-) delete mode 100644 staging/src/k8s.io/code-generator/pkg/printer/printer.go diff --git a/staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd.go b/staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd.go index a98f404f9884c..ba79f26dae791 100644 --- a/staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd.go +++ b/staging/src/k8s.io/code-generator/internal/codegen/command/client/cmd.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/pflag" "k8s.io/code-generator/internal/codegen/execution" "k8s.io/code-generator/pkg/codegen/client" - "k8s.io/code-generator/pkg/printer" ) // Generator is the interface for generating client code. @@ -50,7 +49,7 @@ func (c Command) Run(ex *execution.Vars) { ex.Exit(8) return } - gen := c.createOrGetGenerator(ex) + gen := c.createOrGetGenerator() if err := gen.Generate(args); err != nil { ex.PrintErrln("Error generating clients:", err) ex.Exit(9) @@ -58,9 +57,9 @@ func (c Command) Run(ex *execution.Vars) { } } -func (c Command) createOrGetGenerator(pr printer.Printer) Generator { +func (c Command) createOrGetGenerator() Generator { if c.Gen == nil { - return &client.Generator{Printer: pr} + return &client.Generator{} } return c.Gen } diff --git a/staging/src/k8s.io/code-generator/internal/codegen/command/helpers/cmd.go b/staging/src/k8s.io/code-generator/internal/codegen/command/helpers/cmd.go index ee34c32426635..003964d028864 100644 --- a/staging/src/k8s.io/code-generator/internal/codegen/command/helpers/cmd.go +++ b/staging/src/k8s.io/code-generator/internal/codegen/command/helpers/cmd.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/pflag" "k8s.io/code-generator/internal/codegen/execution" "k8s.io/code-generator/pkg/codegen/helpers" - "k8s.io/code-generator/pkg/printer" ) // Generator is the interface for generating helper code. @@ -50,7 +49,7 @@ func (c Command) Run(ex *execution.Vars) { c.printErrorWithUsage(ex, err) return } - gen := c.createOrGetGenerator(ex) + gen := c.createOrGetGenerator() if err := gen.Generate(args); err != nil { ex.PrintErrln("Error generating helpers:", err) ex.Exit(11) @@ -65,9 +64,9 @@ func (c Command) printErrorWithUsage(ex *execution.Vars, i ...any) { ex.Exit(10) } -func (c Command) createOrGetGenerator(pr printer.Printer) Generator { +func (c Command) createOrGetGenerator() Generator { if c.Gen == nil { - return &helpers.Generator{Printer: pr} + return &helpers.Generator{} } return c.Gen } diff --git a/staging/src/k8s.io/code-generator/internal/codegen/command/openapi/cmd.go b/staging/src/k8s.io/code-generator/internal/codegen/command/openapi/cmd.go index 7b86e59990fdc..6d55845a3bdf0 100644 --- a/staging/src/k8s.io/code-generator/internal/codegen/command/openapi/cmd.go +++ b/staging/src/k8s.io/code-generator/internal/codegen/command/openapi/cmd.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/pflag" "k8s.io/code-generator/internal/codegen/execution" "k8s.io/code-generator/pkg/codegen/openapi" - "k8s.io/code-generator/pkg/printer" ) // Generator is the interface for generating openapi code. @@ -50,7 +49,7 @@ func (c Command) Run(ex *execution.Vars) { ex.Exit(12) return } - gen := c.createOrGetGenerator(ex) + gen := c.createOrGetGenerator() if err := gen.Generate(args); err != nil { ex.PrintErrln("Error generating openapi:", err) ex.Exit(13) @@ -58,9 +57,9 @@ func (c Command) Run(ex *execution.Vars) { } } -func (c Command) createOrGetGenerator(pr printer.Printer) Generator { +func (c Command) createOrGetGenerator() Generator { if c.Gen == nil { - return &openapi.Generator{Printer: pr} + return &openapi.Generator{} } return c.Gen } diff --git a/staging/src/k8s.io/code-generator/pkg/codegen/client/gen.go b/staging/src/k8s.io/code-generator/pkg/codegen/client/gen.go index 0f7257c463153..ccd761b0bda12 100644 --- a/staging/src/k8s.io/code-generator/pkg/codegen/client/gen.go +++ b/staging/src/k8s.io/code-generator/pkg/codegen/client/gen.go @@ -18,13 +18,10 @@ package client import ( "errors" - "k8s.io/code-generator/pkg/printer" ) // Generator is the client generator. -type Generator struct { - printer.Printer -} +type Generator struct{} func (g *Generator) Generate(args *Args) error { // TODO: implement me diff --git a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/deepcopy.go b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/deepcopy.go index 1401e3acd288f..c795db1ed409d 100644 --- a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/deepcopy.go +++ b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/deepcopy.go @@ -121,21 +121,20 @@ func collectDeepCopyPackages(args *Args) ([]string, error) { func resolvePackage(file string) (string, error) { dir := path.Dir(file) - if wd, err := os.Getwd(); err != nil { - return "", err - } else { - defer os.Chdir(wd) - } - if err := os.Chdir(dir); err != nil { - return "", err - } - if p, err := golang.PackageOf("."); err != nil { - klog.Errorf("Error finding package for %s: %s", dir, err) + var foundPkg string + if err := withinDirectory(dir, func() error { + if p, err := golang.PackageOf("."); err != nil { + klog.Errorf("Error finding package for %s: %s", dir, err) + return err + } else { + klog.V(4).Infof("Found %s", p) + foundPkg = p + return nil + } + }); err != nil { return "", err - } else { - klog.V(4).Infof("Found %s", p) - return p, nil } + return foundPkg, nil } func withinDirectory(dir string, fn func() error) error { diff --git a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/gen.go b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/gen.go index 54404853ede26..2828aa12d92c0 100644 --- a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/gen.go +++ b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/gen.go @@ -17,14 +17,11 @@ package helpers import ( - "k8s.io/code-generator/pkg/printer" "k8s.io/klog/v2" "os" ) -type Generator struct { - printer.Printer -} +type Generator struct{} func (g *Generator) Generate(args *Args) error { // These tools all assume out-dir == in-dir. diff --git a/staging/src/k8s.io/code-generator/pkg/codegen/openapi/gen.go b/staging/src/k8s.io/code-generator/pkg/codegen/openapi/gen.go index ed270ec603b39..d4d6bd02b161c 100644 --- a/staging/src/k8s.io/code-generator/pkg/codegen/openapi/gen.go +++ b/staging/src/k8s.io/code-generator/pkg/codegen/openapi/gen.go @@ -18,12 +18,9 @@ package openapi import ( "errors" - "k8s.io/code-generator/pkg/printer" ) -type Generator struct { - printer.Printer -} +type Generator struct{} func (g *Generator) Generate(args *Args) error { // TODO: implement me diff --git a/staging/src/k8s.io/code-generator/pkg/osbin/golang/packageof.go b/staging/src/k8s.io/code-generator/pkg/osbin/golang/packageof.go index 9d0437c1829b2..2ac021858988c 100644 --- a/staging/src/k8s.io/code-generator/pkg/osbin/golang/packageof.go +++ b/staging/src/k8s.io/code-generator/pkg/osbin/golang/packageof.go @@ -25,6 +25,8 @@ import ( ) // PackageOf returns the package name of the given path. +// +// TODO: Consider rewriting this to use go/packages instead of shelling out. func PackageOf(path string) (string, error) { c := exec.Command("go", "list", "-find", path) c.Env = append(os.Environ(), "GO111MODULE=on") diff --git a/staging/src/k8s.io/code-generator/pkg/printer/printer.go b/staging/src/k8s.io/code-generator/pkg/printer/printer.go deleted file mode 100644 index 43f207db63c30..0000000000000 --- a/staging/src/k8s.io/code-generator/pkg/printer/printer.go +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2023 The Kubernetes Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except In compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to In writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package printer - -// Printer is the interface for printing messages to the standard output and -// error streams. -type Printer interface { - Print(i ...any) - Println(i ...any) - Printf(format string, i ...any) - PrintErr(i ...any) - PrintErrln(i ...any) - PrintErrf(format string, i ...any) -}