Skip to content

Commit

Permalink
tool: make insert field a subcommand of update-types
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyih committed Dec 17, 2024
1 parent 34f68d4 commit 66b4710
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2024 Google LLC
//
// 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 updatetypes

import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/typeupdater"
"github.com/spf13/cobra"
)

type insertFieldOptions struct {
*baseUpdateTypeOptions

parent string // Fully qualified name of the proto message holding the new field
field string // Name of the field to be inserted
}

func buildInsertCommand(baseOptions *baseUpdateTypeOptions) *cobra.Command {
opt := &insertFieldOptions{
baseUpdateTypeOptions: baseOptions,
}

cmd := &cobra.Command{
Use: "insert",
Short: "insert a new field and all of its dependent messages into KRM types",
PreRunE: validateInsertOptions(opt),
RunE: runInsert(opt),
}

bindInsertFlags(cmd, opt)

return cmd
}

func bindInsertFlags(cmd *cobra.Command, opt *insertFieldOptions) {
opt.BindFlags(cmd)
cmd.Flags().StringVar(&opt.parent, "parent", opt.parent, "Fully qualified name of the proto message holding the new field. e.g. `google.cloud.bigquery.datatransfer.v1.TransferConfig`")
cmd.Flags().StringVar(&opt.field, "field", opt.field, "Name of the field to be inserted, e.g. `schedule_options_v2`")
}

func validateInsertOptions(opt *insertFieldOptions) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
if opt.apiDirectory == "" {
return fmt.Errorf("--api-dir is required")
}
if opt.parent == "" {
return fmt.Errorf("--parent is required")
}
if opt.field == "" {
return fmt.Errorf("--field is required")
}
return nil
}
}

func runInsert(opt *insertFieldOptions) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if err := runFieldInserter(ctx, opt); err != nil {
return err
}
return nil
}
}

func runFieldInserter(ctx context.Context, opt *insertFieldOptions) error {
fieldInserter := typeupdater.NewFieldInserter(&typeupdater.InsertFieldOptions{
ProtoSourcePath: opt.GenerateOptions.ProtoSourcePath,
ParentMessageFullName: opt.parent,
FieldToInsert: opt.field,
IgnoredFields: opt.ignoredFields,
APIDirectory: opt.apiDirectory,
GoPackagePath: opt.apiGoPackagePath,
})
return fieldInserter.Run()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,23 @@
package updatetypes

import (
"context"
"fmt"
"os"

"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/options"
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/typeupdater"

"github.com/spf13/cobra"
)

type UpdateTypeOptions struct {
type baseUpdateTypeOptions struct {
*options.GenerateOptions

parentMessage string // The fully qualified name of the parent proto message of the field to be inserted
insertField string
ignoredFields string // TODO: could be part of GenerateOptions
apiDirectory string
apiGoPackagePath string
}

func (o *UpdateTypeOptions) InitDefaults() error {
func (o *baseUpdateTypeOptions) InitDefaults() error {
root, err := options.RepoRoot()
if err != nil {
return err
Expand All @@ -45,20 +41,17 @@ func (o *UpdateTypeOptions) InitDefaults() error {
return nil
}

func (o *UpdateTypeOptions) BindFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.parentMessage, "parent", o.parentMessage, "Fully qualified name of the proto message holding the new field. e.g. `google.cloud.bigquery.datatransfer.v1.TransferConfig`")
cmd.Flags().StringVar(&o.insertField, "insert-field", o.insertField, "Name of the new field to be inserted, e.g. `schedule_options_v2`")
func (o *baseUpdateTypeOptions) BindFlags(cmd *cobra.Command) {
// TODO: Update this flag to accept a file path pointing to the ignored fields YAML file.
cmd.Flags().StringVar(&o.ignoredFields, "ignored-fields", o.ignoredFields, "Comma-separated list of fields to ignore")
cmd.Flags().StringVar(&o.apiDirectory, "api-dir", o.apiDirectory, "Base directory for APIs")
cmd.Flags().StringVar(&o.apiGoPackagePath, "api-go-package-path", o.apiGoPackagePath, "API Go package path")
}

func BuildCommand(baseOptions *options.GenerateOptions) *cobra.Command {
opt := &UpdateTypeOptions{
opt := &baseUpdateTypeOptions{
GenerateOptions: baseOptions,
}

if err := opt.InitDefaults(); err != nil {
fmt.Fprintf(os.Stderr, "Error initializing defaults: %v\n", err)
os.Exit(1)
Expand All @@ -67,35 +60,12 @@ func BuildCommand(baseOptions *options.GenerateOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "update-types",
Short: "update KRM types for a proto service",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if err := runFieldInserter(ctx, opt); err != nil {
return err
}
return nil
},
}

opt.BindFlags(cmd)

return cmd
}

func runFieldInserter(ctx context.Context, opt *UpdateTypeOptions) error {
if opt.apiDirectory == "" {
return fmt.Errorf("--api-dir is required")
}
// subcommands
cmd.AddCommand(buildInsertCommand(opt))

fieldInserter := typeupdater.NewFieldInserter(&typeupdater.InsertFieldOptions{
ProtoSourcePath: opt.GenerateOptions.ProtoSourcePath,
ParentMessageFullName: opt.parentMessage,
FieldToInsert: opt.insertField,
IgnoredFields: opt.ignoredFields,
APIDirectory: opt.apiDirectory,
GoPackagePath: opt.apiGoPackagePath,
})
if err := fieldInserter.Run(); err != nil {
return err
}
return nil
return cmd
}
4 changes: 2 additions & 2 deletions dev/tools/controllerbuilder/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ REPO_ROOT="$(git rev-parse --show-toplevel)"
cd ${REPO_ROOT}/dev/tools/controllerbuilder

# example usage
go run . update-types \
go run . update-types insert \
--parent "google.monitoring.dashboard.v1.Dashboard" \
--insert-field "row_layout" \
--field "row_layout" \
--api-dir ${REPO_ROOT}/apis/monitoring/v1beta1 \
--ignored-fields "google.monitoring.dashboard.v1.PickTimeSeriesFilter.interval"
10 changes: 5 additions & 5 deletions docs/develop-resources/scenarios/new-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ Run the following command. This should add the new field and all of its dependen
REPO_ROOT="$(git rev-parse --show-toplevel)"
cd $REPO_ROOT/dev/tools/controllerbuilder

go run . update-types \
go run . update-types insert \
--parent "google.monitoring.dashboard.v1.Dashboard" \
--insert-field "row_layout" \
--field "row_layout" \
--api-dir ${REPO_ROOT}/apis/monitoring/v1beta1
```

* `--parent-message`
* `--parent`

Fully qualified name of the proto message holding the new field.

* `--field-to-insert`
* `--field`

Name of the new proto field to be inserted.
Name of the proto field to be inserted under the parent message.

* `--api-dir`

Expand Down

0 comments on commit 66b4710

Please sign in to comment.