|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package exportcsv |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/options" |
| 24 | + "github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/toolbot" |
| 25 | + |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +// ExportCSVOptions are the options for the export-csv command. |
| 30 | +type ExportCSVOptions struct { |
| 31 | + *options.GenerateOptions |
| 32 | + |
| 33 | + ProtoDir string |
| 34 | + SrcDir string |
| 35 | + OutputDir string |
| 36 | +} |
| 37 | + |
| 38 | +// BindFlags binds the flags to the command. |
| 39 | +func (o *ExportCSVOptions) BindFlags(cmd *cobra.Command) { |
| 40 | + cmd.Flags().StringVar(&o.ProtoDir, "proto-dir", o.ProtoDir, "base directory for checkout of proto API definitions") |
| 41 | + cmd.Flags().StringVar(&o.SrcDir, "src-dir", o.SrcDir, "base directory for source code") |
| 42 | + cmd.Flags().StringVar(&o.OutputDir, "output-dir", o.OutputDir, "base directory for writing CSVs") |
| 43 | +} |
| 44 | + |
| 45 | +// BuildCommand builds the export-csv command. |
| 46 | +func BuildCommand(baseOptions *options.GenerateOptions) *cobra.Command { |
| 47 | + opt := &ExportCSVOptions{ |
| 48 | + GenerateOptions: baseOptions, |
| 49 | + } |
| 50 | + |
| 51 | + cmd := &cobra.Command{ |
| 52 | + Use: "export-csv", |
| 53 | + Short: "generate CSV from tool annotations", |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + ctx := cmd.Context() |
| 56 | + if err := RunExportCSV(ctx, opt); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + return nil |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + opt.BindFlags(cmd) |
| 64 | + |
| 65 | + return cmd |
| 66 | +} |
| 67 | + |
| 68 | +// rewriteFilePath rewrites the file path to the user's home directory if it starts with "~". |
| 69 | +func rewriteFilePath(p *string) error { |
| 70 | + if strings.HasPrefix(*p, "~/") { |
| 71 | + homeDir, err := os.UserHomeDir() |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("getting home directory: %w", err) |
| 74 | + } |
| 75 | + *p = strings.Replace(*p, "~", homeDir, 1) |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// RunExportCSV runs the export-csv command. |
| 81 | +func RunExportCSV(ctx context.Context, o *ExportCSVOptions) error { |
| 82 | + if err := rewriteFilePath(&o.ProtoDir); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + if o.ProtoDir == "" { |
| 87 | + return fmt.Errorf("--proto-dir is required") |
| 88 | + } |
| 89 | + if o.SrcDir == "" { |
| 90 | + return fmt.Errorf("--src-dir is required") |
| 91 | + } |
| 92 | + if o.OutputDir == "" { |
| 93 | + return fmt.Errorf("--output-dir is required") |
| 94 | + } |
| 95 | + |
| 96 | + extractor := &toolbot.ExtractToolMarkers{} |
| 97 | + addProtoDefinition, err := toolbot.NewEnhanceWithProtoDefinition(o.ProtoDir) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + x, err := toolbot.NewCSVExporter(extractor, addProtoDefinition) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + if err := x.VisitCodeDir(ctx, o.SrcDir); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + if err := x.WriteCSVForAllTools(ctx, o.OutputDir); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
0 commit comments