Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit cdc5cfd

Browse files
authored
feat: enable pretty output (#16)
* feat: enable pretty output by default * feat: allow granular output control with `--output-colors` (colors not implemented yet) and `--output-ascii` * fix: rbac: remove duplicated resource names in output
1 parent fc1b142 commit cdc5cfd

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

internal/checks/kube/rbac.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,27 @@ func (k *KubernetesChecker) checkRBACFallback(ctx context.Context) []*api.CheckR
168168
results := make([]*api.CheckResult, 0)
169169

170170
for req, reqVerbs := range k.reqs.ResourceRequirements {
171-
resName := fmt.Sprintf("%s-%s", checkName, req.Resource)
172171
if err := k.checkOneRBACSSAR(ctx, authClient, req, reqVerbs); err != nil {
173172
summary := fmt.Sprintf("missing permissions on resource %s: %s", req.Resource, err)
174-
results = append(results, api.ErrorResult(resName, summary, err))
173+
results = append(results, api.ErrorResult(checkName, summary, err))
175174
continue
176175
}
177176

178177
summary := fmt.Sprintf("%s: can %s", req.Resource, strings.Join(reqVerbs, ", "))
179-
results = append(results, api.PassResult(resName, summary))
178+
results = append(results, api.PassResult(checkName, summary))
180179
}
181180

182181
// TODO: delete this when the enterprise-helm role no longer requests resources on things
183182
// that don't exist.
184183
for req, reqVerbs := range k.reqs.RoleOnlyResourceRequirements {
185-
resName := fmt.Sprintf("%s-%s", checkName, req.Resource)
186184
if err := k.checkOneRBACSSAR(ctx, authClient, req, reqVerbs); err != nil {
187185
summary := fmt.Sprintf("missing permissions on resource %s: %s", req.Resource, err)
188-
results = append(results, api.ErrorResult(resName, summary, err))
186+
results = append(results, api.ErrorResult(checkName, summary, err))
189187
continue
190188
}
191189

192190
summary := fmt.Sprintf("%s: can %s", req.Resource, strings.Join(reqVerbs, ", "))
193-
results = append(results, api.PassResult(resName, summary))
191+
results = append(results, api.PassResult(checkName, summary))
194192
}
195193

196194
return results

internal/cmd/check/kubernetes/kubernetes.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kubernetes
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/Masterminds/semver/v3"
@@ -120,30 +121,54 @@ func run(cmd *cobra.Command, _ []string) error {
120121
currentContext.Namespace = "default"
121122
}
122123

123-
log.Info(cmd.Context(), "kubernetes config:",
124-
slog.F("context", rawConfig.CurrentContext),
125-
slog.F("cluster", currentContext.Cluster),
126-
slog.F("namespace", currentContext.Namespace),
127-
slog.F("authinfo", currentContext.AuthInfo),
128-
)
124+
colorFlag, err := cmd.Flags().GetBool("output-colors")
125+
if err != nil {
126+
return xerrors.Errorf("parse output-color: %w", err)
127+
}
128+
129+
asciiFlag, err := cmd.Flags().GetBool("output-ascii")
130+
if err != nil {
131+
return xerrors.Errorf("parse output-ascii: %w", err)
132+
}
129133

130-
hw := humanwriter.New(os.Stdout)
134+
outputMode := humanwriter.OutputModeEmoji
135+
if asciiFlag {
136+
outputMode = humanwriter.OutputModeText
137+
}
138+
139+
var writer api.ResultWriter = humanwriter.New(
140+
os.Stdout,
141+
humanwriter.WithColors(colorFlag),
142+
humanwriter.WithMode(outputMode),
143+
)
131144

132145
localChecker := local.NewChecker(
133146
local.WithLogger(log),
134147
local.WithCoderVersion(cv),
135-
local.WithWriter(hw),
148+
local.WithWriter(writer),
136149
local.WithTarget(api.CheckTargetKubernetes),
137150
)
138151

139152
kubeChecker := kube.NewKubernetesChecker(
140153
clientset,
141154
kube.WithLogger(log),
142155
kube.WithCoderVersion(cv),
143-
kube.WithWriter(hw),
156+
kube.WithWriter(writer),
144157
kube.WithNamespace(currentContext.Namespace),
145158
)
146159

160+
_ = writer.WriteResult(&api.CheckResult{
161+
Name: "kubernetes current-context",
162+
State: api.StateInfo,
163+
Summary: fmt.Sprintf("kube context: %q", rawConfig.CurrentContext),
164+
Details: map[string]interface{}{
165+
"current-context": rawConfig.CurrentContext,
166+
"cluster": currentContext.Cluster,
167+
"namespace": currentContext.Namespace,
168+
"user": currentContext.AuthInfo,
169+
},
170+
})
171+
147172
if err := localChecker.Validate(); err != nil {
148173
return xerrors.Errorf("failed to validate local checks: %w", err)
149174
}

internal/cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ func NewDefaultDoctorCommand() *cobra.Command {
1919
check.NewCommand(),
2020
)
2121

22+
rootCmd.PersistentFlags().Bool("output-colors", true, "enable colorful output")
23+
rootCmd.PersistentFlags().Bool("output-ascii", false, "output ascii only")
24+
2225
return rootCmd
2326
}

internal/humanwriter/human.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ func (w *HumanResultWriter) WriteResult(result *api.CheckResult) error {
8282
return err
8383
}
8484

85-
_, err = fmt.Fprintln(w.out, prefix, result.Summary)
85+
_, err = fmt.Fprintf(w.out, "%s %s\n", prefix, result.Summary)
8686
return err
8787
}

0 commit comments

Comments
 (0)