From dada55f89eca0ef7db2e89c578637797b0965f09 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Thu, 25 May 2023 15:01:22 -0700 Subject: [PATCH] add github grouping of log lines see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines Signed-off-by: Joe Julian --- ct/cmd/root.go | 3 +++ pkg/chart/chart.go | 63 +++++++++++++++++++++++++++++++------------- pkg/config/config.go | 17 +++++++++--- pkg/util/util.go | 14 ++++++---- 4 files changed, 69 insertions(+), 28 deletions(-) diff --git a/ct/cmd/root.go b/ct/cmd/root.go index ea690041..5ec6fd2b 100644 --- a/ct/cmd/root.go +++ b/ct/cmd/root.go @@ -77,6 +77,9 @@ func addCommonFlags(flags *pflag.FlagSet) { Prints the configuration to stderr (caution: setting this may expose sensitive data when helm-repo-extra-args contains passwords)`)) flags.Bool("exclude-deprecated", false, "Skip charts that are marked as deprecated") + flags.Bool("github-groups", false, heredoc.Doc(` + Change the delimiters for github to create collapsible groups + for command output`)) } func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index f873ab42..352c8298 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -318,15 +318,23 @@ func (t *Testing) processCharts(action func(chart *Chart) TestResult) ([]TestRes } } - fmt.Println() - util.PrintDelimiterLineToWriter(os.Stdout, "-") - fmt.Println(" Charts to be processed:") - util.PrintDelimiterLineToWriter(os.Stdout, "-") + if !t.config.GithubGroups { + fmt.Println() + util.PrintDelimiterLineToWriter(os.Stdout, "-") + fmt.Println(" Charts to be processed:") + util.PrintDelimiterLineToWriter(os.Stdout, "-") + } else { + util.GithubGroupsBegin(os.Stdout, "Charts to be processed") + } for _, chart := range charts { fmt.Printf(" %s\n", chart) } - util.PrintDelimiterLineToWriter(os.Stdout, "-") - fmt.Println() + if !t.config.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stdout, "-") + fmt.Println() + } else { + util.GithubGroupsEnd(os.Stdout) + } repoArgs := map[string][]string{} @@ -414,7 +422,12 @@ func (t *Testing) LintAndInstallCharts() ([]TestResult, error) { // PrintResults writes test results to stdout. func (t *Testing) PrintResults(results []TestResult) { - util.PrintDelimiterLineToWriter(os.Stdout, "-") + if !t.config.GithubGroups { + fmt.Println() + util.PrintDelimiterLineToWriter(os.Stdout, "-") + } else { + util.GithubGroupsBegin(os.Stdout, "Test Results") + } if results != nil { for _, result := range results { err := result.Error @@ -427,7 +440,11 @@ func (t *Testing) PrintResults(results []TestResult) { } else { fmt.Println("No chart changes detected.") } - util.PrintDelimiterLineToWriter(os.Stdout, "-") + if !t.config.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stdout, "-") + } else { + util.GithubGroupsEnd(os.Stdout) + } } // LintChart lints the specified chart. @@ -882,7 +899,7 @@ func (t *Testing) ValidateMaintainers(chart *Chart) error { func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string) { util.PrintDelimiterLineToWriter(os.Stdout, "=") - printDetails(namespace, "Events of namespace", ".", func(item string) error { + t.printDetails(namespace, "Events of namespace", ".", func(item string) error { return t.kubectl.GetEvents(namespace) }, namespace) @@ -901,7 +918,7 @@ func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string } for _, pod := range pods { - printDetails(pod, "Description of pod", "~", func(item string) error { + t.printDetails(pod, "Description of pod", "~", func(item string) error { return t.kubectl.DescribePod(namespace, pod) }, pod) @@ -912,7 +929,7 @@ func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string } if t.config.PrintLogs { - printDetails(pod, "Logs of init container", "-", + t.printDetails(pod, "Logs of init container", "-", func(item string) error { return t.kubectl.Logs(namespace, pod, item) }, initContainers...) @@ -923,7 +940,7 @@ func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string return } - printDetails(pod, "Logs of container", "-", + t.printDetails(pod, "Logs of container", "-", func(item string) error { return t.kubectl.Logs(namespace, pod, item) }, @@ -934,21 +951,29 @@ func (t *Testing) PrintEventsPodDetailsAndLogs(namespace string, selector string util.PrintDelimiterLineToWriter(os.Stdout, "=") } -func printDetails(resource string, text string, delimiterChar string, printFunc func(item string) error, items ...string) { +func (t *Testing) printDetails(resource string, text string, delimiterChar string, printFunc func(item string) error, items ...string) { for _, item := range items { item = strings.Trim(item, "'") - util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) - fmt.Printf("==> %s %s\n", text, resource) - util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + if !t.config.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + fmt.Printf("==> %s %s\n", text, resource) + util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + } else { + util.GithubGroupsBegin(os.Stdout, fmt.Sprintf("%s %s", text, resource)) + } if err := printFunc(item); err != nil { fmt.Println("Error printing details:", err) return } - util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) - fmt.Printf("<== %s %s\n", text, resource) - util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + if !t.config.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + fmt.Printf("<== %s %s\n", text, resource) + util.PrintDelimiterLineToWriter(os.Stdout, delimiterChar) + } else { + util.GithubGroupsEnd(os.Stdout) + } } } diff --git a/pkg/config/config.go b/pkg/config/config.go index a020c7ed..114e2596 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -71,6 +71,7 @@ type Configuration struct { ExcludeDeprecated bool `mapstructure:"exclude-deprecated"` KubectlTimeout time.Duration `mapstructure:"kubectl-timeout"` PrintLogs bool `mapstructure:"print-logs"` + GithubGroups bool `mapstructure:"github-groups"` } func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*Configuration, error) { @@ -173,9 +174,13 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C } func printCfg(cfg *Configuration) { - util.PrintDelimiterLineToWriter(os.Stderr, "-") - fmt.Fprintln(os.Stderr, " Configuration") - util.PrintDelimiterLineToWriter(os.Stderr, "-") + if !cfg.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stderr, "-") + fmt.Fprintln(os.Stderr, " Configuration") + util.PrintDelimiterLineToWriter(os.Stderr, "-") + } else { + util.GithubGroupsBegin(os.Stderr, "Configuration") + } e := reflect.ValueOf(cfg).Elem() typeOfCfg := e.Type() @@ -191,7 +196,11 @@ func printCfg(cfg *Configuration) { fmt.Fprintf(os.Stderr, pattern, typeOfCfg.Field(i).Name, e.Field(i).Interface()) } - util.PrintDelimiterLineToWriter(os.Stderr, "-") + if !cfg.GithubGroups { + util.PrintDelimiterLineToWriter(os.Stderr, "-") + } else { + util.GithubGroupsEnd(os.Stderr) + } } func findConfigFile(fileName string) (string, error) { diff --git a/pkg/util/util.go b/pkg/util/util.go index 49579873..9189ebc7 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -219,11 +219,15 @@ func BreakingChangeAllowed(left string, right string) (bool, error) { } func PrintDelimiterLineToWriter(w io.Writer, delimiterChar string) { - delim := make([]string, 120) - for i := 0; i < 120; i++ { - delim[i] = delimiterChar - } - fmt.Fprintln(w, strings.Join(delim, "")) + fmt.Fprintln(w, strings.Repeat(delimiterChar, 120)) +} + +func GithubGroupsBegin(w io.Writer, title string) { + fmt.Fprintf(w, "::group::%s\n", title) +} + +func GithubGroupsEnd(w io.Writer) { + fmt.Fprintln(w, "::endgroup::") } func SanitizeName(s string, maxLength int) string {