Skip to content

Commit

Permalink
bt: rename project to stack
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Feb 2, 2024
1 parent fa14bda commit bc0766f
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 108 deletions.
4 changes: 2 additions & 2 deletions bt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"

"github.com/DavidGamba/dgtools/bt/config"
"github.com/DavidGamba/dgtools/bt/project"
"github.com/DavidGamba/dgtools/bt/stack"
"github.com/DavidGamba/dgtools/bt/terraform"
"github.com/DavidGamba/dgtools/buildutils"
"github.com/DavidGamba/go-getoptions"
Expand Down Expand Up @@ -42,7 +42,7 @@ func program(args []string) int {
opt.SetUnknownMode(getoptions.Pass)

terraform.NewCommand(ctx, opt)
project.NewCommand(ctx, opt)
stack.NewCommand(ctx, opt)

opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
Expand Down
26 changes: 0 additions & 26 deletions bt/project/config/schema.cue

This file was deleted.

25 changes: 0 additions & 25 deletions bt/project/config/schema.go

This file was deleted.

53 changes: 0 additions & 53 deletions bt/project/graph.go

This file was deleted.

File renamed without changes.
27 changes: 27 additions & 0 deletions bt/stack/config/schema.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package stack

#ComponentID: string & =~"^[a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])?$"

#Variable: {
name: string
value: string
}

#Component: {
id: #ComponentID
path: string | *id
depends_on: [...#Component.id]
variables: [...#Variable]
}

#Stack: [ID=_]: {
id: ID
components: [...#Component]
component_overrides: [ComponentID=_]: {
workspaces: [...string]
}
}

component: [ID=_]: #Component & {id: ID}

stack: #Stack
42 changes: 42 additions & 0 deletions bt/stack/config/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import "fmt"

type Config struct {
Component map[ComponentID]Component `json:"component"`
Stack map[string]Stack `json:"stack"`
}

type Component struct {
ID ComponentID `json:"id"`
Path string `json:"path"`
DependsOn []string `json:"depends_on"`
Variables []Variable `json:"variables"`
}

type ComponentID string

type Variable struct {
Name string `json:"name"`
Value string `json:"value"`
}

type Stack struct {
ID string `json:"id"`
Components []Component `json:"components"`
ComponentOverrides map[string]struct {
Workspaces []string
} `json:"component_overrides"`
}

func (c Component) String() string {
return fmt.Sprintf("id: %s, path: %s, depends_on: %v, variables: %v", c.ID, c.Path, c.DependsOn, c.Variables)
}

func (s Stack) String() string {
return fmt.Sprintf("id: %s, components: %v, component_overrides: %v", s.ID, s.Components, s.ComponentOverrides)
}

func (v Variable) String() string {
return fmt.Sprintf("%s=%s", v.Name, v.Value)
}
51 changes: 51 additions & 0 deletions bt/stack/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package stack

import (
"context"
"fmt"

"github.com/DavidGamba/dgtools/bt/stack/config"
"github.com/DavidGamba/go-getoptions"
)

func GraphCMD(parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("graph", "Visual representation of the project layers DAG")
opt.SetCommandFn(GraphRun)
opt.Bool("reverse", false, opt.Description("Reverses the order of operation"))
opt.String("T", "", opt.Description("Set output format. For example: -T png"))
opt.String("filename", "project-graph.png")
return opt
}

func GraphRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
configFile := opt.Value("config").(string)

cfg, _, err := config.Get(ctx, configFile)
if err != nil {
return fmt.Errorf("failed to get project config: %w", err)
}

for _, c := range cfg.Component {
Logger.Printf("component: %s\n", c)
}

for _, s := range cfg.Stack {
Logger.Printf("stack: %s\n", s)
for _, c := range s.Components {
Logger.Printf("%s\n", c)
}
// fmt.Printf("\tlayers\n")
// for _, v := range v.Components {
// fmt.Printf("\t\t%s\n", v)
// }
// fmt.Printf("\tlayer_overrides\n")
// for k, v := range v.ComponentOverrides {
// fmt.Printf("\t\t%s\n", k)
// for _, v := range v.Workspaces {
// fmt.Printf("\t\t\t%s\n", v)
// }
// }
}

return nil
}
6 changes: 4 additions & 2 deletions bt/project/project.go → bt/stack/stack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package project
package stack

import (
"context"
Expand All @@ -11,7 +11,9 @@ import (
var Logger = log.New(os.Stderr, "", log.LstdFlags)

func NewCommand(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("project", "project related tasks")
opt := parent.NewCommand("stack", "project related tasks")
opt.String("config", "bt-stacks.cue", opt.Description("Stacks configuration file"))

GraphCMD(opt)
return opt
}

0 comments on commit bc0766f

Please sign in to comment.