fix: check server status before local config to improve error message (#58)

This commit is contained in:
Daniel Moran 2021-05-03 11:17:57 -04:00 committed by GitHub
parent cf2b2f2791
commit bf750650ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -34,12 +34,6 @@ var (
const MinPasswordLen = 8
func (c *CLI) Setup(ctx context.Context, client api.SetupApi, params *SetupParams) error {
// Ensure we'll be able to write onboarding results to local config.
// Do this first so we catch any problems before modifying state on the server side.
if err := c.validateNoNameCollision(params.ConfigName); err != nil {
return err
}
// Check if setup is even allowed.
checkResp, _, err := client.GetSetup(ctx).Execute()
if err != nil {
@ -49,6 +43,12 @@ func (c *CLI) Setup(ctx context.Context, client api.SetupApi, params *SetupParam
return ErrAlreadySetUp
}
// Ensure we'll be able to write onboarding results to local config.
// Do this so we catch any problems before modifying state on the server side.
if err := c.validateNoNameCollision(params.ConfigName); err != nil {
return err
}
// Initialize the server.
setupBody, err := c.onboardingRequest(params)
if err != nil {

View File

@ -20,6 +20,12 @@ import (
func Test_SetupConfigNameCollision(t *testing.T) {
t.Parallel()
client := &mock.SetupApi{
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, *http.Response, error) {
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil, nil
},
}
cfg := "foo"
configSvc := &mock.ConfigService{
ListConfigsFn: func() (config.Configs, error) {
@ -28,7 +34,7 @@ func Test_SetupConfigNameCollision(t *testing.T) {
}
cli := &internal.CLI{ConfigService: configSvc}
err := cli.Setup(context.Background(), &mock.SetupApi{}, &internal.SetupParams{ConfigName: cfg})
err := cli.Setup(context.Background(), client, &internal.SetupParams{ConfigName: cfg})
require.Error(t, err)
require.Contains(t, err.Error(), cfg)
require.Contains(t, err.Error(), "already exists")
@ -37,6 +43,12 @@ func Test_SetupConfigNameCollision(t *testing.T) {
func Test_SetupConfigNameRequired(t *testing.T) {
t.Parallel()
client := &mock.SetupApi{
GetSetupExecuteFn: func(api.ApiGetSetupRequest) (api.InlineResponse200, *http.Response, error) {
return api.InlineResponse200{Allowed: api.PtrBool(true)}, nil, nil
},
}
configSvc := &mock.ConfigService{
ListConfigsFn: func() (config.Configs, error) {
return map[string]config.Config{"foo": {}}, nil
@ -44,7 +56,7 @@ func Test_SetupConfigNameRequired(t *testing.T) {
}
cli := &internal.CLI{ConfigService: configSvc}
err := cli.Setup(context.Background(), &mock.SetupApi{}, &internal.SetupParams{})
err := cli.Setup(context.Background(), client, &internal.SetupParams{})
require.Error(t, err)
require.Equal(t, internal.ErrConfigNameRequired, err)
}
@ -60,7 +72,7 @@ func Test_SetupAlreadySetup(t *testing.T) {
configSvc := &mock.ConfigService{
ListConfigsFn: func() (config.Configs, error) {
return nil, nil
return map[string]config.Config{"foo": {}}, nil
},
}
cli := &internal.CLI{ConfigService: configSvc}