From 69cf0b2ccb765a42432fe15b6c102ec9cba0123a Mon Sep 17 00:00:00 2001 From: Daniel Barnes Date: Tue, 3 Mar 2026 05:42:00 +0900 Subject: [PATCH] feat(apply): allow provider as a flag --- Makefile | 2 +- cmd/ctrlc/root/apply/cmd.go | 10 ++++++++++ internal/api/providers/resource.go | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9bc2f40..cee5c48 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,4 @@ lint: format: go fmt ./... -.PHONY: build install test clean lint format \ No newline at end of file +.PHONY: build install test clean lint format diff --git a/cmd/ctrlc/root/apply/cmd.go b/cmd/ctrlc/root/apply/cmd.go index 687ee25..2c299cf 100644 --- a/cmd/ctrlc/root/apply/cmd.go +++ b/cmd/ctrlc/root/apply/cmd.go @@ -20,6 +20,7 @@ import ( func NewApplyCmd() *cobra.Command { var filePatterns []string var selectorRaw string + var providerName string cmd := &cobra.Command{ Use: "apply", @@ -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") + return cmd } @@ -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 { @@ -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 } diff --git a/internal/api/providers/resource.go b/internal/api/providers/resource.go index 16f1cb1..83d271e 100644 --- a/internal/api/providers/resource.go +++ b/internal/api/providers/resource.go @@ -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" ) @@ -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) @@ -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},