Skip to content

Commit 5ed5c04

Browse files
authored
feat(cli): adds dump command and allows template command to process modules (#129)
1 parent dd72421 commit 5ed5c04

File tree

5 files changed

+111
-7
lines changed

5 files changed

+111
-7
lines changed

cli/cmd/cmds/module/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package module
22

33
type ModuleCmd struct {
4-
Deploy DeployCmd `cmd:"" help:"Deploys a module (or project) to the configured GitOps repository."`
5-
Template TemplateCmd `cmd:"" help:"Generates a module's (or project's) deployment YAML."`
4+
Deploy DeployCmd `cmd:"" help:"Deploys a project to the configured GitOps repository."`
5+
Dump DumpCmd `cmd:"" help:"Dumps a project's deployment modules."`
6+
Template TemplateCmd `cmd:"" help:"Generates a project's (or module's) deployment YAML."`
67
}

cli/cmd/cmds/module/dump.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package module
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/input-output-hk/catalyst-forge/cli/pkg/run"
7+
"github.com/input-output-hk/catalyst-forge/lib/project/deployment"
8+
)
9+
10+
type DumpCmd struct {
11+
Project string `arg:"" help:"The path to the project to dump." kong:"arg,predictor=path"`
12+
}
13+
14+
func (c *DumpCmd) Run(ctx run.RunContext) error {
15+
project, err := ctx.ProjectLoader.Load(c.Project)
16+
if err != nil {
17+
return fmt.Errorf("could not load project: %w", err)
18+
}
19+
20+
modules := project.Blueprint.Project.Deployment.Modules
21+
if modules == nil {
22+
return fmt.Errorf("no deployment modules found for project")
23+
}
24+
25+
result, err := deployment.DumpBundle(modules)
26+
if err != nil {
27+
return fmt.Errorf("failed to dump deployment modules: %w", err)
28+
}
29+
30+
fmt.Print(string(result))
31+
return nil
32+
}

cli/cmd/cmds/module/template.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,45 @@ package module
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
"github.com/input-output-hk/catalyst-forge/cli/pkg/run"
9+
"github.com/input-output-hk/catalyst-forge/lib/project/deployment"
10+
"github.com/input-output-hk/catalyst-forge/lib/project/schema"
811
)
912

1013
type TemplateCmd struct {
11-
Module string `arg:"" help:"The path to the module (or project)." kong:"arg,predictor=path"`
14+
Path string `arg:"" help:"The path to the module (or project)." kong:"arg,predictor=path"`
1215
}
1316

1417
func (c *TemplateCmd) Run(ctx run.RunContext) error {
15-
project, err := ctx.ProjectLoader.Load(c.Module)
18+
stat, err := os.Stat(c.Path)
1619
if err != nil {
17-
return fmt.Errorf("could not load project: %w", err)
20+
return fmt.Errorf("could not stat path: %w", err)
1821
}
1922

20-
modules := project.Blueprint.Project.Deployment.Modules
23+
var bundle schema.DeploymentModuleBundle
24+
if stat.IsDir() {
25+
project, err := ctx.ProjectLoader.Load(c.Path)
26+
if err != nil {
27+
return fmt.Errorf("could not load project: %w", err)
28+
}
29+
30+
bundle = project.Blueprint.Project.Deployment.Modules
31+
} else {
32+
src, err := os.ReadFile(c.Path)
33+
if err != nil {
34+
return fmt.Errorf("could not read file: %w", err)
35+
}
36+
37+
bundle, err = deployment.ParseBundle(src)
38+
if err != nil {
39+
return fmt.Errorf("could not parse module file: %w", err)
40+
}
41+
}
2142

22-
result, err := ctx.DeploymentGenerator.GenerateBundle(modules)
43+
result, err := ctx.DeploymentGenerator.GenerateBundle(bundle)
2344
if err != nil {
2445
return fmt.Errorf("failed to generate manifests: %w", err)
2546
}

cli/out.cue

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
main: {
3+
instance: "foundry-api"
4+
name: "app"
5+
namespace: "default"
6+
registry: "332405224602.dkr.ecr.eu-central-1.amazonaws.com/catalyst-deployments"
7+
values: {
8+
service: {
9+
targetPort: 8080
10+
port: 8080
11+
}
12+
deployment: {
13+
containers: {
14+
main: {
15+
probes: {
16+
readiness: {
17+
path: "/"
18+
}
19+
liveness: {
20+
path: "/"
21+
}
22+
}
23+
port: 8080
24+
image: {
25+
tag: "dd724218713a01e0d96d95d60b9dcd044980399b"
26+
name: "ghcr.io/input-output-hk/catalyst-forge/foundry-api"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
version: "0.2.0"
33+
}
34+
}

lib/project/deployment/module.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ func DumpBundle(mod schema.DeploymentModuleBundle) ([]byte, error) {
4141

4242
return src, nil
4343
}
44+
45+
// ParseModule parses a deployment module from CUE source.
46+
func ParseBundle(src []byte) (schema.DeploymentModuleBundle, error) {
47+
ctx := cuecontext.New()
48+
v := ctx.CompileBytes(src)
49+
if v.Err() != nil {
50+
return schema.DeploymentModuleBundle{}, fmt.Errorf("failed to compile bundle: %w", v.Err())
51+
}
52+
53+
var bundle schema.DeploymentModuleBundle
54+
if err := v.Decode(&bundle); err != nil {
55+
return schema.DeploymentModuleBundle{}, fmt.Errorf("failed to decode bundle: %w", err)
56+
}
57+
58+
return bundle, nil
59+
}

0 commit comments

Comments
 (0)