Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ lint:
format:
go fmt ./...

.PHONY: build install test clean lint format
.PHONY: build install test clean lint format
10 changes: 10 additions & 0 deletions cmd/ctrlc/root/apply/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
func NewApplyCmd() *cobra.Command {
var filePatterns []string
var selectorRaw string
var providerName string

cmd := &cobra.Command{
Use: "apply",
Expand All @@ -46,8 +47,12 @@ func NewApplyCmd() *cobra.Command {

cmd.Flags().StringArrayVarP(&filePatterns, "file", "f", nil, "Path or glob pattern to YAML files (can be specified multiple times, prefix with ! to exclude)")
cmd.Flags().StringVar(&selectorRaw, "selector", "", "Metadata selector in key=value format to apply to created resources")
cmd.Flags().StringVarP(&providerName, "provider", "p", "ctrlc-apply", "Name of the resource provider")
cmd.MarkFlagRequired("file")

viper.BindPFlag("provider", cmd.Flags().Lookup("provider"))
viper.BindEnv("provider", "CTRLPLANE_PROVIDER")
Comment on lines +53 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

cat -n cmd/ctrlc/root/apply/cmd.go | head -80

Repository: ctrlplanedev/cli

Length of output: 3036


🏁 Script executed:

rg -n 'viper\.BindPFlag\(|viper\.BindEnv\(' --type go

Repository: ctrlplanedev/cli

Length of output: 1959


🏁 Script executed:

sed -n '38,55p' cmd/ctrlc/root/sync/salesforce/salesforce.go

Repository: ctrlplanedev/cli

Length of output: 1176


🏁 Script executed:

rg -n 'cobra\.CheckErr' --type go | head -20

Repository: ctrlplanedev/cli

Length of output: 42


🏁 Script executed:

sed -n '20,40p' cmd/ctrlc/ctrlc.go

Repository: ctrlplanedev/cli

Length of output: 973


Handle Viper bind errors instead of ignoring them.

If binding fails, --provider / CTRLPLANE_PROVIDER can silently stop taking effect. The codebase uses panic() for initialization errors (see cmd/ctrlc/root/sync/salesforce/salesforce.go), so use that pattern rather than cobra.CheckErr():

-	viper.BindPFlag("provider", cmd.Flags().Lookup("provider"))
-	viper.BindEnv("provider", "CTRLPLANE_PROVIDER")
+	if err := viper.BindPFlag("provider", cmd.Flags().Lookup("provider")); err != nil {
+		panic(fmt.Errorf("failed to bind provider flag: %w", err))
+	}
+	if err := viper.BindEnv("provider", "CTRLPLANE_PROVIDER"); err != nil {
+		panic(fmt.Errorf("failed to bind provider env: %w", err))
+	}

Note: cmd/ctrlc/ctrlc.go has multiple unhandled BindPFlag/BindEnv calls (lines 21-36+) and should also be fixed for consistency.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
viper.BindPFlag("provider", cmd.Flags().Lookup("provider"))
viper.BindEnv("provider", "CTRLPLANE_PROVIDER")
if err := viper.BindPFlag("provider", cmd.Flags().Lookup("provider")); err != nil {
panic(fmt.Errorf("failed to bind provider flag: %w", err))
}
if err := viper.BindEnv("provider", "CTRLPLANE_PROVIDER"); err != nil {
panic(fmt.Errorf("failed to bind provider env: %w", err))
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/ctrlc/root/apply/cmd.go` around lines 53 - 54, The viper.BindPFlag and
viper.BindEnv calls in cmd/ctrlc/root/apply/cmd.go are currently ignoring
returned errors; change them to check the error and panic on failure (consistent
with the project's initialization error pattern used in
cmd/ctrlc/root/sync/salesforce/salesforce.go) so a binding failure cannot
silently disable --provider/CTRLPLANE_PROVIDER; specifically wrap the calls to
viper.BindPFlag("provider", cmd.Flags().Lookup("provider")) and
viper.BindEnv("provider", "CTRLPLANE_PROVIDER") to capture the error and panic
with a clear message including the returned error, and apply the same defensive
check/panic pattern to the other Viper binds in cmd/ctrlc/ctrlc.go for
consistency.


return cmd
}

Expand All @@ -64,6 +69,7 @@ func runApply(ctx context.Context, filePatterns []string, selectorRaw string) er
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspace := viper.GetString("workspace")
providerName := viper.GetString("provider")

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
Expand Down Expand Up @@ -109,6 +115,10 @@ func runApply(ctx context.Context, filePatterns []string, selectorRaw string) er
for _, ts := range sortedSpecs {
if ts.Type == "Resource" {
if spec, ok := ts.Spec.(*providers.ResourceItemSpec); ok {
if spec.Provider == "" {
log.Debug("Updating resource provider", "from", spec.Provider, "to", providerName)
spec.Provider = providerName
}
resourceSpecs = append(resourceSpecs, spec)
continue
}
Expand Down
3 changes: 3 additions & 0 deletions internal/api/providers/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/avast/retry-go"
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -163,6 +164,7 @@ func BatchUpsertResources(ctx Context, specs []*ResourceItemSpec) []Result {
for _, spec := range specs {
providerName := spec.Provider
if providerName == "" {
log.Debug("Using ctrlc-apply providerName")
providerName = "ctrlc-apply"
}
byProvider[providerName] = append(byProvider[providerName], spec)
Expand Down Expand Up @@ -205,6 +207,7 @@ func BatchUpsertResources(ctx Context, specs []*ResourceItemSpec) []Result {
}

// Single API call for all resources under this provider
log.Debug("Upserting resources", "workspaceID", ctx.WorkspaceIDValue(), "provider", providerName, "providerID", providerID)
resp, err := ctx.APIClient().SetResourceProviderResourcesWithResponse(
ctx.Ctx(), ctx.WorkspaceIDValue(), providerID,
api.SetResourceProviderResourcesJSONRequestBody{Resources: apiResources},
Expand Down