diff --git a/CHANGELOG.md b/CHANGELOG.md index 70b858a12..ccd6c960d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Enhancements: - build(dockerfile-rust): add wasm tools to the rust docker container ([#1552](https://github.com/fastly/cli/pull/1552)) +- feat(env): add detection for workspace ID ([#1560](https://github.com/fastly/cli/pull/1560)) ### Bug fixes: - fix(compute): clarify fastly.toml error message when file not found ([#1556](https://github.com/fastly/cli/pull/1556)) diff --git a/pkg/argparser/common.go b/pkg/argparser/common.go index 05e2433ec..5a92eb7fe 100644 --- a/pkg/argparser/common.go +++ b/pkg/argparser/common.go @@ -9,6 +9,10 @@ var ( FlagJSONName = "json" // FlagJSONDesc is the flag description. FlagJSONDesc = "Render output as JSON" + // FlagNGWAFWorkspaceID is the workspace ID. + FlagNGWAFWorkspaceID = "workspace-id" + // FlagNGWAFWorkspaceIDDesc is the workspace ID flag description. + FlagNGWAFWorkspaceIDDesc = "Alphanumeric string identifying the NGWAF Workspace (falls back to FASTLY_WORKSPACE_ID)" // FlagServiceIDName is the flag name. FlagServiceIDName = "service-id" // FlagServiceIDDesc is the flag description. diff --git a/pkg/argparser/flags.go b/pkg/argparser/flags.go index e617e6239..c08a685d9 100644 --- a/pkg/argparser/flags.go +++ b/pkg/argparser/flags.go @@ -198,6 +198,26 @@ func (sv *OptionalCustomerID) Parse() error { return nil } +// OptionalWorkspaceID represents a Fastly NGWAF Workspace ID. +type OptionalWorkspaceID struct { + OptionalString +} + +// Parse returns a workspace ID either from a flag or from a user defined +// environment variable (see pkg/env/env.go). +// +// NOTE: Will fallback to FASTLY_WORKSPACE_ID environment variable if no flag value set. +func (sv *OptionalWorkspaceID) Parse() error { + if sv.Value == "" { + if e := os.Getenv(env.WorkspaceID); e != "" { + sv.Value = e + return nil + } + return fsterr.ErrNoWorkspaceID + } + return nil +} + // AutoCloneFlagOpts enables easy configuration of the --autoclone flag defined // via the RegisterAutoCloneFlag constructor. type AutoCloneFlagOpts struct { diff --git a/pkg/env/env.go b/pkg/env/env.go index ba666942f..cda2bfd63 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -49,6 +49,9 @@ const ( // collection related to a Wasm binary. // Set to "true" to disable all forms of data collection. WasmMetadataDisable = "FASTLY_WASM_METADATA_DISABLE" + + // WorkspaceID is the env we look for in Workspace related commands if none is provided. + WorkspaceID = "FASTLY_WORKSPACE_ID" ) // Parse transforms the local environment data structure into a map type. diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 2873d903a..01061c738 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -47,6 +47,13 @@ var ErrNoCustomerID = RemediationError{ Remediation: CustomerIDRemediation, } +// ErrNoWorkspaceID means no --workspace-id or FASTLY_WORKSPACE_ID environment +// variable found. +var ErrNoWorkspaceID = RemediationError{ + Inner: fmt.Errorf("error reading workspace ID: no workspace ID found"), + Remediation: WorkspaceIDRemediation, +} + // ErrMissingManifestVersion means an invalid manifest (fastly.toml) has been used. var ErrMissingManifestVersion = RemediationError{ Inner: fmt.Errorf("no manifest_version found in the fastly.toml"), diff --git a/pkg/errors/remediation_error.go b/pkg/errors/remediation_error.go index 7e191ee29..eecdbdbd5 100644 --- a/pkg/errors/remediation_error.go +++ b/pkg/errors/remediation_error.go @@ -97,6 +97,12 @@ var CustomerIDRemediation = strings.Join([]string{ "Please provide one via the --customer-id flag, or by setting the FASTLY_CUSTOMER_ID environment variable", }, " ") +// WorkspaceIDRemediation suggests provide a customer ID via --workspace-id flag +// or via environment variable. +var WorkspaceIDRemediation = strings.Join([]string{ + "Please provide one via the --workspace-id flag, or by setting the FASTLY_WORKSPACE_ID environment variable", +}, " ") + // ExistingDirRemediation suggests moving to another directory and retrying. var ExistingDirRemediation = strings.Join([]string{ "Please create a new directory and initialize a new project using:",