bootstrap.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright © The ESO Authors
  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. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "os"
  17. "regexp"
  18. "strings"
  19. "github.com/spf13/cobra"
  20. "github.com/external-secrets/external-secrets/cmd/esoctl/generator"
  21. )
  22. var (
  23. generatorName string
  24. generatorDescription string
  25. generatorPackage string
  26. )
  27. func init() {
  28. bootstrapCmd.AddCommand(bootstrapGeneratorCmd)
  29. bootstrapGeneratorCmd.Flags().StringVar(&generatorName, "name", "", "Name of the generator (e.g., MyGenerator)")
  30. bootstrapGeneratorCmd.Flags().StringVar(&generatorDescription, "description", "", "Description of the generator")
  31. bootstrapGeneratorCmd.Flags().StringVar(&generatorPackage, "package", "", "Package name (default: lowercase of name)")
  32. _ = bootstrapGeneratorCmd.MarkFlagRequired("name")
  33. }
  34. var bootstrapCmd = &cobra.Command{
  35. Use: "bootstrap",
  36. Short: "Bootstrap new resources for external-secrets",
  37. Long: `Bootstrap new resources like generators for external-secrets operator.`,
  38. Run: func(cmd *cobra.Command, _ []string) {
  39. _ = cmd.Usage()
  40. },
  41. }
  42. var bootstrapGeneratorCmd = &cobra.Command{
  43. Use: "generator",
  44. Short: "Bootstrap a new generator",
  45. Long: `Bootstrap a new generator with CRD definition and provider implementation.`,
  46. RunE: bootstrapGeneratorRun,
  47. }
  48. func bootstrapGeneratorRun(_ *cobra.Command, _ []string) error {
  49. // Validate generator name
  50. if !regexp.MustCompile(`^[A-Z][a-zA-Z0-9]*$`).MatchString(generatorName) {
  51. return fmt.Errorf("generator name must be PascalCase and start with an uppercase letter")
  52. }
  53. // Set default package name if not provided
  54. if generatorPackage == "" {
  55. generatorPackage = strings.ToLower(generatorName)
  56. }
  57. // Set default description if not provided
  58. if generatorDescription == "" {
  59. generatorDescription = fmt.Sprintf("%s generator", generatorName)
  60. }
  61. // Get root directory
  62. wd, err := os.Getwd()
  63. if err != nil {
  64. return fmt.Errorf("failed to get working directory: %w", err)
  65. }
  66. // Try to find the root directory
  67. rootDir := generator.FindRootDir(wd)
  68. if rootDir == "" {
  69. return fmt.Errorf("could not find repository root directory")
  70. }
  71. // Create generator configuration
  72. cfg := generator.Config{
  73. GeneratorName: generatorName,
  74. PackageName: generatorPackage,
  75. Description: generatorDescription,
  76. GeneratorKind: "GeneratorKind" + generatorName,
  77. }
  78. // Bootstrap the generator
  79. if err := generator.Bootstrap(rootDir, cfg); err != nil {
  80. return err
  81. }
  82. fmt.Printf("✓ Successfully bootstrapped generator: %s\n", generatorName)
  83. fmt.Printf("\nNext steps:\n")
  84. fmt.Printf("1. Review and customize: apis/generators/v1alpha1/types_%s.go\n", generatorPackage)
  85. fmt.Printf("2. Implement the generator logic in: generators/v1/%s/%s.go\n", generatorPackage, generatorPackage)
  86. fmt.Printf("3. Run: go mod tidy\n")
  87. fmt.Printf("4. Run: make generate\n")
  88. fmt.Printf("5. Run: make manifests\n")
  89. fmt.Printf("6. Add tests for your generator\n")
  90. return nil
  91. }