Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
- Add clearer message for stdin error handling
- Exit immediately with code 2 on template errors
(to prevent the CLI usage from displaying)

Signed-off-by: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com>
  • Loading branch information
dhaiducek authored and openshift-merge-bot[bot] committed Oct 2, 2024
1 parent 03f6bb7 commit 697c3d1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions cmd/template-resolver/utils/resolver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type TemplateResolver struct {
func (t *TemplateResolver) GetCmd() *cobra.Command {
// templateResolverCmd represents the template-resolver command
templateResolverCmd := &cobra.Command{
Use: `template-resolver [flags] [file|-]
Use: `template-resolver [flags] [file|-]
The file positional argument is the path to a policy YAML manifest. If file is a dash ('-') or absent, template-resolver reads from the standard input.`,
The file positional argument is the path to a policy YAML manifest. If file
is a dash ('-') or absent, template-resolver reads from the standard input.`,
Short: "Locally resolve Policy templates",
Long: "Locally resolve Policy templates",
Args: cobra.MaximumNArgs(1),
Expand All @@ -38,7 +39,7 @@ func (t *TemplateResolver) GetCmd() *cobra.Command {
&t.clusterName,
"cluster-name",
"",
"the cluster name to use for the .ManagedClusterName template variable when resolving hub templates",
"the cluster name to use for the .ManagedClusterName template variable when resolving hub cluster templates",
)
templateResolverCmd.Flags().StringVar(
&t.hubNamespace,
Expand All @@ -62,9 +63,7 @@ func (t *TemplateResolver) resolveTemplates(cmd *cobra.Command, args []string) e
}

if (stdinInfo.Mode() & os.ModeCharDevice) != 0 {
err := cmd.Usage()

return err
return fmt.Errorf("failed to read from stdin: input is not a pipe")
}
}

Expand All @@ -88,11 +87,13 @@ func (t *TemplateResolver) resolveTemplates(cmd *cobra.Command, args []string) e

resolvedYAML, err := ProcessTemplate(yamlBytes, t.hubKubeConfigPath, t.clusterName, t.hubNamespace)
if err != nil {
return fmt.Errorf("error processing templates: %w", err)
cmd.Printf("error processing templates: %s\n", err.Error())

os.Exit(2)
}

//nolint:forbidigo
fmt.Print(string(resolvedYAML))
cmd.SetOut(os.Stdout)
cmd.Print(string(resolvedYAML))

return nil
}
Expand Down

0 comments on commit 697c3d1

Please sign in to comment.