From 2bd59b123ad27c405113797fdeb01f9b898e746b Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Thu, 19 Nov 2026 20:56:44 +0100 Subject: [PATCH 1/2] Add UpdateSchemas to pkg/project Expose resolving a project's dependencies and generating language schemas for them and the project's own APIs - the work dependency update-cache does - so hosts can regenerate a project's models without shelling out. The update-cache command now delegates to it. Signed-off-by: Christopher Haar --- cmd/crossplane/dependency/cache.go | 64 ++++----------------- pkg/project/project.go | 89 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 54 deletions(-) diff --git a/cmd/crossplane/dependency/cache.go b/cmd/crossplane/dependency/cache.go index ee9962a8..45777ae0 100644 --- a/cmd/crossplane/dependency/cache.go +++ b/cmd/crossplane/dependency/cache.go @@ -17,7 +17,6 @@ limitations under the License. package dependency import ( - "cmp" "context" "fmt" "path/filepath" @@ -25,7 +24,6 @@ import ( "github.com/alecthomas/kong" "github.com/spf13/afero" - "github.com/crossplane/crossplane-runtime/v2/pkg/errors" "github.com/crossplane/crossplane-runtime/v2/pkg/logging" "github.com/crossplane/cli/v2/cmd/crossplane/common" @@ -33,11 +31,8 @@ import ( "github.com/crossplane/cli/v2/internal/config" "github.com/crossplane/cli/v2/internal/dependency" "github.com/crossplane/cli/v2/internal/project/projectfile" - "github.com/crossplane/cli/v2/internal/schemas/generator" - "github.com/crossplane/cli/v2/internal/schemas/manager" - "github.com/crossplane/cli/v2/internal/schemas/runner" "github.com/crossplane/cli/v2/internal/terminal" - clixpkg "github.com/crossplane/cli/v2/internal/xpkg" + "github.com/crossplane/cli/v2/pkg/project" _ "embed" ) @@ -73,56 +68,17 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, return err } - client, err := clixpkg.NewClient(clixpkg.WithCacheDir(afero.NewOsFs(), cmp.Or(c.CacheDir, dependency.DefaultCacheDir())), clixpkg.WithImageConfigs(proj.Spec.ImageConfigs)) - if err != nil { - return errors.Wrap(err, "cannot create xpkg client") - } - - generators := generator.Filter( - generator.AllLanguages( - generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), - generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), - ), - proj.Spec.Schemas.GetLanguages(), - ) - - gitOpts := c.ManagerOptions() - opts := make([]dependency.ManagerOption, 0, 3+len(gitOpts)) - opts = append(opts, - dependency.WithProjectFile(c.ProjectFile), - dependency.WithSchemaGenerators(generators), - dependency.WithXpkgClient(client), - ) - opts = append(opts, gitOpts...) - - m := dependency.NewManager(proj, projFS, opts...) - logger.Debug("Updating all dependencies") return sp.WrapAsyncWithSuccessSpinners(func(ch async.EventChannel) error { - if err := m.RefreshAll(ctx, ch); err != nil { - return err - } - - // Also regenerate schemas for the project's own APIs, so the models - // are complete without a build (e.g. for language tooling in - // functions and tests that import them). - apisExist, err := afero.DirExists(projFS, proj.Spec.Paths.APIs) - if err != nil || !apisExist { - return err - } - - ch.SendEvent("Generating project API schemas", async.EventStatusStarted) - schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas) - schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs)) - schemaMgr := manager.New(schemasFS, generators, schemaRunner) - apisFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.APIs) - if _, err := schemaMgr.Generate(ctx, manager.NewFSSource(proj.Spec.Paths.APIs, apisFS)); err != nil { - ch.SendEvent("Generating project API schemas", async.EventStatusFailure) - return errors.Wrap(err, "failed to generate project API schemas") - } - ch.SendEvent("Generating project API schemas", async.EventStatusSuccess) - - return nil + return project.UpdateSchemas(ctx, cfg, proj, projFS, project.UpdateSchemasOptions{ + CacheDir: c.CacheDir, + GitUsername: c.GitUsername, + GitToken: c.GitToken, + GitProxy: c.GitProxy, + GitInsecureHostKey: c.GitInsecureHostKey, + Refresh: true, + EventChannel: ch, + }) }) } diff --git a/pkg/project/project.go b/pkg/project/project.go index d6b2c324..7179887d 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -211,6 +211,95 @@ func Build(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, p // unconditionally. func noopCleanup() {} +// UpdateSchemasOptions configures UpdateSchemas. +type UpdateSchemasOptions struct { + // CacheDir is the xpkg dependency cache directory. Defaults to the + // standard per-user crossplane cache location. + CacheDir string + // GitUsername and GitToken authenticate git-based dependencies over + // HTTPS. + GitUsername string + GitToken string + // GitProxy tunnels git operations via an HTTP CONNECT proxy. + GitProxy string + // GitInsecureHostKey disables SSH host key verification for git-based + // dependencies over SSH. + GitInsecureHostKey bool + // Refresh re-resolves dependency versions and refetches packages even + // when they are cached. + Refresh bool + // EventChannel receives progress events, if set. + EventChannel EventChannel +} + +// UpdateSchemas resolves the project's dependencies and generates language +// schemas for them and for the project's own APIs into the project's schemas +// directory - the same work `crossplane dependency update-cache` does. Hosts +// use it e.g. to regenerate a project's models without a build. +func UpdateSchemas(ctx context.Context, cfg *config.Config, proj *devv1alpha1.Project, projFS afero.Fs, o UpdateSchemasOptions) error { + client, err := clixpkg.NewClient(clixpkg.WithCacheDir(afero.NewOsFs(), cmp.Or(o.CacheDir, dependency.DefaultCacheDir())), clixpkg.WithImageConfigs(proj.Spec.ImageConfigs)) + if err != nil { + return errors.Wrap(err, "cannot create xpkg client") + } + + generators := generator.Filter( + generator.AllLanguages( + generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), + generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), + ), + proj.Spec.Schemas.GetLanguages(), + ) + + opts := []dependency.ManagerOption{ + dependency.WithProjectFile(FileName), + dependency.WithSchemaGenerators(generators), + dependency.WithXpkgClient(client), + } + if o.GitToken != "" { + opts = append(opts, dependency.WithGitAuthProvider(&git.HTTPSAuthProvider{ + Username: o.GitUsername, + Password: o.GitToken, + })) + } + if o.GitProxy != "" { + opts = append(opts, dependency.WithGitProxyURL(o.GitProxy)) + } + if o.GitInsecureHostKey { + opts = append(opts, dependency.WithGitInsecureHostKey(true)) + } + m := dependency.NewManager(proj, projFS, opts...) + + if o.Refresh { + err = m.RefreshAll(ctx, o.EventChannel) + } else { + err = m.AddAll(ctx, o.EventChannel) + } + if err != nil { + return err + } + + // Also generate schemas for the project's own APIs, so the models are + // complete without a build (e.g. for language tooling in functions and + // tests that import them). + apisExist, err := afero.DirExists(projFS, proj.Spec.Paths.APIs) + if err != nil || !apisExist { + return err + } + + o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusStarted) + schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas) + schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs)) + schemaMgr := manager.New(schemasFS, generators, schemaRunner) + apisFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.APIs) + if _, err := schemaMgr.Generate(ctx, manager.NewFSSource(proj.Spec.Paths.APIs, apisFS)); err != nil { + o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusFailure) + return errors.Wrap(err, "failed to generate project API schemas") + } + o.EventChannel.SendEvent("Generating project API schemas", async.EventStatusSuccess) + + return nil +} + // SchemaSource is a source of resources (CRDs/XRDs) for which language // schemas can be generated. type SchemaSource = manager.Source From fa8b4c31a0cf7ba06097950721bfbd73316dbcf8 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Thu, 19 Nov 2026 21:06:06 +0100 Subject: [PATCH 2/2] Export the dependency subcommand types for library consumers Host CLIs embedding the dependency commands can mount them in their own command trees and wrap them, e.g. update-cache to generate additional schemas after the update, the same way the project subcommands are exported. Signed-off-by: Christopher Haar --- cmd/crossplane/dependency/add.go | 11 ++++++----- cmd/crossplane/dependency/cache.go | 20 ++++++++++++-------- cmd/crossplane/dependency/dependency.go | 6 +++--- cmd/crossplane/dependency/dependency_test.go | 4 ++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/crossplane/dependency/add.go b/cmd/crossplane/dependency/add.go index e9a7d170..2dd75771 100644 --- a/cmd/crossplane/dependency/add.go +++ b/cmd/crossplane/dependency/add.go @@ -43,8 +43,8 @@ import ( //go:embed help/add.md var addHelp string -// addCmd adds a dependency to the current project. -type addCmd struct { +// AddCmd adds a dependency to the current project. +type AddCmd struct { common.GitFlags Package string `arg:"" help:"Package to add (xpkg OCI reference, k8s:, git repository URL, or HTTP(S) URL)."` @@ -57,12 +57,13 @@ type addCmd struct { GitPath string `help:"Path to CRDs in the git repository." name:"git-path"` } -func (c *addCmd) Help() string { +// Help returns help for the add command. +func (c *AddCmd) Help() string { return addHelp } // Run executes the add command. -func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { +func (c *AddCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { ctx := context.Background() projFilePath, err := filepath.Abs(c.ProjectFile) @@ -111,7 +112,7 @@ func (c *addCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *con }) } -func (c *addCmd) buildDependency() (v1alpha1.Dependency, error) { +func (c *AddCmd) buildDependency() (v1alpha1.Dependency, error) { // k8s dependency: k8s:vX.Y.Z if version, found := strings.CutPrefix(c.Package, "k8s:"); found { if version == "" { diff --git a/cmd/crossplane/dependency/cache.go b/cmd/crossplane/dependency/cache.go index 45777ae0..e575b72e 100644 --- a/cmd/crossplane/dependency/cache.go +++ b/cmd/crossplane/dependency/cache.go @@ -40,20 +40,23 @@ import ( //go:embed help/update-cache.md var updateHelp string -// updateCacheCmd updates the dependency cache by regenerating all schemas. -type updateCacheCmd struct { +// UpdateCacheCmd updates the dependency cache by regenerating all schemas. +// It is exported so host CLIs embedding the dependency commands can wrap it, +// e.g. to generate additional schemas after the update. +type UpdateCacheCmd struct { common.GitFlags ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"` CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"` } -func (c *updateCacheCmd) Help() string { +// Help returns help for the update-cache command. +func (c *UpdateCacheCmd) Help() string { return updateHelp } // Run executes the update-cache command. -func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { +func (c *UpdateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, cfg *config.Config) error { ctx := context.Background() projFilePath, err := filepath.Abs(c.ProjectFile) @@ -85,19 +88,20 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, //go:embed help/clean-cache.md var cleanHelp string -// cleanCacheCmd removes all generated schemas. -type cleanCacheCmd struct { +// CleanCacheCmd removes all generated schemas. +type CleanCacheCmd struct { ProjectFile string `default:"${project_file}" help:"Path to project definition file." short:"f"` CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents." name:"cache-dir"` KeepPackages bool `help:"Keep cached xpkg package contents; remove only generated schemas." name:"keep-packages"` } -func (c *cleanCacheCmd) Help() string { +// Help returns help for the clean-cache command. +func (c *CleanCacheCmd) Help() string { return cleanHelp } // Run executes the clean-cache command. -func (c *cleanCacheCmd) Run(k *kong.Context, _ logging.Logger) error { +func (c *CleanCacheCmd) Run(k *kong.Context, _ logging.Logger) error { projFilePath, err := filepath.Abs(c.ProjectFile) if err != nil { return err diff --git a/cmd/crossplane/dependency/dependency.go b/cmd/crossplane/dependency/dependency.go index 75e30eb1..d1f58917 100644 --- a/cmd/crossplane/dependency/dependency.go +++ b/cmd/crossplane/dependency/dependency.go @@ -19,7 +19,7 @@ package dependency // Cmd contains commands for dependency management. type Cmd struct { - Add addCmd `cmd:"" help:"Add a dependency to the current project." novale:"Google.WordList[\"k8s\"],Google.Units,gitlab.Units"` - UpdateCache updateCacheCmd `cmd:"" help:"Update the dependency cache for the current project."` - CleanCache cleanCacheCmd `cmd:"" help:"Clean the dependency cache."` + Add AddCmd `cmd:"" help:"Add a dependency to the current project." novale:"Google.WordList[\"k8s\"],Google.Units,gitlab.Units"` + UpdateCache UpdateCacheCmd `cmd:"" help:"Update the dependency cache for the current project."` + CleanCache CleanCacheCmd `cmd:"" help:"Clean the dependency cache."` } diff --git a/cmd/crossplane/dependency/dependency_test.go b/cmd/crossplane/dependency/dependency_test.go index 3ffa6c52..a5c4ecb6 100644 --- a/cmd/crossplane/dependency/dependency_test.go +++ b/cmd/crossplane/dependency/dependency_test.go @@ -38,14 +38,14 @@ import ( func TestAddGitAuth(t *testing.T) { testDependencyGitAuth(t, func(serverURL, projectFile, cacheDir, token string) error { - c := &addCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}} + c := &AddCmd{Package: serverURL + "/repo.git", GitRef: "main", ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}} return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{}) }) } func TestUpdateCacheGitAuth(t *testing.T) { testDependencyGitAuth(t, func(_, projectFile, cacheDir, token string) error { - c := &updateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}} + c := &UpdateCacheCmd{ProjectFile: projectFile, CacheDir: cacheDir, GitFlags: common.GitFlags{GitToken: token, GitUsername: "test-user"}} return c.Run(logging.NewNopLogger(), terminal.NewSpinnerPrinter(io.Discard, false), &config.Config{}) }) }