Skip to content

Commit

Permalink
Remove unneeded Printer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent aaf0d26 commit eb6748c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,17 +49,17 @@ 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)
return
}
}

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,17 +49,17 @@ 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)
return
}
}

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
}
5 changes: 1 addition & 4 deletions staging/src/k8s.io/code-generator/pkg/codegen/client/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 12 additions & 13 deletions staging/src/k8s.io/code-generator/pkg/codegen/helpers/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions staging/src/k8s.io/code-generator/pkg/codegen/helpers/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions staging/src/k8s.io/code-generator/pkg/codegen/openapi/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 0 additions & 28 deletions staging/src/k8s.io/code-generator/pkg/printer/printer.go

This file was deleted.

0 comments on commit eb6748c

Please sign in to comment.