Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apply command #110

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions cmd/other/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package other

import (
"encoding/json"
"fmt"
"os"

"github.com/cloudforet-io/cfctl/pkg/transport"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

type ResourceSpec struct {
Service string `yaml:"service"`
Verb string `yaml:"verb"`
Resource string `yaml:"resource"`
Spec map[string]interface{} `yaml:"spec"`
}

// ApplyCmd represents the apply command
var ApplyCmd = &cobra.Command{
Use: "apply",
Short: "Apply a configuration to a resource using a file",
Long: `Apply the configuration in the YAML file to create or update a resource`,
Example: ` # Create test.yaml
service: identity
verb: create
resource: user
spec:
user_id: test-user
auth_type: LOCAL
# Apply the configuration in test.yaml
$ cfctl apply -f test.yaml`,
RunE: func(cmd *cobra.Command, args []string) error {
filename, _ := cmd.Flags().GetString("filename")
if filename == "" {
return fmt.Errorf("filename is required (-f flag)")
}

// Read and parse YAML file
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

var resource ResourceSpec
if err := yaml.Unmarshal(data, &resource); err != nil {
return fmt.Errorf("failed to parse YAML: %v", err)
}

// Convert spec to parameters
var parameters []string
for key, value := range resource.Spec {
switch v := value.(type) {
case string:
parameters = append(parameters, fmt.Sprintf("%s=%s", key, v))
case bool, int, float64:
parameters = append(parameters, fmt.Sprintf("%s=%v", key, v))
case []interface{}, map[string]interface{}:
// For arrays and maps, convert to JSON string
jsonBytes, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("failed to marshal parameter %s: %v", key, err)
}
parameters = append(parameters, fmt.Sprintf("%s=%s", key, string(jsonBytes)))
default:
// For other complex types, try JSON marshaling
jsonBytes, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("failed to marshal parameter %s: %v", key, err)
}
parameters = append(parameters, fmt.Sprintf("%s=%s", key, string(jsonBytes)))
}
}

options := &transport.FetchOptions{
Parameters: parameters,
}

_, err = transport.FetchService(resource.Service, resource.Verb, resource.Resource, options)
if err != nil {
pterm.Error.Println(err.Error())
return nil
}

pterm.Success.Printf("Resource %s/%s applied successfully\n", resource.Service, resource.Resource)
return nil
},
}

func init() {
ApplyCmd.Flags().StringP("filename", "f", "", "Filename to use to apply the resource")
ApplyCmd.MarkFlagRequired("filename")
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func init() {
rootCmd.AddCommand(other.SettingCmd)
rootCmd.AddCommand(other.LoginCmd)
rootCmd.AddCommand(other.AliasCmd)
rootCmd.AddCommand(other.ApplyCmd)

// Set default group for commands without a group
for _, cmd := range rootCmd.Commands() {
Expand Down
Loading