Skip to content

Commit

Permalink
add more option for release creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 13, 2024
1 parent b227934 commit 9f1c5d9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 43 deletions.
5 changes: 0 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

Expand Down
24 changes: 22 additions & 2 deletions cmd/ctrlc/root/api/create/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package release

import (
"fmt"
"time"

"github.com/MakeNowJust/heredoc/v2"
"github.com/ctrlplanedev/cli/internal/api"
Expand All @@ -14,7 +15,9 @@ func NewReleaseCmd() *cobra.Command {
var versionFlag string
var deploymentID string
var metadata map[string]string

var configArray map[string]string
var createdAt string
var name string
cmd := &cobra.Command{
Use: "release [flags]",
Short: "Create a new release",
Expand All @@ -34,10 +37,23 @@ func NewReleaseCmd() *cobra.Command {
return fmt.Errorf("failed to create API client: %w", err)
}

var parsedTime *time.Time
if createdAt != "" {
t, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
return fmt.Errorf("failed to parse created_at time: %w", err)
}
parsedTime = &t
}

config := cliutil.ConvertConfigArrayToNestedMap(configArray)
resp, err := client.CreateRelease(cmd.Context(), api.CreateReleaseJSONRequestBody{
Version: versionFlag,
DeploymentId: deploymentID,
Metadata: &metadata,
CreatedAt: parsedTime,
Config: &config,
Name: &name,
})
if err != nil {
return fmt.Errorf("failed to create release: %w", err)
Expand All @@ -49,8 +65,12 @@ func NewReleaseCmd() *cobra.Command {

// Add flags
cmd.Flags().StringVar(&versionFlag, "version", "", "Version of the release (required)")
cmd.Flags().StringVar(&deploymentID, "deployment-id", "", "ID of the deployment (required)")
cmd.Flags().StringVar(&deploymentID, "deployment", "", "ID of the deployment (required)")
cmd.Flags().StringToStringVar(&metadata, "metadata", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)")
cmd.Flags().StringToStringVar(&configArray, "config", make(map[string]string), "Config key-value pairs with nested values (can be specified multiple times)")
cmd.Flags().StringVar(&createdAt, "created-at", "", "Created at timestamp (e.g. --created-at 2024-01-01T00:00:00Z) for the release channel")
cmd.Flags().StringVar(&name, "name", "", "Name of the release channel")

cmd.MarkFlagRequired("version")
cmd.MarkFlagRequired("deployment-id")

Expand Down
34 changes: 1 addition & 33 deletions cmd/ctrlc/root/api/create/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package resources

import (
"fmt"
"strconv"
"strings"

"github.com/MakeNowJust/heredoc/v2"
"github.com/ctrlplanedev/cli/internal/api"
Expand Down Expand Up @@ -42,37 +40,7 @@ func NewResourcesCmd() *cobra.Command {
}

// Convert configArray into a nested map[string]interface{}
config := make(map[string]interface{})
for path, value := range configArray {
// Split path into segments
segments := strings.Split(path, ".")

// Start at the root map
current := config

// Navigate through segments
for i, segment := range segments {
if i == len(segments)-1 {
// Last segment - try to convert string to number or boolean if possible
if num, err := strconv.ParseFloat(value, 64); err == nil {
current[segment] = num
} else if value == "true" {
current[segment] = true
} else if value == "false" {
current[segment] = false
} else {
current[segment] = value
}
} else {
// Create nested map if it doesn't exist
if _, exists := current[segment]; !exists {
current[segment] = make(map[string]interface{})
}
// Move to next level
current = current[segment].(map[string]interface{})
}
}
}
config := cliutil.ConvertConfigArrayToNestedMap(configArray)

// Extrat into vars
resp, err := client.UpsertTargets(cmd.Context(), api.UpsertTargetsJSONRequestBody{
Expand Down
23 changes: 20 additions & 3 deletions internal/api/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions internal/cliutil/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cliutil

import (
"strconv"
"strings"
)

// ConvertConfigArrayToNestedMap converts a flat key-value map with dot notation
// paths into a nested map[string]interface{} structure. It handles conversion
// of string values to numbers and booleans where appropriate.
func ConvertConfigArrayToNestedMap(configArray map[string]string) map[string]interface{} {
config := make(map[string]interface{})
for path, value := range configArray {
segments := strings.Split(path, ".")
current := config
for i, segment := range segments {
if i == len(segments)-1 {
if num, err := strconv.ParseFloat(value, 64); err == nil {
current[segment] = num
} else if value == "true" {
current[segment] = true
} else if value == "false" {
current[segment] = false
} else {
current[segment] = value
}
} else {
// Create nested map if it doesn't exist
if _, exists := current[segment]; !exists {
current[segment] = make(map[string]interface{})
}
// Move to next level
current = current[segment].(map[string]interface{})
}
}
}
return config
}

0 comments on commit 9f1c5d9

Please sign in to comment.