From 66b47108596eb458ac6b648a0337630c27ec792b Mon Sep 17 00:00:00 2001 From: Jingyi Hu Date: Fri, 13 Dec 2024 22:02:45 +0000 Subject: [PATCH] tool: make insert field a subcommand of update-types --- .../pkg/commands/updatetypes/insertcommand.go | 90 +++++++++++++++++++ .../updatetypes/updatetypescommand.go | 44 ++------- dev/tools/controllerbuilder/update.sh | 4 +- docs/develop-resources/scenarios/new-field.md | 10 +-- 4 files changed, 104 insertions(+), 44 deletions(-) create mode 100644 dev/tools/controllerbuilder/pkg/commands/updatetypes/insertcommand.go diff --git a/dev/tools/controllerbuilder/pkg/commands/updatetypes/insertcommand.go b/dev/tools/controllerbuilder/pkg/commands/updatetypes/insertcommand.go new file mode 100644 index 00000000000..55837d2ad2b --- /dev/null +++ b/dev/tools/controllerbuilder/pkg/commands/updatetypes/insertcommand.go @@ -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() +} diff --git a/dev/tools/controllerbuilder/pkg/commands/updatetypes/updatetypescommand.go b/dev/tools/controllerbuilder/pkg/commands/updatetypes/updatetypescommand.go index 7dd83e6494c..dfe8a913208 100644 --- a/dev/tools/controllerbuilder/pkg/commands/updatetypes/updatetypescommand.go +++ b/dev/tools/controllerbuilder/pkg/commands/updatetypes/updatetypescommand.go @@ -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 @@ -45,9 +41,7 @@ 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") @@ -55,10 +49,9 @@ func (o *UpdateTypeOptions) BindFlags(cmd *cobra.Command) { } 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) @@ -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 } diff --git a/dev/tools/controllerbuilder/update.sh b/dev/tools/controllerbuilder/update.sh index 7aac058e515..02aaee4d617 100755 --- a/dev/tools/controllerbuilder/update.sh +++ b/dev/tools/controllerbuilder/update.sh @@ -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" diff --git a/docs/develop-resources/scenarios/new-field.md b/docs/develop-resources/scenarios/new-field.md index b32c7d665ce..f10983f85e2 100644 --- a/docs/develop-resources/scenarios/new-field.md +++ b/docs/develop-resources/scenarios/new-field.md @@ -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`