Skip to content

Commit

Permalink
Merge pull request #2783 from jingyih/codegen
Browse files Browse the repository at this point in the history
tools: add default values to type and mapper generator flags
  • Loading branch information
google-oss-prow[bot] authored Sep 24, 2024
2 parents 4aa0a1d + 791f2ff commit c78b992
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package generatemapper
import (
"context"
"fmt"
"os"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/codegen"
Expand All @@ -36,8 +37,16 @@ type GenerateMapperOptions struct {
OutputMapperDirectory string
}

func (o *GenerateMapperOptions) InitDefaults() {

func (o *GenerateMapperOptions) InitDefaults() error {
root, err := options.RepoRoot()
if err != nil {
return nil
}
o.ProtoSourcePath = root + "/dev/tools/proto-to-mapper/build/googleapis.pb"
o.APIGoPackagePath = "github.com/GoogleCloudPlatform/k8s-config-connector/apis/"
o.APIDirectory = root + "/apis/"
o.OutputMapperDirectory = root + "/pkg/controller/direct/"
return nil
}

func (o *GenerateMapperOptions) BindFlags(cmd *cobra.Command) {
Expand All @@ -51,7 +60,10 @@ func BuildCommand(baseOptions *options.GenerateOptions) *cobra.Command {
GenerateOptions: baseOptions,
}

opt.InitDefaults()
if err := opt.InitDefaults(); err != nil {
fmt.Fprintf(os.Stderr, "Error initializing defaults: %v\n", err)
os.Exit(1)
}

cmd := &cobra.Command{
Use: "generate-mapper",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package generatetypes
import (
"context"
"fmt"
"os"
"strings"
"unicode"

Expand All @@ -37,7 +38,14 @@ type GenerateCRDOptions struct {
ResourceProtoName string
}

func (o *GenerateCRDOptions) InitDefaults() {
func (o *GenerateCRDOptions) InitDefaults() error {
root, err := options.RepoRoot()
if err != nil {
return nil
}
o.ProtoSourcePath = root + "/dev/tools/proto-to-mapper/build/googleapis.pb"
o.OutputAPIDirectory = root + "/apis/"
return nil
}

func (o *GenerateCRDOptions) BindFlags(cmd *cobra.Command) {
Expand All @@ -51,7 +59,10 @@ func BuildCommand(baseOptions *options.GenerateOptions) *cobra.Command {
GenerateOptions: baseOptions,
}

opt.InitDefaults()
if err := opt.InitDefaults(); err != nil {
fmt.Fprintf(os.Stderr, "Error initializing defaults: %v\n", err)
os.Exit(1)
}

cmd := &cobra.Command{
Use: "generate-types",
Expand Down
17 changes: 16 additions & 1 deletion dev/tools/controllerbuilder/pkg/options/generateoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package options

import "github.com/spf13/cobra"
import (
"os/exec"
"strings"

"github.com/spf13/cobra"
)

type GenerateOptions struct {
ProtoSourcePath string
Expand All @@ -30,3 +35,13 @@ func (o *GenerateOptions) BindPersistentFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&o.APIVersion, "api-version", "v", o.APIVersion, "the KRM API version. used to import the KRM API")
cmd.PersistentFlags().StringVarP(&o.ServiceName, "service", "s", o.ServiceName, "the GCP service name")
}

func RepoRoot() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.Output()
if err != nil {
return "", err
}
repoRoot := strings.TrimSpace(string(output))
return repoRoot, nil
}

0 comments on commit c78b992

Please sign in to comment.