Skip to content

Commit b2bc126

Browse files
committed
feat: new release history
Signed-off-by: Dmitry Mordvinov <dmitry.mordvinov@flant.com>
1 parent d2c1e8a commit b2bc126

3 files changed

Lines changed: 431 additions & 21 deletions

File tree

cmd/nelm/release_history.go

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,125 @@
11
package main
22

33
import (
4+
"cmp"
45
"context"
5-
"strings"
6+
"fmt"
67

7-
"github.com/samber/lo"
88
"github.com/spf13/cobra"
99

1010
"github.com/werf/common-go/pkg/cli"
1111
"github.com/werf/nelm/pkg/action"
12-
"github.com/werf/nelm/pkg/helm/pkg/chart/loader"
13-
helmcmd "github.com/werf/nelm/pkg/helm/pkg/cmd"
12+
"github.com/werf/nelm/pkg/common"
1413
"github.com/werf/nelm/pkg/log"
1514
)
1615

16+
type releaseHistoryConfig struct {
17+
action.ReleaseHistoryOptions
18+
19+
LogColorMode string
20+
LogLevel string
21+
ReleaseName string
22+
ReleaseNamespace string
23+
}
24+
1725
func newReleaseHistoryCommand(ctx context.Context, afterAllCommandsBuiltFuncs map[*cobra.Command]func(cmd *cobra.Command) error) *cobra.Command {
18-
cmd := lo.Must(lo.Find(helmRootCmd.Commands(), func(c *cobra.Command) bool {
19-
return strings.HasPrefix(c.Use, "history")
20-
}))
26+
cfg := &releaseHistoryConfig{}
27+
28+
cmd := cli.NewSubCommand(
29+
ctx,
30+
"history [options...] -n namespace -r release ",
31+
"Show release history.",
32+
"Show release history.",
33+
40,
34+
releaseCmdGroup,
35+
cli.SubCommandOptions{},
36+
func(cmd *cobra.Command, args []string) error {
37+
ctx = action.SetupLogging(ctx, cmp.Or(log.Level(cfg.LogLevel), action.DefaultReleaseHistoryLogLevel), action.SetupLoggingOptions{
38+
ColorMode: cfg.LogColorMode,
39+
LogIsParseable: true,
40+
})
41+
42+
if _, err := action.ReleaseHistory(ctx, cfg.ReleaseName, cfg.ReleaseNamespace, cfg.ReleaseHistoryOptions); err != nil {
43+
return fmt.Errorf("release history: %w", err)
44+
}
45+
46+
return nil
47+
},
48+
)
49+
50+
afterAllCommandsBuiltFuncs[cmd] = func(cmd *cobra.Command) error {
51+
if err := AddKubeConnectionFlags(cmd, &cfg.KubeConnectionOptions); err != nil {
52+
return fmt.Errorf("add kube connection flags: %w", err)
53+
}
54+
55+
// TODO: restrict values
56+
if err := cli.AddFlag(cmd, &cfg.OutputFormat, "output-format", action.DefaultReleaseHistoryOutputFormat, "Result output format", cli.AddFlagOptions{
57+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
58+
Group: miscFlagGroup,
59+
}); err != nil {
60+
return fmt.Errorf("add flag: %w", err)
61+
}
2162

22-
cmd.LocalFlags().AddFlagSet(cmd.InheritedFlags())
23-
cmd.Short = "Show release history."
24-
cmd.Aliases = []string{}
25-
cli.SetSubCommandAnnotations(cmd, 30, releaseCmdGroup)
63+
if err := cli.AddFlag(cmd, &cfg.ReleaseName, "release", "", "The release name. Must be unique within the release namespace", cli.AddFlagOptions{
64+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
65+
Group: mainFlagGroup,
66+
Required: true,
67+
ShortName: "r",
68+
}); err != nil {
69+
return fmt.Errorf("add flag: %w", err)
70+
}
71+
72+
if err := cli.AddFlag(cmd, &cfg.Max, "max", 0, "Maximum number of revisions to show. 0 means no limit", cli.AddFlagOptions{
73+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
74+
Group: mainFlagGroup,
75+
}); err != nil {
76+
return fmt.Errorf("add flag: %w", err)
77+
}
78+
79+
if err := cli.AddFlag(cmd, &cfg.ReleaseNamespace, "namespace", "", "The release namespace. Resources with no namespace will be deployed here", cli.AddFlagOptions{
80+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
81+
Group: mainFlagGroup,
82+
Required: true,
83+
ShortName: "n",
84+
}); err != nil {
85+
return fmt.Errorf("add flag: %w", err)
86+
}
2687

27-
originalRunE := cmd.RunE
28-
cmd.RunE = func(cmd *cobra.Command, args []string) error {
29-
helmSettings := helmcmd.Settings
88+
// TODO: restrict allowed values
89+
if err := cli.AddFlag(cmd, &cfg.ReleaseStorageDriver, "release-storage", "", "How releases should be stored", cli.AddFlagOptions{
90+
GetEnvVarRegexesFunc: cli.GetFlagGlobalEnvVarRegexes,
91+
Group: miscFlagGroup,
92+
}); err != nil {
93+
return fmt.Errorf("add flag: %w", err)
94+
}
3095

31-
ctx = action.SetupLogging(ctx, lo.Ternary(helmSettings.Debug, log.DebugLevel, log.InfoLevel), action.SetupLoggingOptions{})
96+
if err := cli.AddFlag(cmd, &cfg.ReleaseStorageSQLConnection, "release-storage-sql-connection", "", "SQL connection string for MySQL release storage driver", cli.AddFlagOptions{
97+
GetEnvVarRegexesFunc: cli.GetFlagGlobalEnvVarRegexes,
98+
Group: miscFlagGroup,
99+
}); err != nil {
100+
return fmt.Errorf("add flag: %w", err)
101+
}
32102

33-
loader.NoChartLockWarning = ""
103+
if err := cli.AddFlag(cmd, &cfg.TempDirPath, "temp-dir", "", "The directory for temporary files. By default, create a new directory in the default system directory for temporary files", cli.AddFlagOptions{
104+
GetEnvVarRegexesFunc: cli.GetFlagGlobalEnvVarRegexes,
105+
Group: miscFlagGroup,
106+
Type: cli.FlagTypeDir,
107+
}); err != nil {
108+
return fmt.Errorf("add flag: %w", err)
109+
}
110+
111+
if err := cli.AddFlag(cmd, &cfg.LogColorMode, "color-mode", common.DefaultLogColorMode, "Color mode for logs. "+allowedLogColorModesHelp(), cli.AddFlagOptions{
112+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
113+
Group: miscFlagGroup,
114+
}); err != nil {
115+
return fmt.Errorf("add flag: %w", err)
116+
}
34117

35-
if err := originalRunE(cmd, args); err != nil {
36-
return err
118+
if err := cli.AddFlag(cmd, &cfg.LogLevel, "log-level", string(action.DefaultReleaseHistoryLogLevel), "Set log level. "+allowedLogLevelsHelp(), cli.AddFlagOptions{
119+
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
120+
Group: miscFlagGroup,
121+
}); err != nil {
122+
return fmt.Errorf("add flag: %w", err)
37123
}
38124

39125
return nil

0 commit comments

Comments
 (0)