-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global --quiet flag to hide non-error messages (#656)
Closes #644
- Loading branch information
Showing
121 changed files
with
829 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
) | ||
|
||
type fakeResource struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
var fakeCreateCmd = &base.CreateCmd{ | ||
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "create", | ||
} | ||
}, | ||
Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) { | ||
cmd.Println("Creating fake resource") | ||
|
||
resource := &fakeResource{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
return resource, util.Wrap("resource", resource), nil | ||
}, | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
const resourceSchema = `{"resource": {"id": 123, "name": "test"}}` | ||
testutil.TestCommand(t, fakeCreateCmd, map[string]testutil.TestCase{ | ||
"no flags": { | ||
Args: []string{"create"}, | ||
ExpOut: "Creating fake resource\n", | ||
}, | ||
"json": { | ||
Args: []string{"create", "-o=json"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeJSON, | ||
ExpErrOut: "Creating fake resource\n", | ||
}, | ||
"yaml": { | ||
Args: []string{"create", "-o=yaml"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeYAML, | ||
ExpErrOut: "Creating fake resource\n", | ||
}, | ||
"quiet": { | ||
Args: []string{"create", "--quiet"}, | ||
}, | ||
"json quiet": { | ||
Args: []string{"create", "-o=json", "--quiet"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeJSON, | ||
}, | ||
"yaml quiet": { | ||
Args: []string{"create", "-o=yaml", "--quiet"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeYAML, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
"github.com/hetznercloud/hcloud-go/v2/hcloud" | ||
) | ||
|
||
var fakeDeleteCmd = &base.DeleteCmd{ | ||
ResourceNameSingular: "Fake resource", | ||
Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { | ||
cmd.Println("Deleting fake resource") | ||
return nil | ||
}, | ||
|
||
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { | ||
cmd.Println("Fetching fake resource") | ||
|
||
resource := &fakeResource{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
return resource, nil, nil | ||
}, | ||
|
||
NameSuggestions: func(client hcapi2.Client) func() []string { | ||
return nil | ||
}, | ||
} | ||
|
||
func TestDelete(t *testing.T) { | ||
testutil.TestCommand(t, fakeDeleteCmd, map[string]testutil.TestCase{ | ||
"no flags": { | ||
Args: []string{"delete", "123"}, | ||
ExpOut: "Fetching fake resource\nDeleting fake resource\nFake resource 123 deleted\n", | ||
}, | ||
"quiet": { | ||
Args: []string{"delete", "123", "--quiet"}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/util" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/cli/internal/testutil" | ||
) | ||
|
||
var fakeDescribeCmd = &base.DescribeCmd{ | ||
ResourceNameSingular: "Fake resource", | ||
|
||
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { | ||
cmd.Println("Fetching fake resource") | ||
|
||
resource := &fakeResource{ | ||
ID: 123, | ||
Name: "test", | ||
} | ||
|
||
return resource, util.Wrap("resource", resource), nil | ||
}, | ||
|
||
PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { | ||
rsc := resource.(*fakeResource) | ||
cmd.Printf("ID: %d\n", rsc.ID) | ||
cmd.Printf("Name: %s\n", rsc.Name) | ||
return nil | ||
}, | ||
|
||
NameSuggestions: func(client hcapi2.Client) func() []string { | ||
return nil | ||
}, | ||
} | ||
|
||
func TestDescribe(t *testing.T) { | ||
const resourceSchema = `{"resource": {"id": 123, "name": "test"}}` | ||
testutil.TestCommand(t, fakeDescribeCmd, map[string]testutil.TestCase{ | ||
"no flags": { | ||
Args: []string{"describe", "123"}, | ||
ExpOut: `Fetching fake resource | ||
ID: 123 | ||
Name: test | ||
`, | ||
}, | ||
"json": { | ||
Args: []string{"describe", "123", "-o=json"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeJSON, | ||
ExpErrOut: "Fetching fake resource\n", | ||
}, | ||
"yaml": { | ||
Args: []string{"describe", "123", "-o=yaml"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeYAML, | ||
ExpErrOut: "Fetching fake resource\n", | ||
}, | ||
"quiet": { | ||
Args: []string{"describe", "123", "--quiet"}, | ||
}, | ||
"json quiet": { | ||
Args: []string{"describe", "123", "-o=json", "--quiet"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeJSON, | ||
}, | ||
"yaml quiet": { | ||
Args: []string{"describe", "123", "-o=yaml", "--quiet"}, | ||
ExpOut: resourceSchema, | ||
ExpOutType: testutil.DataTypeYAML, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.