Skip to content

Commit

Permalink
refactor: introduces project abstraction (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman authored Sep 9, 2024
1 parent 4ab6cee commit 1c2b4b9
Show file tree
Hide file tree
Showing 22 changed files with 429 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
EARTHFILE="+$TARGET"
fi
IMAGE="$(echo "$RESULT" | jq -r ".images[\"$EARTHFILE\"]")"
IMAGE="$(echo "$RESULT" | jq -r ".\"linux/amd64\".images[\"$EARTHFILE\"]")"
if [[ "$IMAGE" == "null" ]]; then
echo "::error file=${PROJECT}/Earthfile::No images produced. Cannot publish."
Expand Down
4 changes: 3 additions & 1 deletion blueprint/pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/input-output-hk/catalyst-forge/tools/pkg/walker"
)

//go:generate go run github.com/matryer/moq@latest --pkg mocks --out ./mocks/loader.go . BlueprintLoader

const BlueprintFileName = "blueprint.cue"

var (
Expand All @@ -29,7 +31,7 @@ var (
type BlueprintLoader interface {

// Load loads the blueprint.
Load() blueprint.RawBlueprint
Load() (blueprint.RawBlueprint, error)
}

// DefaultBlueprintLoader is the default implementation of the BlueprintLoader
Expand Down
69 changes: 69 additions & 0 deletions blueprint/pkg/loader/mocks/loader.go

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

1 change: 0 additions & 1 deletion forge/actions/publish/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ module.exports = {
*/
async function getBlueprint(project) {
const result = await exec.getExecOutput("forge", [
"blueprint",
"dump",
project,
]);
Expand Down
1 change: 0 additions & 1 deletion forge/actions/release/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ module.exports = {
*/
async function getBlueprint(project) {
const result = await exec.getExecOutput("forge", [
"blueprint",
"dump",
project,
]);
Expand Down
12 changes: 6 additions & 6 deletions forge/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ runs:
shell: bash
run: |
echo "==== AWS Setup ====="
BP=$(forge blueprint dump .)
BP=$(forge dump .)
AWS=$(echo "$BP" | jq -r .global.ci.providers.aws)
if [[ "$AWS" != "null" ]]; then
Expand Down Expand Up @@ -74,11 +74,11 @@ runs:
shell: bash
run: |
echo "==== Docker Setup ====="
BP=$(forge blueprint dump .)
BP=$(forge dump .)
DOCKER=$(echo "$BP" | jq -r .global.ci.providers.docker.credentials)
if [[ "$DOCKER" != "null" ]]; then
SECRET=$(forge secret get -b . global.ci.providers.docker.credentials)
SECRET=$(forge secret get --project . global.ci.providers.docker.credentials)
USERNAME=$(echo "$SECRET" | jq -r .username)
PASSWORD=$(echo "$SECRET" | jq -r .password)
Expand Down Expand Up @@ -106,7 +106,7 @@ runs:
shell: bash
run: |
echo "==== GitHub Setup ====="
BP=$(forge blueprint dump .)
BP=$(forge dump .)
GITHUB=$(echo "$BP" | jq -r .global.ci.providers.github.registry)
if [[ "$GITHUB" != "null" ]]; then
Expand All @@ -128,7 +128,7 @@ runs:
shell: bash
run: |
echo "==== Earthly Setup ====="
BP=$(forge blueprint dump .)
BP=$(forge dump .)
EARTHLY=$(echo "$BP" | jq -r .global.ci.providers.earthly)
if [[ "$EARTHLY" != "null" ]]; then
Expand All @@ -140,7 +140,7 @@ runs:
EARTHLY_CREDS=$(echo "$BP" | jq -r .global.ci.providers.earthly.credentials)
if [[ "$EARTHLY_CREDS" != "null" ]]; then
SECRET=$(forge secret get -b . global.ci.providers.earthly.credentials)
SECRET=$(forge secret get --project . global.ci.providers.earthly.credentials)
TOKEN=$(echo "$SECRET" | jq -r .token)
if [[ "$TOKEN" == "null" ]]; then
Expand Down
31 changes: 0 additions & 31 deletions forge/cli/cmd/cmds/blueprint_dump.go

This file was deleted.

25 changes: 0 additions & 25 deletions forge/cli/cmd/cmds/blueprint_validate.go

This file was deleted.

26 changes: 26 additions & 0 deletions forge/cli/cmd/cmds/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmds

import (
"fmt"
"log/slog"
)

type DumpCmd struct {
Project string `arg:"" help:"Path to the project."`
Pretty bool `help:"Pretty print JSON output."`
}

func (c *DumpCmd) Run(logger *slog.Logger) error {
project, err := loadProject(c.Project, logger)
if err != nil {
return fmt.Errorf("could not load project: %w", err)
}

json, err := project.Raw().MarshalJSON()
if err != nil {
return err
}

fmt.Println(string(json))
return nil
}
4 changes: 2 additions & 2 deletions forge/cli/cmd/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *RunCmd) Run(logger *slog.Logger) error {
earthfileDir := strings.Split(c.Path, "+")[0]
target := strings.Split(c.Path, "+")[1]

config, err := loadBlueprint(earthfileDir, logger)
project, err := loadProject(earthfileDir, logger)
if err != nil {
return err
}
Expand All @@ -38,7 +38,7 @@ func (c *RunCmd) Run(logger *slog.Logger) error {
executor.WithRedirect(),
)

opts := generateOpts(target, c, &config)
opts := generateOpts(target, c, &project.Blueprint)
earthlyExec := earthly.NewEarthlyExecutor(
earthfileDir,
target,
Expand Down
34 changes: 17 additions & 17 deletions forge/cli/cmd/cmds/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ const (
)

type Get struct {
Blueprint string `short:"b" help:"Path to a blueprint file (or directory)."`
Key string `short:"k" help:"The key inside of the secret to get."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a blueprint if --blueprint is specified)."`
Key string `short:"k" help:"The key inside of the secret to get."`
Project string `help:"Path to a project to use for getting secret configuration."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
}

type Set struct {
Blueprint string `short:"b" help:"Path to a blueprint file (or directory)."`
Field []string `short:"f" help:"A secret field to set."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a blueprint if --blueprint is specified)."`
Value string `arg:"" help:"The value to set." default:""`
Field []string `short:"f" help:"A secret field to set."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
Project string `help:"Path to a project to use for getting secret configuration."`
Value string `arg:"" help:"The value to set." default:""`
}

type SecretCmd struct {
Expand All @@ -39,14 +39,14 @@ func (c *Get) Run(logger *slog.Logger) error {
var path, provider string
var maps map[string]string

if c.Blueprint != "" {
rbp, err := loadRawBlueprint(c.Blueprint, logger)
if c.Project != "" {
project, err := loadProject(c.Project, logger)
if err != nil {
return fmt.Errorf("could not load blueprint: %w", err)
return fmt.Errorf("could not load project: %w", err)
}

var secret schema.Secret
if err := rbp.DecodePath(c.Path, &secret); err != nil {
if err := project.Raw().DecodePath(c.Path, &secret); err != nil {
return fmt.Errorf("could not decode secret: %w", err)
}

Expand Down Expand Up @@ -128,14 +128,14 @@ func (c *Get) Run(logger *slog.Logger) error {
func (c *Set) Run(logger *slog.Logger) error {
var path, provider string

if c.Blueprint != "" {
rbp, err := loadRawBlueprint(c.Blueprint, logger)
if c.Project != "" {
project, err := loadProject(c.Project, logger)
if err != nil {
return fmt.Errorf("could not load blueprint: %w", err)
return fmt.Errorf("could not load project: %w", err)
}

var secret schema.Secret
if err := rbp.DecodePath(c.Path, &secret); err != nil {
if err := project.Raw().DecodePath(c.Path, &secret); err != nil {
return fmt.Errorf("could not decode secret: %w", err)
}

Expand Down
24 changes: 4 additions & 20 deletions forge/cli/cmd/cmds/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"
"log/slog"

"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/schema"
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/earthly"
"github.com/input-output-hk/catalyst-forge/forge/cli/pkg/project"
)

// enumerate enumerates the Earthfile+Target pairs from the target map.
Expand Down Expand Up @@ -85,24 +84,9 @@ func generateOpts(target string, flags *RunCmd, config *schema.Blueprint) []eart
return opts
}

// loadBlueprint loads the blueprint file from the given root path.
func loadBlueprint(rootPath string, logger *slog.Logger) (schema.Blueprint, error) {
raw, err := loadRawBlueprint(rootPath, logger)
if err != nil {
return schema.Blueprint{}, fmt.Errorf("failed loading blueprint: %w", err)
}

bp, err := raw.Decode()
if err != nil {
return schema.Blueprint{}, fmt.Errorf("failed decoding blueprint: %w", err)
}

return bp, nil
}

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

Expand Down
18 changes: 18 additions & 0 deletions forge/cli/cmd/cmds/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmds

import (
"log/slog"
)

type ValidateCmd struct {
Project string `arg:"" help:"Path to the project."`
}

func (c *ValidateCmd) Run(logger *slog.Logger) error {
_, err := loadProject(c.Project, logger)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 1c2b4b9

Please sign in to comment.