Updated to use environment variables if present, with manual overrides
This commit is contained in:
parent
815f5f40b5
commit
6b04b192d6
16
README.md
16
README.md
|
@ -12,11 +12,19 @@ A command line interface for interacting with Gitea servers.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Create a `.env` file in the project root with your Gitea credentials:
|
You can configure the Gitea CLI in two ways:
|
||||||
|
|
||||||
```env
|
### 1. Environment Variables
|
||||||
GITEA_URL=https://your-gitea-instance.com
|
Set the following environment variables:
|
||||||
GITEA_TOKEN=your_access_token
|
```bash
|
||||||
|
export GITEA_URL=https://your-gitea-instance.com
|
||||||
|
export GITEA_TOKEN=your_access_token
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Command Line Arguments
|
||||||
|
Alternatively, provide the configuration via command line arguments:
|
||||||
|
```bash
|
||||||
|
python gitea_cli.py create-repo --gitea-url https://your-gitea-instance.com --gitea-token your_access_token --name my-repo
|
||||||
```
|
```
|
||||||
|
|
||||||
To get an access token:
|
To get an access token:
|
||||||
|
|
|
@ -1,18 +1,10 @@
|
||||||
#!/usr/bin/env python3
|
#!/opt/homebrew/anaconda3/bin/python
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
import subprocess
|
import subprocess
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
# Load environment variables
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
# Get Gitea configuration from environment
|
|
||||||
GITEA_URL = os.getenv('GITEA_URL')
|
|
||||||
GITEA_TOKEN = os.getenv('GITEA_TOKEN')
|
|
||||||
|
|
||||||
class GiteaAPI:
|
class GiteaAPI:
|
||||||
def __init__(self, base_url, token):
|
def __init__(self, base_url, token):
|
||||||
|
@ -40,15 +32,30 @@ class GiteaAPI:
|
||||||
else:
|
else:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
def validate_environment():
|
def get_gitea_config(url=None, token=None):
|
||||||
"""Validate that required environment variables are set."""
|
"""Get Gitea configuration from environment or command line arguments."""
|
||||||
if not GITEA_URL:
|
config = {
|
||||||
click.echo("Error: GITEA_URL environment variable is not set")
|
'url': url or os.environ.get('GITEA_URL'),
|
||||||
sys.exit(1)
|
'token': token or os.environ.get('GITEA_TOKEN')
|
||||||
if not GITEA_TOKEN:
|
}
|
||||||
click.echo("Error: GITEA_TOKEN environment variable is not set")
|
|
||||||
|
missing = []
|
||||||
|
if not config['url']:
|
||||||
|
missing.append('GITEA_URL')
|
||||||
|
if not config['token']:
|
||||||
|
missing.append('GITEA_TOKEN')
|
||||||
|
|
||||||
|
if missing:
|
||||||
|
missing_str = ', '.join(missing)
|
||||||
|
click.echo(f"Error: Missing required configuration: {missing_str}")
|
||||||
|
click.echo("Please either:")
|
||||||
|
click.echo("1. Set environment variables for the missing values")
|
||||||
|
click.echo("2. Provide them as command line arguments:")
|
||||||
|
click.echo(" --gitea-url URL --gitea-token TOKEN")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
def is_git_repository():
|
def is_git_repository():
|
||||||
"""Check if current directory is a git repository."""
|
"""Check if current directory is a git repository."""
|
||||||
try:
|
try:
|
||||||
|
@ -85,20 +92,48 @@ def set_git_remote(repo_url):
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
def cli():
|
def cli():
|
||||||
"""Gitea CLI - Command line interface for Gitea"""
|
"""Gitea CLI - Command line interface for Gitea
|
||||||
|
|
||||||
|
Configuration can be provided in two ways:
|
||||||
|
1. Environment variables:
|
||||||
|
- GITEA_URL: Your Gitea server URL
|
||||||
|
- GITEA_TOKEN: Your Gitea API token
|
||||||
|
|
||||||
|
2. Command line arguments:
|
||||||
|
--gitea-url and --gitea-token
|
||||||
|
(These override environment variables if both are present)
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option('--name', required=True, help='Name of the repository')
|
@click.option('--name', required=True, help='Name of the repository')
|
||||||
@click.option('--description', default="", help='Repository description')
|
@click.option('--description', default="", help='Repository description')
|
||||||
@click.option('--private', is_flag=True, default=False, help='Make repository private')
|
@click.option('--private', is_flag=True, default=False, help='Make repository private')
|
||||||
@click.option('--set-remote', is_flag=True, default=False, help='Set the remote origin for the current git repository')
|
@click.option('--set-remote', is_flag=True, default=False,
|
||||||
def create_repo(name, description, private, set_remote):
|
help='Set the remote origin for the current git repository')
|
||||||
"""Create a new repository on Gitea."""
|
@click.option('--gitea-url',
|
||||||
validate_environment()
|
help='Gitea server URL (overrides GITEA_URL environment variable)')
|
||||||
|
@click.option('--gitea-token',
|
||||||
|
help='Gitea API token (overrides GITEA_TOKEN environment variable)')
|
||||||
|
def create_repo(name, description, private, set_remote, gitea_url, gitea_token):
|
||||||
|
"""Create a new repository on Gitea.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
Using environment variables:
|
||||||
|
$ export GITEA_URL=https://gitea.example.com
|
||||||
|
$ export GITEA_TOKEN=your_token
|
||||||
|
$ gitea_cli.py create-repo --name my-repo
|
||||||
|
|
||||||
|
Using command line arguments:
|
||||||
|
$ gitea_cli.py create-repo --gitea-url https://gitea.example.com --gitea-token your_token --name my-repo
|
||||||
|
|
||||||
|
Create private repo and set as remote:
|
||||||
|
$ gitea_cli.py create-repo --name my-repo --private --set-remote
|
||||||
|
"""
|
||||||
|
config = get_gitea_config(gitea_url, gitea_token)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
gitea = GiteaAPI(GITEA_URL, GITEA_TOKEN)
|
gitea = GiteaAPI(config['url'], config['token'])
|
||||||
repo = gitea.create_repository(name, description, private)
|
repo = gitea.create_repository(name, description, private)
|
||||||
click.echo(f"Successfully created repository: {repo['html_url']}")
|
click.echo(f"Successfully created repository: {repo['html_url']}")
|
||||||
|
|
||||||
|
@ -111,7 +146,7 @@ def create_repo(name, description, private, set_remote):
|
||||||
# Replace HTTPS URL with token-based URL for authentication
|
# Replace HTTPS URL with token-based URL for authentication
|
||||||
if clone_url.startswith('https://'):
|
if clone_url.startswith('https://'):
|
||||||
parsed_url = clone_url.split('://')
|
parsed_url = clone_url.split('://')
|
||||||
clone_url = f"{parsed_url[0]}://oauth2:{GITEA_TOKEN}@{parsed_url[1]}"
|
clone_url = f"{parsed_url[0]}://oauth2:{config['token']}@{parsed_url[1]}"
|
||||||
|
|
||||||
set_git_remote(clone_url)
|
set_git_remote(clone_url)
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
requests>=2.31.0
|
requests>=2.31.0
|
||||||
click>=8.1.0
|
click>=8.1.0
|
||||||
python-dotenv>=1.0.0
|
|
||||||
|
|
Loading…
Reference in New Issue