Skip to content

Commit 7694ac9

Browse files
committed
some moving
1 parent 822138a commit 7694ac9

18 files changed

Lines changed: 382 additions & 4 deletions

cmd/charts_genchangelog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func runChartsGenChangelog(args []string) error {
4444
PrettyJSON: true,
4545
StatusUpdateInterval: 5,
4646
SkipCommitsWithBadMessage: false,
47+
AppType: changelog.AppTypeChart,
4748
}
4849
if err := chartsGenChangelogGenerate(opts); err != nil {
4950
return fmt.Errorf("generate changelog: %w", err)

cmd/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ These are all commands that can be used to test container images
1111
`)
1212

1313
var containerCmd = &cobra.Command{
14-
Use: "container",
14+
Use: "containers",
1515
Short: "Commands for container validation",
16-
Example: "forgetool container test",
16+
Example: "forgetool containers test",
1717
Long: containerLongHelp,
1818
SilenceUsage: true,
1919
SilenceErrors: true,

cmd/container_genlist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var genContainerListCmd = &cobra.Command{
6868
Use: "gencontainerlist",
6969
Short: "Generate container list json file",
7070
Long: containerGenListLongHelp,
71-
Example: "forgetool container gencontainerlist <path to apps folder>",
71+
Example: "forgetool containers gencontainerlist <path to apps folder>",
7272
Run: func(cmd *cobra.Command, args []string) {
7373
if err := containerGenListRunner(args); err != nil {
7474
containerGenListOnError(err)

cmd/container_testcmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func runContainerTest(ctx context.Context, image string, configPath string, envP
5959
var containerTestCmd = &cobra.Command{
6060
Use: "test",
6161
Short: "Run container tests from YAML configuration",
62-
Example: "forgetool container test --image ghcr.io/trueforge-org/myimage:latest --config ./container-test.yaml",
62+
Example: "forgetool containers test --image ghcr.io/trueforge-org/myimage:latest --config ./container-test.yaml",
6363
Long: containerTestLongHelp,
6464
Args: cobra.NoArgs,
6565
RunE: func(cmd *cobra.Command, args []string) error {

cmd/containers.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var containerLongHelp = strings.TrimSpace(`
10+
These are all commands that can be used to test container images
11+
`)
12+
13+
var containerCmd = &cobra.Command{
14+
Use: "containers",
15+
Short: "Commands for container validation",
16+
Example: "forgetool containers test",
17+
Long: containerLongHelp,
18+
SilenceUsage: true,
19+
SilenceErrors: true,
20+
}
21+
22+
func init() {
23+
RootCmd.AddCommand(containerCmd)
24+
}

cmd/containers_genchangelog.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/rs/zerolog/log"
9+
10+
"github.com/spf13/cobra"
11+
"github.com/trueforge-org/forgetool/pkg/changelog"
12+
)
13+
14+
var containerGenChangelogLongHelp = strings.TrimSpace(`
15+
16+
`)
17+
18+
func defaultContainerGenChangelogGenerate(opts *changelog.ChangelogOptions) error {
19+
return opts.Generate()
20+
}
21+
22+
func defaultContainerGenChangelogRender(opts *changelog.ChangelogOptions) error {
23+
return opts.Render()
24+
}
25+
26+
var (
27+
containerGenChangelogGenerate = defaultContainerGenChangelogGenerate
28+
containerGenChangelogRender = defaultContainerGenChangelogRender
29+
containerGenChangelogRunner = runContainerGenChangelog
30+
containerGenChangelogOnError = func(err error) { log.Fatal().Err(err) }
31+
)
32+
33+
func runContainerGenChangelog(args []string) error {
34+
if len(args) < 3 {
35+
return errors.New("missing required arguments. please provide the repo path, template path and apps directory")
36+
}
37+
38+
opts := &changelog.ChangelogOptions{
39+
RepoPath: args[0],
40+
TemplatePath: args[1],
41+
AppsDir: args[2],
42+
ChangelogFileName: "CHANGELOG.md",
43+
JSONOutputPath: "./changelog.json",
44+
PrettyJSON: true,
45+
StatusUpdateInterval: 5,
46+
SkipCommitsWithBadMessage: false,
47+
AppType: changelog.AppTypeContainer,
48+
}
49+
if err := containerGenChangelogGenerate(opts); err != nil {
50+
return fmt.Errorf("generate changelog: %w", err)
51+
}
52+
if err := containerGenChangelogRender(opts); err != nil {
53+
return fmt.Errorf("render changelog: %w", err)
54+
}
55+
56+
return nil
57+
}
58+
59+
var containerGenChangelogCmd = &cobra.Command{
60+
Use: "genchangelog",
61+
Short: "Generate changelog for containers",
62+
Long: containerGenChangelogLongHelp,
63+
Example: "forgetool containers genchangelog <repo path> <template path> <apps dir>",
64+
Run: func(cmd *cobra.Command, args []string) {
65+
if err := containerGenChangelogRunner(args); err != nil {
66+
containerGenChangelogOnError(err)
67+
}
68+
},
69+
}
70+
71+
func init() {
72+
containerCmd.AddCommand(containerGenChangelogCmd)
73+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/trueforge-org/forgetool/pkg/changelog"
10+
)
11+
12+
func TestRunContainerGenChangelog(t *testing.T) {
13+
oldGenerate := containerGenChangelogGenerate
14+
oldRender := containerGenChangelogRender
15+
t.Cleanup(func() {
16+
containerGenChangelogGenerate = oldGenerate
17+
containerGenChangelogRender = oldRender
18+
})
19+
20+
if err := runContainerGenChangelog([]string{"only", "two"}); err == nil {
21+
t.Fatalf("expected missing-args error")
22+
}
23+
24+
containerGenChangelogGenerate = func(opts *changelog.ChangelogOptions) error {
25+
if opts.RepoPath != "repo" || opts.TemplatePath != "tmpl" || opts.AppsDir != "apps" {
26+
t.Fatalf("unexpected options: %#v", opts)
27+
}
28+
if opts.AppType != changelog.AppTypeContainer {
29+
t.Fatalf("expected AppTypeContainer, got %s", opts.AppType)
30+
}
31+
return nil
32+
}
33+
containerGenChangelogRender = func(*changelog.ChangelogOptions) error { return nil }
34+
if err := runContainerGenChangelog([]string{"repo", "tmpl", "apps"}); err != nil {
35+
t.Fatalf("unexpected error: %v", err)
36+
}
37+
38+
genErr := errors.New("gen failed")
39+
containerGenChangelogGenerate = func(*changelog.ChangelogOptions) error { return genErr }
40+
if err := runContainerGenChangelog([]string{"repo", "tmpl", "apps"}); err == nil || !strings.Contains(err.Error(), "generate changelog") {
41+
t.Fatalf("expected wrapped generate error, got %v", err)
42+
}
43+
44+
containerGenChangelogGenerate = func(*changelog.ChangelogOptions) error { return nil }
45+
rendErr := errors.New("render failed")
46+
containerGenChangelogRender = func(*changelog.ChangelogOptions) error { return rendErr }
47+
if err := runContainerGenChangelog([]string{"repo", "tmpl", "apps"}); err == nil || !strings.Contains(err.Error(), "render changelog") {
48+
t.Fatalf("expected wrapped render error, got %v", err)
49+
}
50+
}
51+
52+
func TestContainerGenChangelogCommandRunCallsRunner(t *testing.T) {
53+
oldRunner := containerGenChangelogRunner
54+
t.Cleanup(func() { containerGenChangelogRunner = oldRunner })
55+
56+
called := false
57+
containerGenChangelogRunner = func(args []string) error {
58+
called = true
59+
if len(args) != 3 || args[0] != "repo" || args[1] != "tmpl" || args[2] != "apps" {
60+
t.Fatalf("unexpected args: %#v", args)
61+
}
62+
return nil
63+
}
64+
65+
containerGenChangelogCmd.Run(containerGenChangelogCmd, []string{"repo", "tmpl", "apps"})
66+
67+
if !called {
68+
t.Fatalf("expected command Run to call runner")
69+
}
70+
}
71+
72+
func TestContainerGenChangelogCommandRunCallsOnError(t *testing.T) {
73+
oldRunner := containerGenChangelogRunner
74+
oldOnError := containerGenChangelogOnError
75+
t.Cleanup(func() {
76+
containerGenChangelogRunner = oldRunner
77+
containerGenChangelogOnError = oldOnError
78+
})
79+
80+
want := errors.New("boom")
81+
containerGenChangelogRunner = func([]string) error { return want }
82+
called := false
83+
containerGenChangelogOnError = func(err error) {
84+
called = true
85+
if !errors.Is(err, want) {
86+
t.Fatalf("unexpected error: %v", err)
87+
}
88+
}
89+
90+
containerGenChangelogCmd.Run(containerGenChangelogCmd, []string{"repo", "tmpl", "apps"})
91+
92+
if !called {
93+
t.Fatalf("expected command Run to call error handler")
94+
}
95+
}
96+
97+
func TestDefaultContainerChangelogWrappersReturnErrorsOnInvalidInput(t *testing.T) {
98+
oldGenerate := containerGenChangelogGenerate
99+
oldRender := containerGenChangelogRender
100+
t.Cleanup(func() {
101+
containerGenChangelogGenerate = oldGenerate
102+
containerGenChangelogRender = oldRender
103+
})
104+
105+
containerGenChangelogGenerate = defaultContainerGenChangelogGenerate
106+
containerGenChangelogRender = defaultContainerGenChangelogRender
107+
108+
bad := &changelog.ChangelogOptions{
109+
RepoPath: "",
110+
TemplatePath: filepath.Join("..", "does-not-exist"),
111+
AppsDir: "",
112+
ChangelogFileName: "CHANGELOG.md",
113+
JSONOutputPath: filepath.Join(t.TempDir(), "changelog.json"),
114+
StatusUpdateInterval: 1,
115+
SkipCommitsWithBadMessage: false,
116+
AppType: changelog.AppTypeContainer,
117+
}
118+
119+
if err := containerGenChangelogGenerate(bad); err == nil {
120+
t.Fatalf("expected generate wrapper to return error for invalid options")
121+
}
122+
_ = containerGenChangelogRender(bad)
123+
}

cmd/containers_genlist.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io/fs"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/rs/zerolog/log"
10+
"github.com/spf13/cobra"
11+
"github.com/trueforge-org/forgetool/pkg/containers/website"
12+
)
13+
14+
var containerGenListLongHelp = strings.TrimSpace(`
15+
Generates a JSON file listing all containers found under the given path(s).
16+
Each container is identified by its docker-bake.hcl file.
17+
`)
18+
19+
func defaultContainerGenListOptionsFactory() *website.ContainerListOptions {
20+
return &website.ContainerListOptions{
21+
OutputPath: "./containers.json",
22+
}
23+
}
24+
25+
func defaultContainerGenListGetData(opts *website.ContainerListOptions) fs.WalkDirFunc {
26+
return opts.GetContainerData
27+
}
28+
29+
func defaultContainerGenListWrite(opts *website.ContainerListOptions) error {
30+
return opts.WriteContainerList()
31+
}
32+
33+
var defaultContainerGenListWalk = func(paths []string, fn fs.WalkDirFunc) error {
34+
if len(paths) == 0 {
35+
paths = []string{"./apps"}
36+
}
37+
for _, dir := range paths {
38+
if err := filepath.WalkDir(dir, fn); err != nil {
39+
return fmt.Errorf("error walking directory %s: %w", dir, err)
40+
}
41+
}
42+
return nil
43+
}
44+
45+
var (
46+
containerGenListWalk = defaultContainerGenListWalk
47+
containerGenListOptionsFactory = defaultContainerGenListOptionsFactory
48+
containerGenListGetData = defaultContainerGenListGetData
49+
containerGenListWrite = defaultContainerGenListWrite
50+
containerGenListRunner = runContainerGenList
51+
containerGenListOnError = func(err error) { log.Fatal().Err(err).Msg("container list generation failed") }
52+
)
53+
54+
func runContainerGenList(args []string) error {
55+
opts := containerGenListOptionsFactory()
56+
if err := containerGenListWalk(args, containerGenListGetData(opts)); err != nil {
57+
return fmt.Errorf("failed to generate container list json file: %w", err)
58+
}
59+
60+
if err := containerGenListWrite(opts); err != nil {
61+
return fmt.Errorf("failed to write container list json file: %w", err)
62+
}
63+
64+
return nil
65+
}
66+
67+
var genContainerListCmd = &cobra.Command{
68+
Use: "gencontainerlist",
69+
Short: "Generate container list json file",
70+
Long: containerGenListLongHelp,
71+
Example: "forgetool containers gencontainerlist <path to apps folder>",
72+
Run: func(cmd *cobra.Command, args []string) {
73+
if err := containerGenListRunner(args); err != nil {
74+
containerGenListOnError(err)
75+
}
76+
},
77+
}
78+
79+
func init() {
80+
containerCmd.AddCommand(genContainerListCmd)
81+
}

0 commit comments

Comments
 (0)