# Split.io Feature Flag Bulk Management CLI

A Python CLI tool to **backup**, **delete**, and **restore** [Split.io](https://www.split.io/) feature flags using the Split.io Admin API. Supports **local and S3** backups, **dry-run mode**, and **bulk operations**.

---

## 🔧 Features

* 🔥 **Bulk Delete** feature flags with optional backup
* 📦 **Backup** feature flags to local disk or S3
* 🔁 **Restore** flags (metadata, tags, rollout, definitions)
* 🧪 **Dry-run** mode to preview actions without making changes
* ✅ Simple CLI interface with support for files or inline flag names

---

## 🧠 Requirements

* Python **3.12+**
* [`boto3`](https://pypi.org/project/boto3/) (for AWS S3 support)
* Split.io Admin API token (with write access)

---

## ⚙️ Setup

1. **Clone and install dependencies**

   ```bash
   pip install -r requirements.txt
   ```

2. **Configure your Split.io credentials**

   Copy `.env.shadow` and add your values:

   ```env
   SPLITIO_ADMIN_API_KEY=your_split_admin_api_key
   SPLITIO_WORKSPACE_ID=your_workspace_id
   ```

   Or export them directly:

   ```bash
   export SPLITIO_ADMIN_API_KEY=...
   export SPLITIO_WORKSPACE_ID=...
   ```

3. **(Optional)** Set up [AWS CLI credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html) for S3 access:

   ```bash
   aws configure
   ```

---

## 📦 Backup Directory Structure

Each feature flag is backed up as:

```
backups/
  └── my-flag/
      ├── metadata.json
      └── definitions/
          ├── staging.json
          └── production.json
```

If using S3, the same structure is mirrored under a key prefix (e.g. `my-flag/metadata.json`).

---

## 🚀 Usage

Run the CLI using one of the subcommands below:

### 🔥 Bulk Delete Feature Flags

Delete multiple flags, with optional backup:

```bash
# Backup to S3 before deleting
python main.py bulk-delete --file flags.txt --s3-backup-bucket my-bucket

# Dry-run only
python main.py bulk-delete --names flag1,flag2 --dry-run

# Skip backup
python main.py bulk-delete --file flags.txt --skip-backup
```

### 📦 Backup Feature Flags Only

```bash
# Backup to both local + S3
python main.py backup --file flags.txt --s3-backup-bucket my-bucket

# Backup only to S3
python main.py backup --names flag1,flag2 --only-s3
```

### 🔁 Restore Feature Flag from Backup

```bash
# Restore from S3
python main.py restore --name my-flag --s3-backup-bucket my-bucket

# Preview only
python main.py restore --name my-flag --dry-run
```

---

## 🦾 CLI Options

| Command       | Option               | Description                                 | Required |
| ------------- | -------------------- | ------------------------------------------- | -------- |
| `bulk-delete` | `--file`             | File with feature flag names (one per line) | One of   |
|               | `--names`            | Comma-separated feature flag names          | One of   |
|               | `--s3-backup-bucket` | S3 bucket name for backup                   | Optional |
|               | `--only-s3`          | Skip writing to local disk                  | Optional |
|               | `--dry-run`          | Preview actions without deletion            | Optional |
|               | `--skip-backup`      | Delete without making any backup            | Optional |
| `backup`      | `--file`             | File with feature flag names                | One of   |
|               | `--names`            | Comma-separated feature flag names          | One of   |
|               | `--s3-backup-bucket` | S3 bucket to write backups                  | Optional |
|               | `--only-s3`          | Write backups to S3 only                    | Optional |
|               | `--dry-run`          | Show what would be backed up                | Optional |
| `restore`     | `--name`             | Feature flag to restore                     | ✅        |
|               | `--s3-backup-bucket` | Read backups from S3 instead of local       | Optional |
|               | `--dry-run`          | Preview flag and definitions to be restored | Optional |

---

## 💡 Tips

* Use `--dry-run` to verify the actions before executing them.
* All backup files include metadata + per-environment definitions.
* `restore` supports both local and S3 source paths.
* If you get `409` errors during restore, the flag already exists.
