diff --git a/blueprint.cue b/blueprint.cue index 053be8ff..ba274811 100644 --- a/blueprint.cue +++ b/blueprint.cue @@ -1,6 +1,13 @@ version: "1.0" global: { ci: { + local: [ + "^check.*$", + "^build.*$", + "^test.*$", + "^release.*$", + "^publish.*$", + ] registries: [ ci.providers.aws.registry, ] diff --git a/blueprint/schema/_embed/schema.cue b/blueprint/schema/_embed/schema.cue index d7a41876..e7c858dd 100644 --- a/blueprint/schema/_embed/schema.cue +++ b/blueprint/schema/_embed/schema.cue @@ -44,6 +44,9 @@ package schema // CI contains the configuration for the CI system. #GlobalCI: { + // Local defines the filters to use when simulating a local CI run. + local: [...string] @go(Local,[]string) + // Providers contains the configuration for the providers being used by the CI system. // +optional providers?: #Providers @go(Providers) diff --git a/blueprint/schema/schema.go b/blueprint/schema/schema.go index ad6b1f21..f665e474 100644 --- a/blueprint/schema/schema.go +++ b/blueprint/schema/schema.go @@ -52,6 +52,9 @@ type Project struct { // CI contains the configuration for the CI system. type GlobalCI struct { + // Local defines the filters to use when simulating a local CI run. + Local []string `json:"local"` + // Providers contains the configuration for the providers being used by the CI system. // +optional Providers Providers `json:"providers"` diff --git a/blueprint/schema/schema_go_gen.cue b/blueprint/schema/schema_go_gen.cue index 68cf0890..a6407ef8 100644 --- a/blueprint/schema/schema_go_gen.cue +++ b/blueprint/schema/schema_go_gen.cue @@ -47,6 +47,9 @@ package schema // CI contains the configuration for the CI system. #GlobalCI: { + // Local defines the filters to use when simulating a local CI run. + local: [...string] @go(Local,[]string) + // Providers contains the configuration for the providers being used by the CI system. // +optional providers?: #Providers @go(Providers) diff --git a/forge/cli/cmd/cmds/ci.go b/forge/cli/cmd/cmds/ci.go index da396e0d..7a378555 100644 --- a/forge/cli/cmd/cmds/ci.go +++ b/forge/cli/cmd/cmds/ci.go @@ -8,7 +8,7 @@ import ( type CICmd struct { Artifact string `short:"a" help:"Dump all produced artifacts to the given path."` - Path string `arg:"" help:"The path to scan from."` + Path string `arg:"" default:"" help:"The path to scan from."` Platform []string `short:"p" help:"Run the target with the given platform."` } @@ -18,6 +18,5 @@ func (c *CICmd) Run(logger *slog.Logger, global GlobalArgs) error { Platform: c.Platform, } opts := generateOpts(&flags, &global) - filters := []string{"^check.*$", "^build.*$", "^test.*$"} - return ci.Run(c.Path, filters, global.Local, opts...) + return ci.Run(c.Path, global.Local, opts...) } diff --git a/forge/cli/tui/ci/app.go b/forge/cli/tui/ci/app.go index f39eae8a..89c1cb5c 100644 --- a/forge/cli/tui/ci/app.go +++ b/forge/cli/tui/ci/app.go @@ -3,6 +3,8 @@ package ci import ( "fmt" "log/slog" + "os" + "path/filepath" "strings" "github.com/charmbracelet/bubbles/spinner" @@ -11,6 +13,8 @@ import ( "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/earthly" "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/project" "github.com/input-output-hk/catalyst-forge/forge/cli/tui" + "github.com/input-output-hk/catalyst-forge/tools/pkg/git" + "github.com/input-output-hk/catalyst-forge/tools/pkg/walker" ) var ( @@ -100,7 +104,6 @@ func (a App) line() string { // Run starts the TUI application. func Run(scanPath string, - filters []string, local bool, opts ...earthly.EarthlyExecutorOption, ) error { @@ -117,17 +120,42 @@ func Run(scanPath string, logger, ) + if scanPath == "" { + scanPath, err = findRoot(".", logger) + if err != nil { + return fmt.Errorf("failed to find root of git repository: %w", err) + } + + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("failed to get current working directory: %w", err) + } + + scanPath, err = filepath.Rel(cwd, scanPath) + if err != nil { + return fmt.Errorf("failed to get relative path: %w", err) + } + } + + project, err := loader.Load(scanPath) + if err != nil { + return fmt.Errorf("failed to load project: %w", err) + } + if len(project.Blueprint.Global.CI.Local) <= 0 { + return fmt.Errorf("no local CI filters found in project") + } + ci := CI{ - filters: filters, + filters: project.Blueprint.Global.CI.Local, loader: &loader, logger: logger, options: opts, scanPath: scanPath, } - logger.Info("Loading project") + logger.Info("Loading CI") if err := ci.Load(); err != nil { - return err + return fmt.Errorf("failed to load CI: %w", err) } app := App{ @@ -138,8 +166,14 @@ func Run(scanPath string, logger.Info("Starting program") p := tea.NewProgram(app) if _, err := p.Run(); err != nil { - return err + return fmt.Errorf("failed to run program: %w", err) } return nil } + +// findRoot finds the root of a git repository. +func findRoot(scanPath string, logger *slog.Logger) (string, error) { + rw := walker.NewDefaultFSReverseWalker(logger) + return git.FindGitRoot(scanPath, &rw) +}