# Rigstate CLI - Quick Start Guide

## Installation

```bash
cd packages/cli
npm install
npm run build
```

## Global Installation (Optional)

To use `rigstate` from anywhere:

```bash
# Option 1: npm install
npm install -g .

# Option 2: Use the install script
chmod +x install.sh
./install.sh
```

**Note:** You may need to use `sudo` for global installation on macOS/Linux.

## Testing Locally (Without Global Install)

You can test all commands without installing globally:

```bash
node dist/index.js --help
node dist/index.js login sk_rigstate_your_key_here
node dist/index.js scan
```

## Step-by-Step Tutorial

### 1. Get Your API Key

1. Go to your Rigstate dashboard
2. Navigate to Settings → API Keys
3. Click "Generate New Key"
4. Copy the key (it starts with `sk_rigstate_`)

### 2. Login

```bash
rigstate login sk_rigstate_1234567890abcdef
```

You should see:
```
✅ Successfully logged in!

Your API key has been securely stored. You can now use "rigstate scan" to audit your code.
```

### 3. Run Your First Scan

**Scan current directory:**
```bash
rigstate scan
```

**Scan a specific folder:**
```bash
rigstate scan ./src
```

**Scan with project ID:**
```bash
rigstate scan --project abc-123-def-456
```

**Output as JSON (for IDE extensions):**
```bash
rigstate scan --json
```

### 4. Understanding the Output

**Human-readable format (default):**
```
📊 Scan Summary
────────────────────────────────────────────────────────────
Total Files Scanned: 5
Total Issues Found: 3

Issues by Severity:
  critical: 1
  high: 1
  medium: 1

🔍 Detailed Results
────────────────────────────────────────────────────────────

src/auth.js
  [CRITICAL] SQL Injection
  Potential SQL injection vulnerability detected
  [HIGH] Hardcoded Password
  Hardcoded credentials found in source code
```

**JSON format:**
```json
{
  "results": [
    {
      "id": "src/auth.js",
      "file_path": "src/auth.js",
      "issues": [
        {
          "type": "SQL Injection",
          "severity": "critical",
          "message": "Potential SQL injection vulnerability detected",
          "line": 5
        }
      ]
    }
  ],
  "summary": {
    "total_files": 5,
    "total_issues": 3,
    "by_severity": {
      "critical": 1,
      "high": 1,
      "medium": 1
    }
  }
}
```

## File Detection

The CLI automatically:
- ✅ Finds all code files (`.js`, `.ts`, `.py`, etc.)
- ✅ Respects your `.gitignore` patterns
- ✅ Skips `node_modules`, `.git`, `dist`, etc.
- ✅ Processes files in parallel for speed

## Configuration

Your config is stored at:
- **macOS/Linux:** `~/.config/rigstate-cli/config.json`
- **Windows:** `%APPDATA%\rigstate-cli\config.json`

### Environment Variables

Override the API URL for production:

```bash
export RIGSTATE_API_URL=https://api.rigstate.com
rigstate scan
```

Or create a `.env` file:
```
RIGSTATE_API_URL=https://api.rigstate.com
```

## Troubleshooting

### "Not logged in" Error
```bash
❌ Not logged in. Please run "rigstate login <your-api-key>" first.
```
**Solution:** Run `rigstate login sk_rigstate_your_key_here`

### "Invalid API key format" Error
```bash
❌ Invalid API key format
API keys must start with "sk_rigstate_"
```
**Solution:** Make sure your key starts with `sk_rigstate_`. Generate a new one from the dashboard if needed.

### "Project not found" Error
```bash
❌ Project not found or access denied
```
**Solution:** 
- Check that the project ID is correct
- Ensure you own the project
- Try without the `--project` flag

### Network Error
```bash
❌ Network Error: Could not reach the API. Is the server running?
```
**Solution:**
- Check that the Rigstate API is running (`npm run dev` in the main project)
- Verify the API URL in your config or environment variables
- Check your internet connection

## Development

```bash
# Install dependencies
npm install

# Build once
npm run build

# Watch mode (auto-rebuild on changes)
npm run dev

# Type checking
npm run lint
```

## Next Steps

- Check out the full [README.md](./README.md)
- Learn about [IDE Extensions](./README.md#future-ide-extensions)
- Read the [API Documentation](../../docs/api.md)

## Support

- GitHub Issues: [Create an issue](https://github.com/rigstate/rigstate/issues)
- Email: support@rigstate.com
- Discord: [Join our community](https://discord.gg/rigstate)
