Skip to content

Commit 1bb23a3

Browse files
asalkeldtjholm
authored andcommitted
feat: Add -s <stack> option to "stack describe"
Some other minor things: - "ctx" is used very widely for context.Context, seeing it in the code for a path is confusing - allow stderr when running the collection to help see errors - use the output.Print() function - add license headers - rename codeconfig.Stack to codeconfig.CodeConfig - store the function and stack names
1 parent 4c39a35 commit 1bb23a3

File tree

12 files changed

+421
-332
lines changed

12 files changed

+421
-332
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/google/go-github/v41 v41.0.0
1818
github.com/hashicorp/consul/sdk v0.8.0
1919
github.com/jedib0t/go-pretty v4.3.0+incompatible
20+
github.com/imdario/mergo v0.3.12
2021
github.com/jhoonb/archivex v0.0.0-20201016144719-6a343cdae81d
2122
github.com/mattn/go-runewidth v0.0.13 // indirect
2223
github.com/mitchellh/mapstructure v1.4.2

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
662662
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
663663
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
664664
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
665+
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
665666
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
666667
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
667668
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

pkg/build/build.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import (
2121
"os"
2222
"os/exec"
2323
"path"
24-
"strings"
2524

26-
"github.com/nitrictech/newcli/pkg/codeconfig"
2725
"github.com/nitrictech/newcli/pkg/containerengine"
2826
"github.com/nitrictech/newcli/pkg/functiondockerfile"
2927
"github.com/nitrictech/newcli/pkg/stack"
@@ -83,18 +81,12 @@ func Create(s *stack.Stack, t *target.Target) error {
8381
}
8482

8583
// CreateBaseDev builds images for code-as-config
86-
func CreateBaseDev(handlers []string) error {
84+
func CreateBaseDev(stackPath string, imagesToBuild map[string]string) error {
8785
ce, err := containerengine.Discover()
8886
if err != nil {
8987
return err
9088
}
9189

92-
imagesToBuild := map[string]string{}
93-
for _, h := range handlers {
94-
lang := strings.Replace(path.Ext(h), ".", "", 1)
95-
imagesToBuild[lang] = codeconfig.ImageNameFromExt(path.Ext(h))
96-
}
97-
9890
for lang, imageTag := range imagesToBuild {
9991
f, err := os.CreateTemp("", fmt.Sprintf("%s.*.dockerfile", lang))
10092
if err != nil {
@@ -110,7 +102,7 @@ func CreateBaseDev(handlers []string) error {
110102
return err
111103
}
112104

113-
if err := ce.Build(f.Name(), ".", imageTag, map[string]string{}); err != nil {
105+
if err := ce.Build(f.Name(), stackPath, imageTag, map[string]string{}); err != nil {
114106
return err
115107
}
116108
}

pkg/build/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import (
2828
func TestCreateBaseDev(t *testing.T) {
2929
ctrl := gomock.NewController(t)
3030
me := mock_containerengine.NewMockContainerEngine(ctrl)
31-
me.EXPECT().Build(gomock.Any(), ".", "nitric-ts-dev", map[string]string{})
31+
me.EXPECT().Build(gomock.Any(), "path/to/stack", "nitric-ts-dev", map[string]string{})
3232

3333
containerengine.MockEngine = me
3434

35-
if err := CreateBaseDev([]string{"foo.ts"}); err != nil {
35+
if err := CreateBaseDev("path/to/stack", map[string]string{"ts": "nitric-ts-dev"}); err != nil {
3636
t.Errorf("CreateBaseDev() error = %v", err)
3737
}
3838
}

pkg/cmd/stack/root.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/nitrictech/newcli/pkg/build"
3131
"github.com/nitrictech/newcli/pkg/codeconfig"
32+
"github.com/nitrictech/newcli/pkg/output"
3233
"github.com/nitrictech/newcli/pkg/stack"
3334
"github.com/nitrictech/newcli/pkg/templates"
3435
)
@@ -117,37 +118,33 @@ var stackDescribeCmd = &cobra.Command{
117118
Short: "describe stack dependencies",
118119
Long: `Describes stack dependencies`,
119120
Run: func(cmd *cobra.Command, args []string) {
120-
// Run collection on each of the files
121-
ctx, _ := filepath.Abs(".")
121+
stackPath, err := filepath.Abs(stack.StackPath())
122+
if err != nil {
123+
cobra.CheckErr(err)
124+
}
122125

123-
files, err := filepath.Glob(filepath.Join(ctx, args[0]))
126+
cc, err := codeconfig.New(stackPath, args[0])
124127
cobra.CheckErr(err)
125128

126-
fmt.Printf("found files: %v\n", files)
127-
128129
// Generate dev images to run on
129-
err = build.CreateBaseDev(files)
130+
err = build.CreateBaseDev(stackPath, cc.ImagesToBuild())
130131
cobra.CheckErr(err)
131132

132-
// Create a new stack
133-
stack := codeconfig.NewStack()
134-
135-
for _, f := range files {
136-
rel, _ := filepath.Rel(ctx, f)
137-
138-
if err := codeconfig.Collect(ctx, rel, stack); err != nil {
139-
fmt.Println(err)
140-
}
141-
}
133+
err = cc.Collect()
134+
cobra.CheckErr(err)
142135

143-
fmt.Println("stack:" + stack.String())
136+
s, err := cc.ToStack()
137+
cobra.CheckErr(err)
138+
output.Print(s)
144139
},
145140
Args: cobra.ExactArgs(1),
146141
}
147142

148143
func RootCommand() *cobra.Command {
149144
stackCreateCmd.Flags().BoolVarP(&force, "force", "f", false, "force stack creation, even in non-empty directories.")
150145
stackCmd.AddCommand(stackCreateCmd)
146+
147+
stack.AddOptions(stackDescribeCmd)
151148
stackCmd.AddCommand(stackDescribeCmd)
152149
return stackCmd
153150
}

0 commit comments

Comments
 (0)