Skip to content

Commit

Permalink
feat(cli): adds dump command and allows template command to process m…
Browse files Browse the repository at this point in the history
…odules
  • Loading branch information
jmgilman committed Jan 30, 2025
1 parent dd72421 commit 7e84445
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cli/cmd/cmds/module/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package module

type ModuleCmd struct {
Deploy DeployCmd `cmd:"" help:"Deploys a module (or project) to the configured GitOps repository."`
Template TemplateCmd `cmd:"" help:"Generates a module's (or project's) deployment YAML."`
Deploy DeployCmd `cmd:"" help:"Deploys a project to the configured GitOps repository."`
Dump DumpCmd `cmd:"" help:"Dumps a project's deployment modules."`
Template TemplateCmd `cmd:"" help:"Generates a project's (or module's) deployment YAML."`
}
32 changes: 32 additions & 0 deletions cli/cmd/cmds/module/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package module

import (
"fmt"

"github.com/input-output-hk/catalyst-forge/cli/pkg/run"
"github.com/input-output-hk/catalyst-forge/lib/project/deployment"
)

type DumpCmd struct {
Project string `arg:"" help:"The path to the project to dump." kong:"arg,predictor=path"`
}

func (c *DumpCmd) Run(ctx run.RunContext) error {
project, err := ctx.ProjectLoader.Load(c.Project)
if err != nil {
return fmt.Errorf("could not load project: %w", err)
}

modules := project.Blueprint.Project.Deployment.Modules
if modules == nil {
return fmt.Errorf("no deployment modules found for project")
}

result, err := deployment.DumpBundle(modules)
if err != nil {
return fmt.Errorf("failed to dump deployment modules: %w", err)
}

fmt.Print(string(result))
return nil
}
31 changes: 26 additions & 5 deletions cli/cmd/cmds/module/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,45 @@ package module

import (
"fmt"
"os"
"strings"

"github.com/input-output-hk/catalyst-forge/cli/pkg/run"
"github.com/input-output-hk/catalyst-forge/lib/project/deployment"
"github.com/input-output-hk/catalyst-forge/lib/project/schema"
)

type TemplateCmd struct {
Module string `arg:"" help:"The path to the module (or project)." kong:"arg,predictor=path"`
Path string `arg:"" help:"The path to the module (or project)." kong:"arg,predictor=path"`
}

func (c *TemplateCmd) Run(ctx run.RunContext) error {
project, err := ctx.ProjectLoader.Load(c.Module)
stat, err := os.Stat(c.Path)
if err != nil {
return fmt.Errorf("could not load project: %w", err)
return fmt.Errorf("could not stat path: %w", err)
}

modules := project.Blueprint.Project.Deployment.Modules
var bundle schema.DeploymentModuleBundle
if stat.IsDir() {
project, err := ctx.ProjectLoader.Load(c.Path)
if err != nil {
return fmt.Errorf("could not load project: %w", err)
}

bundle = project.Blueprint.Project.Deployment.Modules
} else {
src, err := os.ReadFile(c.Path)
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}

bundle, err = deployment.ParseBundle(src)
if err != nil {
return fmt.Errorf("could not parse module file: %w", err)
}
}

result, err := ctx.DeploymentGenerator.GenerateBundle(modules)
result, err := ctx.DeploymentGenerator.GenerateBundle(bundle)
if err != nil {
return fmt.Errorf("failed to generate manifests: %w", err)
}
Expand Down
34 changes: 34 additions & 0 deletions cli/out.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
main: {
instance: "foundry-api"
name: "app"
namespace: "default"
registry: "332405224602.dkr.ecr.eu-central-1.amazonaws.com/catalyst-deployments"
values: {
service: {
targetPort: 8080
port: 8080
}
deployment: {
containers: {
main: {
probes: {
readiness: {
path: "/"
}
liveness: {
path: "/"
}
}
port: 8080
image: {
tag: "dd724218713a01e0d96d95d60b9dcd044980399b"
name: "ghcr.io/input-output-hk/catalyst-forge/foundry-api"
}
}
}
}
}
version: "0.2.0"
}
}
16 changes: 16 additions & 0 deletions lib/project/deployment/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,19 @@ func DumpBundle(mod schema.DeploymentModuleBundle) ([]byte, error) {

return src, nil
}

// ParseModule parses a deployment module from CUE source.
func ParseBundle(src []byte) (schema.DeploymentModuleBundle, error) {
ctx := cuecontext.New()
v := ctx.CompileBytes(src)
if v.Err() != nil {
return schema.DeploymentModuleBundle{}, fmt.Errorf("failed to compile bundle: %w", v.Err())
}

var bundle schema.DeploymentModuleBundle
if err := v.Decode(&bundle); err != nil {
return schema.DeploymentModuleBundle{}, fmt.Errorf("failed to decode bundle: %w", err)
}

return bundle, nil
}

0 comments on commit 7e84445

Please sign in to comment.