|
| 1 | +/* |
| 2 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package other |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/cloudforet-io/cfctl/pkg/transport" |
| 12 | + "github.com/pterm/pterm" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +type ResourceSpec struct { |
| 18 | + Service string `yaml:"service"` |
| 19 | + Verb string `yaml:"verb"` |
| 20 | + Resource string `yaml:"resource"` |
| 21 | + Spec map[string]interface{} `yaml:"spec"` |
| 22 | +} |
| 23 | + |
| 24 | +// ApplyCmd represents the apply command |
| 25 | +var ApplyCmd = &cobra.Command{ |
| 26 | + Use: "apply", |
| 27 | + Short: "Apply a configuration to a resource using a file", |
| 28 | + Long: `Apply the configuration in the YAML file to create or update a resource`, |
| 29 | + Example: ` # Create test.yaml |
| 30 | + service: identity |
| 31 | + verb: create |
| 32 | + resource: user |
| 33 | + spec: |
| 34 | + user_id: test-user |
| 35 | + auth_type: LOCAL |
| 36 | +
|
| 37 | + # Apply the configuration in test.yaml |
| 38 | + $ cfctl apply -f test.yaml`, |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + filename, _ := cmd.Flags().GetString("filename") |
| 41 | + if filename == "" { |
| 42 | + return fmt.Errorf("filename is required (-f flag)") |
| 43 | + } |
| 44 | + |
| 45 | + // Read and parse YAML file |
| 46 | + data, err := os.ReadFile(filename) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to read file: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + var resource ResourceSpec |
| 52 | + if err := yaml.Unmarshal(data, &resource); err != nil { |
| 53 | + return fmt.Errorf("failed to parse YAML: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Convert spec to parameters |
| 57 | + var parameters []string |
| 58 | + for key, value := range resource.Spec { |
| 59 | + switch v := value.(type) { |
| 60 | + case string: |
| 61 | + parameters = append(parameters, fmt.Sprintf("%s=%s", key, v)) |
| 62 | + case bool, int, float64: |
| 63 | + parameters = append(parameters, fmt.Sprintf("%s=%v", key, v)) |
| 64 | + case []interface{}, map[string]interface{}: |
| 65 | + // For arrays and maps, convert to JSON string |
| 66 | + jsonBytes, err := json.Marshal(v) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to marshal parameter %s: %v", key, err) |
| 69 | + } |
| 70 | + parameters = append(parameters, fmt.Sprintf("%s=%s", key, string(jsonBytes))) |
| 71 | + default: |
| 72 | + // For other complex types, try JSON marshaling |
| 73 | + jsonBytes, err := json.Marshal(v) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to marshal parameter %s: %v", key, err) |
| 76 | + } |
| 77 | + parameters = append(parameters, fmt.Sprintf("%s=%s", key, string(jsonBytes))) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + options := &transport.FetchOptions{ |
| 82 | + Parameters: parameters, |
| 83 | + } |
| 84 | + |
| 85 | + _, err = transport.FetchService(resource.Service, resource.Verb, resource.Resource, options) |
| 86 | + if err != nil { |
| 87 | + pterm.Error.Println(err.Error()) |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + pterm.Success.Printf("Resource %s/%s applied successfully\n", resource.Service, resource.Resource) |
| 92 | + return nil |
| 93 | + }, |
| 94 | +} |
| 95 | + |
| 96 | +func init() { |
| 97 | + ApplyCmd.Flags().StringP("filename", "f", "", "Filename to use to apply the resource") |
| 98 | + ApplyCmd.MarkFlagRequired("filename") |
| 99 | +} |
0 commit comments