Skip to content

feat: add --project-slug flag as alternative to --projectId#1

Merged
yaswanthkumar1995 merged 2 commits intomainfrom
feat/project-slug
Apr 12, 2026
Merged

feat: add --project-slug flag as alternative to --projectId#1
yaswanthkumar1995 merged 2 commits intomainfrom
feat/project-slug

Conversation

@yaswanthkumar1995
Copy link
Copy Markdown
Owner

@yaswanthkumar1995 yaswanthkumar1995 commented Apr 12, 2026

Description

Adds --project-slug as an alternative to --projectId across all CLI commands that accept a project identifier. This allows users to reference projects by their human-readable slug instead of UUID, making CLI usage and config files much more readable.

The slug is resolved to a project ID via the existing /v1/projects/slug/:slug API endpoint. If both --projectId and --project-slug are provided, the CLI returns an error to avoid ambiguity.

Commands updated: run, secrets, secrets get, secrets set, secrets delete, secrets generate-example-env, export, folders get, folders create, folders delete, service-token create, ssh add-host.

Resolves Infisical#141

Type

  • Bug fix
  • New feature
  • Improvement
  • Breaking change
  • Documentation

Tests

# Verify build
GOTOOLCHAIN=auto go build -o /dev/null .

# Verify no lint errors (only pre-existing warnings)
GOTOOLCHAIN=auto go vet ./packages/cmd/...

Example usage

infisical run --project-slug my-project -- env
infisical secrets --project-slug my-project
infisical export --project-slug my-project

Mutual exclusivity check (should error)

infisical run --projectId <id> --project-slug <slug> -- env

Summary by CodeRabbit

  • New Features
    • Added a new --project-slug flag across CLI commands (export, folders, run, secrets, ssh, tokens) as an alternative to --projectId.
    • Project slugs are automatically resolved to the corresponding project identifier at runtime and will override the provided projectId when present.
    • SSH command validation updated to accept either --projectId or --project-slug.

Add --project-slug flag to all commands that accept --projectId,
allowing users to specify projects by their human-readable slug
instead of UUID. The slug is resolved to a project ID via the
existing /v1/projects/slug/:slug API endpoint.

Commands updated: run, secrets, secrets get/set/delete,
secrets generate-example-env, export, folders get/create/delete,
service-token create, ssh add-host.

Resolves Infisical#141
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 12, 2026

📝 Walkthrough

Walkthrough

Adds a new --project-slug CLI flag across multiple commands and a new exported helper ResolveProjectSlug(cmd) that validates, authenticates, calls the API to map a slug to a project ID, and allows commands to override --projectId with the resolved value when present.

Changes

Cohort / File(s) Summary
Core Utility Function
packages/util/helper.go
Added exported ResolveProjectSlug(cmd *cobra.Command) (string, error) which reads/validates --project-slug, enforces mutual exclusivity with --projectId, ensures auth (token or refreshed session), calls api.CallGetProjectBySlug, and returns the resolved project ID or an error.
Export & Run Commands
packages/cmd/export.go, packages/cmd/run.go
Registered --project-slug flag and call util.ResolveProjectSlug(cmd) at runtime to override projectId when a non-empty resolved value is returned.
Secrets Command Group
packages/cmd/secrets.go
Added --project-slug flag to the root and subcommands (get, set, delete, generate-example-env) and invoke util.ResolveProjectSlug(cmd) across handlers to derive/override projectId with consistent error handling.
SSH Subcommand
packages/cmd/ssh.go
Added --project-slug flag to ssh add-host, resolve slug to override projectId, and updated validation message to require --projectId or --project-slug.
Service Tokens
packages/cmd/tokens.go
Added --project-slug flag to service-token create; resolve slug to override workspace/project id before creating the token and handle resolution errors.
Folder Subcommands
packages/cmd/folder.go
Inserted util.ResolveProjectSlug(cmd) into folders subcommands (get, create, delete) to resolve and override projectId early in each handler, with error handling via util.HandleError.

Sequence Diagram

sequenceDiagram
    participant User as User/CLI
    participant Cmd as Command Handler
    participant Util as ResolveProjectSlug()
    participant Auth as Auth System
    participant API as Infisical API

    User->>Cmd: Execute command with --project-slug
    Cmd->>Util: ResolveProjectSlug(cmd)
    Util->>Util: Read & validate `--project-slug` and `--projectId` exclusivity
    Util->>Auth: Get token or establish session
    Auth-->>Util: Auth token
    Util->>API: CallGetProjectBySlug(slug, token)
    API-->>Util: Project { ID, ... }
    Util-->>Cmd: return resolved project ID
    Cmd->>API: Execute command using resolved ID
    API-->>Cmd: Response
    Cmd-->>User: Output/result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hopped through flags both new and snug,
Found slugs that map to project IDs with a tug.
Now CLI trails are tidy and clear,
Hooray — less guessing, more cheer! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main feature: adding a --project-slug flag as an alternative to --projectId across CLI commands.
Linked Issues check ✅ Passed The PR fully implements the requirement from issue #141 to add --project-slug as an alternative to --projectId across all relevant CLI commands, improving readability of config files and CLI invocations.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the --project-slug feature across specified commands, with no unrelated modifications detected.
Description check ✅ Passed The PR description covers the change summary, type, tests, example usage, and mutual exclusivity check, but lacks detailed motivation and dependencies.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/project-slug

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/util/helper.go`:
- Around line 330-333: The code currently swallows parsing/auth errors from
cmd.Flags().GetString for "project-slug" (variable projectSlug) and the other
flag (token), returning "", nil; change the checks so that if err != nil you
return the error (e.g., return "", fmt.Errorf("failed to read flag
'project-slug': %w", err)) instead of nil, and only treat an empty string as the
absent-value case if that is intended; update both usages of
cmd.Flags().GetString(...) (the calls that set projectSlug and the other flag
variable) to propagate errors upward rather than ignoring them.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2141fdca-23d4-4378-bd18-958a1aa2c3f5

📥 Commits

Reviewing files that changed from the base of the PR and between c6a4cf0 and 9466919.

📒 Files selected for processing (7)
  • packages/cmd/export.go
  • packages/cmd/folder.go
  • packages/cmd/run.go
  • packages/cmd/secrets.go
  • packages/cmd/ssh.go
  • packages/cmd/tokens.go
  • packages/util/helper.go

Comment thread packages/util/helper.go
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
packages/util/helper.go (1)

338-341: ⚠️ Potential issue | 🟡 Minor

Handle projectId flag read errors at Line 338.

cmd.Flags().GetString("projectId") still discards err, which can hide misconfigured/missing flags and weaken the mutual-exclusivity check path.

🐛 Proposed fix
-	projectId, _ := cmd.Flags().GetString("projectId")
+	projectId, err := cmd.Flags().GetString("projectId")
+	if err != nil {
+		return "", fmt.Errorf("unable to read --projectId flag: %w", err)
+	}
 	if projectId != "" {
 		return "", fmt.Errorf("cannot specify both --projectId and --project-slug; use one or the other")
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/util/helper.go` around lines 338 - 341, The call to
cmd.Flags().GetString("projectId") currently ignores its error; change the code
that reads projectId (the cmd.Flags().GetString("projectId") call) to capture
and handle the returned error (e.g., if err != nil return "",
fmt.Errorf("reading --projectId flag: %w", err)) before performing the
mutual-exclusion check so misconfigured or missing flags are surfaced instead of
silently ignored.
🧹 Nitpick comments (1)
packages/util/helper.go (1)

334-336: Consider trimming whitespace-only slugs before early return/API call.

" " currently bypasses the empty check and reaches the API. Normalizing with strings.TrimSpace gives cleaner UX.

♻️ Suggested hardening
-	if projectSlug == "" {
+	projectSlug = strings.TrimSpace(projectSlug)
+	if projectSlug == "" {
 		return "", nil
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/util/helper.go` around lines 334 - 336, Trim whitespace from
projectSlug before the empty-check and any API call: normalize projectSlug with
strings.TrimSpace at the start of the function that contains the projectSlug
variable (the branch checking "if projectSlug == \"\" { return \"\", nil }"),
then use the trimmed value for the empty check and for subsequent calls to avoid
sending whitespace-only slugs to the API.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/util/helper.go`:
- Around line 338-341: The call to cmd.Flags().GetString("projectId") currently
ignores its error; change the code that reads projectId (the
cmd.Flags().GetString("projectId") call) to capture and handle the returned
error (e.g., if err != nil return "", fmt.Errorf("reading --projectId flag: %w",
err)) before performing the mutual-exclusion check so misconfigured or missing
flags are surfaced instead of silently ignored.

---

Nitpick comments:
In `@packages/util/helper.go`:
- Around line 334-336: Trim whitespace from projectSlug before the empty-check
and any API call: normalize projectSlug with strings.TrimSpace at the start of
the function that contains the projectSlug variable (the branch checking "if
projectSlug == \"\" { return \"\", nil }"), then use the trimmed value for the
empty check and for subsequent calls to avoid sending whitespace-only slugs to
the API.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f6b717e-5516-4b04-836c-b73534d864d9

📥 Commits

Reviewing files that changed from the base of the PR and between 9466919 and 67e8460.

📒 Files selected for processing (1)
  • packages/util/helper.go

@yaswanthkumar1995 yaswanthkumar1995 merged commit eef209d into main Apr 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --projectSlug=<slug> as an alternative to --projectId=<id>

1 participant