Skip to content

Commit

Permalink
add github grouping of log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
joejulian committed May 26, 2023
1 parent 08d0052 commit dada55f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
3 changes: 3 additions & 0 deletions ct/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
63 changes: 44 additions & 19 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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...)
Expand All @@ -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)
},
Expand All @@ -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)
}
}
}
17 changes: 13 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dada55f

Please sign in to comment.