Skip to content

Commit

Permalink
feat: adds runtime data
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Sep 13, 2024
1 parent a8671c9 commit 55b0c64
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 49 deletions.
8 changes: 8 additions & 0 deletions blueprint/pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type InjectorOverrider func(value cue.Value) map[string]string
type BlueprintLoader interface {
// Load loads the blueprint.
Load(projectPath, gitRootPath string) (blueprint.RawBlueprint, error)

// SetOverrider sets the InjectorOverrider.
SetOverrider(overrider InjectorOverrider)
}

// DefaultBlueprintLoader is the default implementation of the BlueprintLoader
Expand Down Expand Up @@ -142,6 +145,11 @@ func (b *DefaultBlueprintLoader) Load(projectPath, gitRootPath string) (blueprin
return blueprint.NewRawBlueprint(finalBlueprint), nil
}

// SetOverrider sets the InjectorOverrider.
func (b *DefaultBlueprintLoader) SetOverrider(overrider InjectorOverrider) {
b.overrider = overrider
}

// NewDefaultBlueprintLoader creates a new DefaultBlueprintLoader.
func NewDefaultBlueprintLoader(overrider InjectorOverrider, logger *slog.Logger) DefaultBlueprintLoader {
if logger == nil {
Expand Down
46 changes: 45 additions & 1 deletion blueprint/pkg/loader/mocks/loader.go

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

4 changes: 2 additions & 2 deletions forge/cli/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test:
release:
FROM scratch

ARG version="0.0.0"
ARG version="dev"

ARG TARGETOS
ARG TARGETARCH
Expand All @@ -74,7 +74,7 @@ publish:

ARG container="forge"
ARG tag="latest"
ARG version="0.0.2"
ARG version="dev"

ARG TARGETOS
ARG TARGETARCH
Expand Down
30 changes: 20 additions & 10 deletions forge/cli/blueprint.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ version: "1.0"
project: {
name: "forge"
ci: targets: {
publish: platforms: [
"linux/amd64",
"linux/arm64",
]
release: platforms: [
"linux/amd64",
"linux/arm64",
"darwin/amd64",
"darwin/arm64",
]
publish: {
args: {
version: string | *"dev" @env(name="GIT_TAG",type="string")
}
platforms: [
"linux/amd64",
"linux/arm64",
]
}
release: {
args: {
version: string | *"dev" @env(name="GIT_TAG",type="string")
}
platforms: [
"linux/amd64",
"linux/arm64",
"darwin/amd64",
"darwin/arm64",
]
}
}
}
2 changes: 1 addition & 1 deletion forge/cli/cmd/cmds/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ScanCmd struct {

func (c *ScanCmd) Run(logger *slog.Logger) error {
walker := walker.NewDefaultFSWalker(logger)
loader := project.NewDefaultProjectLoader(logger)
loader := project.NewDefaultProjectLoader(loadRuntimes(logger), logger)

var rootPath string
if c.Absolute {
Expand Down
4 changes: 2 additions & 2 deletions forge/cli/cmd/cmds/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log/slog"

"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/tag"
p "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/project"
)

type TagCmd struct {
Expand All @@ -26,7 +26,7 @@ func (c *TagCmd) Run(logger *slog.Logger) error {
}

var output TagOutput
tagger := tag.NewTagger(&project, c.CI, c.Trim, logger)
tagger := p.NewTagger(&project, c.CI, c.Trim, logger)

if project.Blueprint.Global.CI.Tagging.Strategy != "" {
tag, err := tagger.GenerateTag()
Expand Down
9 changes: 8 additions & 1 deletion forge/cli/cmd/cmds/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,17 @@ func generateOpts(target string, flags *RunCmd, config *schema.Blueprint) []eart

// loadProject loads the project from the given root path.
func loadProject(rootPath string, logger *slog.Logger) (project.Project, error) {
loader := project.NewDefaultProjectLoader(logger)
loader := project.NewDefaultProjectLoader(loadRuntimes(logger), logger)
return loader.Load(rootPath)
}

// loadRuntimes loads the all runtime data collectors.
func loadRuntimes(logger *slog.Logger) []project.RuntimeData {
return []project.RuntimeData{
project.NewGitRuntime(logger),
}
}

// printJson prints the given data as a JSON string.
func printJson(data interface{}, pretty bool) {
var out []byte
Expand Down
1 change: 1 addition & 0 deletions forge/cli/pkg/earthly/earthly.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (e EarthlyExecutor) Run() (map[string]EarthlyExecutionResult, error) {
for i := 0; i < e.opts.retries+1; i++ {
arguments := e.buildArguments(platform)

os.Setenv("GIT_TAG", "v0.0.0")
e.logger.Info("Executing Earthly", "attempt", i, "retries", e.opts.retries, "arguments", arguments, "platform", platform)
output, err = e.executor.Execute("earthly", arguments)
if err == nil {
Expand Down
62 changes: 47 additions & 15 deletions forge/cli/pkg/project/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"fmt"
"io"
"log/slog"
"os"
"path/filepath"

"cuelang.org/go/cue"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/blueprint"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/loader"
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/earthfile"
"github.com/input-output-hk/catalyst-forge/tools/pkg/git"
Expand All @@ -28,37 +31,26 @@ type DefaultProjectLoader struct {
fs afero.Fs
logger *slog.Logger
repoLoader git.RepoLoader
runtimes []RuntimeData
}

func (p *DefaultProjectLoader) Load(projectPath string) (Project, error) {
p.logger.Info("Finding git root", "at", projectPath)
p.logger.Info("Finding git root", "projectPath", projectPath)
w := walker.NewCustomReverseFSWalker(p.fs, p.logger)
gitRoot, err := git.FindGitRoot(projectPath, &w)
if err != nil {
p.logger.Error("Failed to find git root", "error", err)
return Project{}, fmt.Errorf("failed to find git root: %w", err)
}

p.logger.Info("Loading repository", "path", gitRoot)
rl := git.NewCustomDefaultRepoLoader(p.fs)
repo, err := rl.Load(gitRoot)
if err != nil {
p.logger.Error("Failed to load repository", "error", err)
return Project{}, fmt.Errorf("failed to load repository: %w", err)
}

p.logger.Info("Loading blueprint", "path", projectPath)
rbp, err := p.blueprintLoader.Load(projectPath, gitRoot)
if err != nil {
p.logger.Error("Failed to load blueprint", "error", err, "path", projectPath)
return Project{}, fmt.Errorf("failed to load blueprint: %w", err)
}

bp, err := rbp.Decode()
if err != nil {
p.logger.Error("Failed to decode blueprint", "error", err)
return Project{}, fmt.Errorf("failed to decode blueprint: %w", err)
}

efPath := filepath.Join(projectPath, "Earthfile")
exists, err := afero.Exists(p.fs, efPath)
if err != nil {
Expand All @@ -83,6 +75,43 @@ func (p *DefaultProjectLoader) Load(projectPath string) (Project, error) {
ef = &efs
}

p.logger.Info("Setting blueprint runtime data")
p.blueprintLoader.SetOverrider(func(value cue.Value) map[string]string {
data := make(map[string]string)
for _, r := range p.runtimes {
partialProject := Project{
Earthfile: ef,
Repo: repo,
Path: projectPath,
RepoRoot: gitRoot,
rawBlueprint: blueprint.NewRawBlueprint(value),
}

for k, v := range r.Load(&partialProject) {
if err := os.Setenv(k, v); err != nil {
p.logger.Error("Failed to set environment variable", "error", err, "key", k, "value", v)
}

data[k] = v
}
}

return data
})

p.logger.Info("Loading blueprint", "path", projectPath)
rbp, err := p.blueprintLoader.Load(projectPath, gitRoot)
if err != nil {
p.logger.Error("Failed to load blueprint", "error", err, "path", projectPath)
return Project{}, fmt.Errorf("failed to load blueprint: %w", err)
}

bp, err := rbp.Decode()
if err != nil {
p.logger.Error("Failed to decode blueprint", "error", err)
return Project{}, fmt.Errorf("failed to decode blueprint: %w", err)
}

return Project{
Blueprint: bp,
Earthfile: ef,
Expand All @@ -95,7 +124,7 @@ func (p *DefaultProjectLoader) Load(projectPath string) (Project, error) {
}

// NewDefaultProjectLoader creates a new DefaultProjectLoader.
func NewDefaultProjectLoader(logger *slog.Logger) DefaultProjectLoader {
func NewDefaultProjectLoader(runtimes []RuntimeData, logger *slog.Logger) DefaultProjectLoader {
if logger == nil {
logger = slog.New(slog.NewTextHandler(io.Discard, nil))
}
Expand All @@ -107,6 +136,7 @@ func NewDefaultProjectLoader(logger *slog.Logger) DefaultProjectLoader {
fs: afero.NewOsFs(),
logger: logger,
repoLoader: &rl,
runtimes: runtimes,
}
}

Expand All @@ -115,6 +145,7 @@ func NewCustomProjectLoader(
fs afero.Fs,
bl loader.BlueprintLoader,
rl git.RepoLoader,
runtimes []RuntimeData,
logger *slog.Logger,
) DefaultProjectLoader {
if logger == nil {
Expand All @@ -126,5 +157,6 @@ func NewCustomProjectLoader(
fs: fs,
logger: logger,
repoLoader: rl,
runtimes: runtimes,
}
}
2 changes: 2 additions & 0 deletions forge/cli/pkg/project/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/blueprint"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/loader"
"github.com/input-output-hk/catalyst-forge/blueprint/pkg/loader/mocks"
"github.com/input-output-hk/catalyst-forge/tools/pkg/testutils"
"github.com/spf13/afero"
Expand Down Expand Up @@ -148,6 +149,7 @@ bar:
}
return tt.blueprint, nil
},
SetOverriderFunc: func(overrider loader.InjectorOverrider) {},
}

loader := DefaultProjectLoader{
Expand Down
6 changes: 6 additions & 0 deletions forge/cli/pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/earthfile"
)

type TagInfo struct {
Generated string `json:"generated"`
Git string `json:"git"`
}

// Project represents a project
type Project struct {
Blueprint schema.Blueprint
Expand All @@ -19,6 +24,7 @@ type Project struct {
Path string
Repo *gg.Repository
RepoRoot string
Tags TagInfo
rawBlueprint blueprint.RawBlueprint
}

Expand Down
Loading

0 comments on commit 55b0c64

Please sign in to comment.