diff --git a/cli/azd/cmd/env.go b/cli/azd/cmd/env.go index fd1f24c9999..cadbb83addf 100644 --- a/cli/azd/cmd/env.go +++ b/cli/azd/cmd/env.go @@ -852,9 +852,9 @@ func (e *envListAction) Run(ctx context.Context) (*actions.ActionResult, error) if invalidCount > 0 { warning := fmt.Sprintf( - "\n⚠ Warning: %d environment(s) have invalid names and may cause issues with azd commands. "+ - "Valid names can only contain: a-z, A-Z, 0-9, -, (, ), _, . (max 64 chars)\n", + "\n⚠ Warning: %d environment(s) have invalid names and may cause issues with azd commands. %s\n", invalidCount, + environment.EnvironmentNameValidationMessage, ) fmt.Fprint(e.writer, warning) } diff --git a/cli/azd/pkg/environment/environment.go b/cli/azd/pkg/environment/environment.go index 761c024872b..68d612e1927 100644 --- a/cli/azd/pkg/environment/environment.go +++ b/cli/azd/pkg/environment/environment.go @@ -136,6 +136,9 @@ var EnvironmentNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9-\(\)_\.]{1,64}$`) // The maximum length of an environment name. var EnvironmentNameMaxLength = 64 +// EnvironmentNameValidationMessage is the message describing valid environment name constraints +const EnvironmentNameValidationMessage = "Valid names can only contain: a-z, A-Z, 0-9, -, (, ), _, . (max 64 chars)" + func IsValidEnvironmentName(name string) bool { return EnvironmentNameRegexp.MatchString(name) } diff --git a/cli/azd/pkg/environment/manager.go b/cli/azd/pkg/environment/manager.go index db605add33b..64109290122 100644 --- a/cli/azd/pkg/environment/manager.go +++ b/cli/azd/pkg/environment/manager.go @@ -272,11 +272,13 @@ func (m *manager) loadOrInitEnvironment(ctx context.Context, environmentName str if environmentName != "" && !IsValidEnvironmentName(environmentName) { fmt.Fprintf( m.console.Handles().Stdout, - "environment name '%s' is invalid (it should contain only alphanumeric characters and hyphens)\n", - environmentName) + "environment name '%s' is invalid. %s\n", + environmentName, + EnvironmentNameValidationMessage) return nil, false, fmt.Errorf( - "environment name '%s' is invalid (it should contain only alphanumeric characters and hyphens)", - environmentName) + "environment name '%s' is invalid. %s", + environmentName, + EnvironmentNameValidationMessage) } // No environment name, no default environment set. @@ -409,8 +411,9 @@ func (m *manager) Get(ctx context.Context, name string) (*Environment, error) { // Validate environment name if !IsValidEnvironmentName(name) { return nil, fmt.Errorf( - "environment name '%s' is invalid. Valid names can only contain: a-z, A-Z, 0-9, -, (, ), _, . (max 64 chars)", + "environment name '%s' is invalid. %s", name, + EnvironmentNameValidationMessage, ) } @@ -580,7 +583,8 @@ func (m *manager) ensureValidEnvironmentName(ctx context.Context, spec *Spec) er func invalidEnvironmentNameMsg(environmentName string) string { return fmt.Sprintf( - "environment name '%s' is invalid (it should contain only alphanumeric characters and hyphens)\n", + "environment name '%s' is invalid. %s\n", environmentName, + EnvironmentNameValidationMessage, ) }