Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add core property for setting quiet. #121

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/121.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
profile: Add a core/quiet property which allows disabling prompting in the profile.
```
1 change: 1 addition & 0 deletions internal/commands/profile/property_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func addCoreProperties(b *availablePropertiesBuilder) {
This can be overridden by using the global {{ template "mdCodeOrBold" "--project" }} flag.`)

b.AddProperty("core", "no_color", "If True, color will not be used when printing messages in the terminal.")
b.AddProperty("core", "quiet", "If True, prompts will be disabled and output will be minimized.")
b.AddProperty("core", "verbosity", `
Default logging verbosity for {{ template "mdCodeOrBold" "hcp" }} commands. This is the
equivalent of using the global {{ template "mdCodeOrBold" "--verbosity" }} flag. Supported log levels:
Expand Down
18 changes: 9 additions & 9 deletions internal/pkg/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ func ConfigureRootCommand(ctx *Context, cmd *Command) {
Name: "project",
DisplayValue: "ID",
Description: "The HCP Project ID to use. If omitted the current project set in the configuration is used.",
Value: flagvalue.Simple[string]("", &ctx.flags.project),
Value: flagvalue.Simple("", &ctx.flags.project),
global: true,
Autocomplete: complete.PredictAnything,
}, &Flag{
Name: "profile",
DisplayValue: "NAME",
Description: "The profile to use. If omitted, the currently selected profile will be used.",
Value: flagvalue.Simple[string]("", &ctx.flags.profile),
Value: flagvalue.Simple("", &ctx.flags.profile),
global: true,
Autocomplete: complete.PredictFunc(func(_ complete.Args) []string {
l, err := profile.NewLoader()
Expand All @@ -112,13 +112,13 @@ func ConfigureRootCommand(ctx *Context, cmd *Command) {
Name: "format",
DisplayValue: "FORMAT",
Description: "Sets the output format.",
Value: flagvalue.Enum[string](formats, "", &ctx.flags.format),
Value: flagvalue.Enum(formats, "", &ctx.flags.format),
global: true,
Autocomplete: complete.PredictSet(formats...),
}, &Flag{
Name: "quiet",
Description: "Minimizes output and disables interactive prompting.",
Value: flagvalue.Simple[bool](false, &ctx.flags.Quiet),
Value: flagvalue.Simple(false, &ctx.flags.Quiet),
IsBooleanFlag: true,
global: true,
Autocomplete: complete.PredictNothing,
Expand All @@ -137,7 +137,7 @@ func ConfigureRootCommand(ctx *Context, cmd *Command) {
ctx.HCP.SetLogger(newAPILogger(c.Logger()))
ctx.HCP.Debug = true

if err := ctx.applyGlobalFlags(c, args); err != nil {
if err := ctx.applyGlobalFlags(c); err != nil {
return err
}

Expand All @@ -146,7 +146,7 @@ func ConfigureRootCommand(ctx *Context, cmd *Command) {
}

// applyGlobalFlags applies the global flags.
func (ctx *Context) applyGlobalFlags(c *Command, args []string) error {
func (ctx *Context) applyGlobalFlags(c *Command) error {
// Mark that we have parsed flags
ctx.flags.parsed = true

Expand Down Expand Up @@ -210,8 +210,8 @@ func (ctx *Context) applyGlobalFlags(c *Command, args []string) error {
ctx.IO.ForceNoColor()
}

// Set quiet on the IOStream if enabled.
if ctx.flags.Quiet {
// Set quiet on the IOStream if enabled by the flag or profile
if ctx.flags.Quiet || ctx.Profile.Core.IsQuiet() {
ctx.IO.SetQuiet(true)
}

Expand All @@ -226,7 +226,7 @@ func (ctx *Context) ParseFlags(c *Command, args []string) ([]string, error) {
return nil, err
}

if err := ctx.applyGlobalFlags(c, args); err != nil {
if err := ctx.applyGlobalFlags(c); err != nil {
return nil, err
}

Expand Down
20 changes: 19 additions & 1 deletion internal/pkg/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ type Core struct {
// NoColor disables color output
NoColor *bool `hcl:"no_color,optional" json:",omitempty"`

// Quiet disables prompting for user input and minimizes output.
Quiet *bool `hcl:"quiet,optional" json:",omitempty"`

// OutputFormat dictates the default output format if unspecified.
OutputFormat *string `hcl:"output_format,optional" json:",omitempty"`

Expand All @@ -254,6 +257,7 @@ type Core struct {
func (c *Core) Predict(args complete.Args) []string {
properties := map[string][]string{
"core/no_color": {"true", "false"},
"core/quiet": {"true", "false"},
"core/output_format": {"pretty", "table", "json"},
"core/verbosity": {"trace", "debug", "info", "warn", "error"},
}
Expand Down Expand Up @@ -300,7 +304,8 @@ func (c *Core) isEmpty() bool {
return true
}

if c.NoColor != nil || c.OutputFormat != nil || c.Verbosity != nil {
if c.NoColor != nil || c.OutputFormat != nil || c.Verbosity != nil ||
c.Quiet != nil {
return false
}

Expand Down Expand Up @@ -334,3 +339,16 @@ func (c *Core) GetVerbosity() string {

return *c.Verbosity
}

// IsQueit returns whether the quiet property has been configured to be quiet.
func (c *Core) IsQuiet() bool {
if c == nil {
return false
}

if c.Quiet == nil {
return false
}

return *c.Quiet
}
12 changes: 3 additions & 9 deletions internal/pkg/profile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestPropertyNames(t *testing.T) {
r.Contains(properties, "organization_id")
r.Contains(properties, "project_id")
r.Contains(properties, "core/no_color")
r.Contains(properties, "core/quiet")
r.Contains(properties, "core/verbosity")
r.Contains(properties, "vault-secrets/app")
}
Expand Down Expand Up @@ -111,14 +112,7 @@ func TestProfile_Predict(t *testing.T) {
Args: complete.Args{
All: []string{"core/"},
},
Expected: []string{"core/no_color", "core/output_format", "core/verbosity"},
},
{
Name: "core",
Args: complete.Args{
All: []string{"core/"},
},
Expected: []string{"core/no_color", "core/output_format", "core/verbosity"},
Expected: []string{"core/no_color", "core/output_format", "core/quiet", "core/verbosity"},
},
{
Name: "vault-secrets",
Expand Down Expand Up @@ -207,7 +201,7 @@ func TestCore_Predict(t *testing.T) {
Args: complete.Args{
All: []string{"core/"},
},
Expected: []string{"core/no_color", "core/output_format", "core/verbosity"},
Expected: []string{"core/no_color", "core/output_format", "core/quiet", "core/verbosity"},
},
{
Name: "no_color",
Expand Down
Loading