Skip to content

Commit 647acab

Browse files
author
Aaron Torres
committed
initial wiring of YAML IR format to cli and example reading a file
1 parent af4276b commit 647acab

File tree

5 files changed

+269
-9
lines changed

5 files changed

+269
-9
lines changed

cmd/k2/main.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/klothoplatform/klotho/pkg/k2/model"
8+
"github.com/spf13/cobra"
9+
"gopkg.in/yaml.v3"
10+
)
11+
12+
func initCmd() string {
13+
return "Initialization view"
14+
}
15+
16+
func deployCmd() string {
17+
return "Deploy view"
18+
}
19+
20+
func destroyCmd() string {
21+
return "Destroy view"
22+
}
23+
24+
func planCmd() string {
25+
return "Plan view"
26+
}
27+
28+
func irCmd(filePath string) string {
29+
ir, err := model.ReadIRFile(filePath)
30+
if err != nil {
31+
return fmt.Sprintf("Error reading IR file: %s", err)
32+
}
33+
res, err := yaml.Marshal(ir)
34+
if err != nil {
35+
return fmt.Sprintf("Error marshalling IR: %s", err)
36+
}
37+
return string(res)
38+
}
39+
40+
func executeCommand(cmd func() string) {
41+
// Execute the command and print the view
42+
result := cmd()
43+
fmt.Println(result)
44+
}
45+
46+
func executeIRCommand(filePath string) {
47+
result := irCmd(filePath)
48+
fmt.Println(result)
49+
}
50+
51+
func main() {
52+
var rootCmd = &cobra.Command{Use: "app"}
53+
54+
var initCommand = &cobra.Command{
55+
Use: "init",
56+
Short: "Run the init command",
57+
Run: func(cmd *cobra.Command, args []string) {
58+
executeCommand(initCmd)
59+
},
60+
}
61+
62+
var deployCommand = &cobra.Command{
63+
Use: "deploy",
64+
Short: "Run the deploy command",
65+
Run: func(cmd *cobra.Command, args []string) {
66+
executeCommand(deployCmd)
67+
},
68+
}
69+
70+
var destroyCommand = &cobra.Command{
71+
Use: "destroy",
72+
Short: "Run the destroy command",
73+
Run: func(cmd *cobra.Command, args []string) {
74+
executeCommand(destroyCmd)
75+
},
76+
}
77+
78+
var planCommand = &cobra.Command{
79+
Use: "plan",
80+
Short: "Run the plan command",
81+
Run: func(cmd *cobra.Command, args []string) {
82+
executeCommand(planCmd)
83+
},
84+
}
85+
86+
var irCommand = &cobra.Command{
87+
Use: "ir [file path]",
88+
Short: "Run the IR command",
89+
Args: cobra.ExactArgs(1),
90+
Run: func(cmd *cobra.Command, args []string) {
91+
filePath := args[0]
92+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
93+
fmt.Println("Invalid file path")
94+
os.Exit(1)
95+
}
96+
97+
executeIRCommand(filePath)
98+
},
99+
}
100+
101+
rootCmd.AddCommand(initCommand)
102+
rootCmd.AddCommand(deployCommand)
103+
rootCmd.AddCommand(destroyCommand)
104+
rootCmd.AddCommand(planCommand)
105+
rootCmd.AddCommand(irCommand)
106+
107+
if err := rootCmd.Execute(); err != nil {
108+
fmt.Println(err)
109+
os.Exit(1)
110+
}
111+
}

go.mod

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.22.0
44

55
require (
66
github.com/Masterminds/sprig/v3 v3.2.3
7+
github.com/alecthomas/kong v0.8.1
78
github.com/alitto/pond v1.8.3
89
github.com/coreos/go-oidc/v3 v3.9.0
910
github.com/coreos/go-semver v0.3.0
@@ -25,9 +26,11 @@ require (
2526
github.com/stretchr/testify v1.8.2
2627
github.com/thessem/zap-prettyconsole v0.3.0
2728
github.com/vmware-labs/yaml-jsonpath v0.3.2
29+
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
2830
go.uber.org/mock v0.4.0
2931
go.uber.org/zap v1.22.0
3032
golang.org/x/oauth2 v0.13.0
33+
gopkg.in/yaml.v2 v2.4.0
3134
gopkg.in/yaml.v3 v3.0.1
3235
helm.sh/helm/v3 v3.11.1
3336
)
@@ -40,7 +43,7 @@ require (
4043
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
4144
github.com/onsi/ginkgo v1.16.5 // indirect
4245
github.com/onsi/gomega v1.23.0 // indirect
43-
github.com/rivo/uniseg v0.4.4 // indirect
46+
github.com/rivo/uniseg v0.4.7 // indirect
4447
github.com/rogpeppe/go-internal v1.8.0 // indirect
4548
go.uber.org/atomic v1.9.0 // indirect
4649
go.uber.org/goleak v1.2.0 // indirect
@@ -62,7 +65,6 @@ require (
6265
github.com/Code-Hex/dd v1.1.0 // indirect
6366
github.com/Masterminds/goutils v1.1.1 // indirect
6467
github.com/Masterminds/semver/v3 v3.2.0 // indirect
65-
github.com/alecthomas/kong v0.8.1 // indirect
6668
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
6769
github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf // indirect
6870
github.com/golang/protobuf v1.5.3 // indirect
@@ -71,7 +73,7 @@ require (
7173
github.com/inconshreveable/mousetrap v1.1.0 // indirect
7274
github.com/mattn/go-colorable v0.1.12 // indirect
7375
github.com/mattn/go-isatty v0.0.17 // indirect
74-
github.com/mattn/go-runewidth v0.0.14 // indirect
76+
github.com/mattn/go-runewidth v0.0.15 // indirect
7577
github.com/mitchellh/copystructure v1.2.0 // indirect
7678
github.com/mitchellh/reflectwalk v1.0.2 // indirect
7779
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -82,7 +84,7 @@ require (
8284
go.uber.org/multierr v1.8.0 // indirect
8385
golang.org/x/crypto v0.19.0 // indirect
8486
golang.org/x/net v0.21.0 // indirect
85-
golang.org/x/sys v0.17.0 // indirect
87+
golang.org/x/sys v0.20.0 // indirect
8688
golang.org/x/term v0.17.0 // indirect
8789
google.golang.org/appengine v1.6.8 // indirect
8890
google.golang.org/protobuf v1.31.0 // indirect

go.sum

+14-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
88
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
99
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
1010
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
11+
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
12+
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
1113
github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY=
1214
github.com/alecthomas/kong v0.8.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
15+
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
16+
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
1317
github.com/alitto/pond v1.8.3 h1:ydIqygCLVPqIX/USe5EaV/aSRXTRXDEI9JwuDdu+/xs=
1418
github.com/alitto/pond v1.8.3/go.mod h1:CmvIIGd5jKLasGI3D87qDkQxjzChdKMmnXMg3fG6M6Q=
1519
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
@@ -66,6 +70,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
6670
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
6771
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6872
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
73+
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
74+
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
6975
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
7076
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
7177
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
@@ -105,8 +111,9 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
105111
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
106112
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
107113
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
108-
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
109114
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
115+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
116+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
110117
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
111118
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
112119
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
@@ -142,8 +149,8 @@ github.com/r3labs/diff v1.1.0/go.mod h1:7WjXasNzi0vJetRcB/RqNl5dlIsmXcTTLmF5IoH6
142149
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
143150
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
144151
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
145-
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
146-
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
152+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
153+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
147154
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
148155
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
149156
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
@@ -183,6 +190,8 @@ github.com/thessem/zap-prettyconsole v0.3.0 h1:jreGIwOwkfqpA1NWTsbXXr0ZSL68b39Kp
183190
github.com/thessem/zap-prettyconsole v0.3.0/go.mod h1:93z1PhlPAYOWIOhPJvNkxE9cVM8jE5xB6/6HCtnvmcA=
184191
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
185192
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
193+
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
194+
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
186195
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
187196
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
188197
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
@@ -246,8 +255,8 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
246255
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
247256
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
248257
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
249-
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
250-
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
258+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
259+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
251260
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
252261
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
253262
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=

pkg/k2/model/ir.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package model
2+
3+
import (
4+
"os"
5+
6+
"github.com/google/uuid"
7+
yaml "gopkg.in/yaml.v2"
8+
)
9+
10+
type ApplicationEnvironment struct {
11+
SchemaVersion string `yaml:"schemaVersion"`
12+
Version int `yaml:"version"`
13+
URN string `yaml:"urn"`
14+
Constructs map[string]Construct `yaml:"constructs"`
15+
}
16+
17+
type Construct struct {
18+
Type ConstructType `yaml:"type"`
19+
URN string `yaml:"urn"`
20+
Version int `yaml:"version"`
21+
PulumiStack UUID `yaml:"pulumi_stack"`
22+
Status ConstructStatus `yaml:"status"`
23+
Inputs map[string]Input `yaml:"inputs"`
24+
Outputs map[string]string `yaml:"outputs"`
25+
Bindings []Binding `yaml:"bindings"`
26+
Options map[string]interface{} `yaml:"options"`
27+
DependsOn []string `yaml:"dependsOn"`
28+
}
29+
30+
type UUID struct {
31+
uuid.UUID
32+
}
33+
34+
func (u *UUID) UnmarshalYAML(unmarshal func(interface{}) error) error {
35+
var s string
36+
if err := unmarshal(&s); err != nil {
37+
return err
38+
}
39+
parsedUUID, err := uuid.Parse(s)
40+
if err != nil {
41+
return err
42+
}
43+
*u = UUID{parsedUUID}
44+
return nil
45+
}
46+
47+
type ConstructType string
48+
49+
const (
50+
ContainerType ConstructType = "klotho.aws.Container"
51+
// Add other construct types as needed
52+
)
53+
54+
type ConstructStatus string
55+
56+
const (
57+
New ConstructStatus = "new"
58+
Creating ConstructStatus = "creating"
59+
Created ConstructStatus = "created"
60+
Updating ConstructStatus = "updating"
61+
Updated ConstructStatus = "updated"
62+
Destroying ConstructStatus = "destroying"
63+
Destroyed ConstructStatus = "destroyed"
64+
CreateFailed ConstructStatus = "create_failed"
65+
UpdateFailed ConstructStatus = "update_failed"
66+
DestroyFailed ConstructStatus = "destroy_failed"
67+
UpdatePending ConstructStatus = "update_pending"
68+
DestroyPendingConstructStatus = "destroy_pending"
69+
)
70+
71+
type Input struct {
72+
Type string `yaml:"type"`
73+
Value interface{} `yaml:"value"`
74+
Encrypted bool `yaml:"encrypted"`
75+
Status InputStatus `yaml:"status,omitempty"`
76+
DependsOn []string `yaml:"dependsOn"`
77+
}
78+
79+
type InputStatus string
80+
81+
const (
82+
Pending InputStatus = "pending"
83+
Resolved InputStatus = "resolved"
84+
Error InputStatus = "error"
85+
)
86+
87+
type Binding struct {
88+
URN string `yaml:"urn"`
89+
BindingType string `yaml:"binding_type"`
90+
}
91+
92+
func ReadIRFile(filename string) (ApplicationEnvironment, error) {
93+
data, err := os.ReadFile(filename)
94+
if err != nil {
95+
return ApplicationEnvironment{}, err
96+
}
97+
98+
var appEnv ApplicationEnvironment
99+
err = yaml.Unmarshal(data, &appEnv)
100+
if err != nil {
101+
return ApplicationEnvironment{}, err
102+
}
103+
104+
return appEnv, nil
105+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
schemaVersion: '1.0'
2+
version: 1
3+
urn: 'urn:1234567890:my-project:dev:my-container-app'
4+
5+
constructs:
6+
my-container:
7+
type: klotho.aws.Container
8+
urn: 'urn:accountid:my-project:dev::construct/klotho.aws.Container:my-container'
9+
version: 1
10+
pulumi_stack: '4c3a0953-3d98-4215-ba09-533d98e215cd'
11+
status: creating
12+
inputs:
13+
image:
14+
type: string
15+
value: 'my-docker-image:latest'
16+
encrypted: false
17+
status: resolved
18+
dependsOn: []
19+
cpu:
20+
type: number
21+
value: 256
22+
encrypted: false
23+
status: resolved
24+
dependsOn: []
25+
outputs:
26+
taskArn: 'arn:aws:ecs:us-west-2:123456789012:task/my-cluster/1234567890123456789'
27+
bindings:
28+
- urn: 'urn:accountid:my-project:dev::construct/klotho.aws.Network:my-vpc'
29+
binding_type: vpc
30+
options:
31+
forceDeploy: true
32+
dependsOn:
33+
- 'urn:accountid:my-project:dev::construct/klotho.aws.Network:my-vpc'

0 commit comments

Comments
 (0)