Skip to content

Commit 8a15b5c

Browse files
authored
feat: install with different branches / forked repositories (#391)
1 parent 68023e4 commit 8a15b5c

File tree

4 files changed

+116
-9
lines changed

4 files changed

+116
-9
lines changed

cli/app/commands/install/command.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def install_callback(
3333
"-vd",
3434
help="The domain where the nixopus view will be accessible (e.g. nixopus.com), if not provided you can use the ip address of the server and the port (e.g. 192.168.1.100:80)",
3535
),
36+
repo: str = typer.Option(
37+
None, "--repo", "-r", help="GitHub repository URL to clone (defaults to config value)"
38+
),
39+
branch: str = typer.Option(
40+
None, "--branch", "-b", help="Git branch to clone (defaults to config value)"
41+
),
3642
):
3743
"""Install Nixopus"""
3844
if ctx.invoked_subcommand is None:
@@ -46,6 +52,8 @@ def install_callback(
4652
config_file=config_file,
4753
api_domain=api_domain,
4854
view_domain=view_domain,
55+
repo=repo,
56+
branch=branch,
4957
)
5058
install.run()
5159

cli/app/commands/install/run.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def __init__(
9696
config_file: str = None,
9797
api_domain: str = None,
9898
view_domain: str = None,
99+
repo: str = None,
100+
branch: str = None,
99101
):
100102
self.logger = logger
101103
self.verbose = verbose
@@ -105,12 +107,32 @@ def __init__(
105107
self.config_file = config_file
106108
self.api_domain = api_domain
107109
self.view_domain = view_domain
110+
self.repo = repo
111+
self.branch = branch
108112
self._user_config = _config.load_user_config(self.config_file)
109113
self.progress = None
110114
self.main_task = None
111115
self._validate_domains()
116+
self._validate_repo()
117+
118+
# Log when using custom repository/branch and staging compose file
119+
if self._is_custom_repo_or_branch():
120+
if self.logger:
121+
self.logger.info("Custom repository/branch detected - will use docker-compose-staging.yml")
112122

113123
def _get_config(self, key: str):
124+
# Override repo_url and branch_name if provided via command line
125+
if key == "repo_url" and self.repo is not None:
126+
return self.repo
127+
if key == "branch_name" and self.branch is not None:
128+
return self.branch
129+
130+
# Override compose_file_path to use docker-compose-staging.yml when custom repo/branch is provided
131+
if key == "compose_file_path" and self._is_custom_repo_or_branch():
132+
# Get the base directory and replace docker-compose.yml with docker-compose-staging.yml
133+
default_compose_path = _config.get_config_value(key, self._user_config, DEFAULTS)
134+
return default_compose_path.replace("docker-compose.yml", "docker-compose-staging.yml")
135+
114136
try:
115137
return _config.get_config_value(key, self._user_config, DEFAULTS)
116138
except ValueError:
@@ -127,6 +149,27 @@ def _validate_domains(self):
127149
if not domain_pattern.match(self.api_domain) or not domain_pattern.match(self.view_domain):
128150
raise ValueError("Invalid domain format. Domains must be valid hostnames")
129151

152+
def _validate_repo(self):
153+
if self.repo:
154+
# Basic validation for repository URL format
155+
if not (
156+
self.repo.startswith(("http://", "https://", "git://", "ssh://"))
157+
or (self.repo.endswith(".git") and not self.repo.startswith("github.com:"))
158+
or ("@" in self.repo and ":" in self.repo and self.repo.count("@") == 1)
159+
):
160+
raise ValueError("Invalid repository URL format")
161+
162+
def _is_custom_repo_or_branch(self):
163+
"""Check if custom repository or branch is provided (different from defaults)"""
164+
default_repo = _config.get_yaml_value(DEFAULT_REPO) # "https://github.com/raghavyuva/nixopus"
165+
default_branch = _config.get_yaml_value(DEFAULT_BRANCH) # "master"
166+
167+
# Check if either repo or branch differs from defaults
168+
repo_differs = self.repo is not None and self.repo != default_repo
169+
branch_differs = self.branch is not None and self.branch != default_branch
170+
171+
return repo_differs or branch_differs
172+
130173
def run(self):
131174
steps = [
132175
("Preflight checks", self._run_preflight_checks),

cli/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nixopus"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
description = "A CLI for Nixopus"
55
authors = ["Nixopus <[email protected]>"]
66
readme = "README.md"

scripts/install.sh

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,37 @@ readonly GREEN='\033[0;32m'
77
readonly BLUE='\033[0;34m'
88
readonly NC='\033[0m'
99

10-
# GitHub repository info
11-
readonly REPO_URL="https://github.com/raghavyuva/nixopus"
10+
# Default GitHub repository info
11+
DEFAULT_REPO_URL="https://github.com/raghavyuva/nixopus"
12+
DEFAULT_BRANCH="master"
1213

13-
readonly PACKAGE_JSON_URL_MASTER="https://raw.githubusercontent.com/raghavyuva/nixopus/master/package.json"
14+
# Variables for custom repository and branch
15+
REPO_URL="$DEFAULT_REPO_URL"
16+
BRANCH="$DEFAULT_BRANCH"
1417

1518
# Logging functions
1619
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
1720
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
1821
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
1922

23+
# Validate repository URL format
24+
validate_repo_url() {
25+
local repo_url="$1"
26+
if [[ ! "$repo_url" =~ ^https://github\.com/[^/]+/[^/]+/?$ ]]; then
27+
log_error "Invalid repository URL format. Expected: https://github.com/owner/repo"
28+
exit 1
29+
fi
30+
}
31+
32+
# Validate branch name
33+
validate_branch() {
34+
local branch="$1"
35+
if [[ ! "$branch" =~ ^[a-zA-Z0-9._/-]+$ ]]; then
36+
log_error "Invalid branch name. Branch names can only contain letters, numbers, dots, underscores, slashes, and hyphens."
37+
exit 1
38+
fi
39+
}
40+
2041
# Show usage information
2142
show_usage() {
2243
cat << EOF
@@ -26,6 +47,8 @@ This script installs the nixopus CLI and optionally runs 'nixopus install' with
2647
2748
CLI Installation Options:
2849
--skip-nixopus-install Skip running 'nixopus install' after CLI installation
50+
--repo REPOSITORY GitHub repository URL for CLI installation and passed to 'nixopus install' (default: https://github.com/raghavyuva/nixopus). When custom repo/branch is used, docker-compose-staging.yml will be used instead of docker-compose.yml
51+
--branch BRANCH Git branch to use for CLI installation and passed to 'nixopus install' (default: master). When custom repo/branch is used, docker-compose-staging.yml will be used instead of docker-compose.yml
2952
3053
nixopus install Options (passed through to 'nixopus install'):
3154
-v, --verbose Show more details while installing
@@ -56,6 +79,7 @@ Quick Install with Options:
5679
curl -sSL https://install.nixopus.com | bash -s -- --dry-run
5780
curl -sSL https://install.nixopus.com | bash -s -- --api-domain api.example.com
5881
curl -sSL https://install.nixopus.com | bash -s -- ssh --verbose
82+
curl -sSL https://install.nixopus.com | bash -s -- --repo https://github.com/user/fork --branch develop
5983
6084
Local Examples:
6185
$0 # Install CLI and run 'nixopus install'
@@ -64,6 +88,7 @@ Local Examples:
6488
$0 --force --timeout 600 # Install CLI and run 'nixopus install' with force and custom timeout
6589
$0 --api-domain api.example.com --view-domain example.com # Install CLI and run 'nixopus install' with custom domains
6690
$0 --dry-run --config-file /path/to/config # Install CLI and run 'nixopus install' in dry-run mode with custom config
91+
$0 --repo https://github.com/user/fork --branch develop # Install CLI from custom repository and branch
6792
$0 ssh --verbose # Install CLI and run 'nixopus install ssh --verbose'
6893
$0 deps --dry-run # Install CLI and run 'nixopus install deps --dry-run'
6994
@@ -107,9 +132,16 @@ detect_os() {
107132
# Get CLI version and package list
108133
get_package_info() {
109134
local package_json
110-
package_json=$(curl -fsSL "$PACKAGE_JSON_URL_MASTER" 2>/dev/null || true)
135+
local package_json_url
136+
137+
# Extract repository owner and name from URL
138+
local repo_owner_name
139+
repo_owner_name=$(echo "$REPO_URL" | sed 's|https://github.com/||')
140+
package_json_url="https://raw.githubusercontent.com/$repo_owner_name/$BRANCH/package.json"
141+
142+
package_json=$(curl -fsSL "$package_json_url" 2>/dev/null || true)
111143
if [[ -z "$package_json" || "$package_json" != \{* ]]; then
112-
log_error "Failed to fetch package.json from master branch"
144+
log_error "Failed to fetch package.json from $BRANCH branch of $REPO_URL"
113145
exit 1
114146
fi
115147

@@ -265,6 +297,16 @@ main() {
265297
SKIP_NIXOPUS_INSTALL=true
266298
shift
267299
;;
300+
--repo)
301+
REPO_URL="$2"
302+
validate_repo_url "$REPO_URL"
303+
shift 2
304+
;;
305+
--branch)
306+
BRANCH="$2"
307+
validate_branch "$BRANCH"
308+
shift 2
309+
;;
268310
--verbose|-v)
269311
NIXOPUS_INSTALL_ARGS+=("$1")
270312
shift
@@ -313,18 +355,32 @@ main() {
313355
esac
314356
done
315357

358+
# Show repository and branch info
359+
log_info "Using repository: $REPO_URL"
360+
log_info "Using branch: $BRANCH"
361+
316362
# Run main function with permission check
317363
pkg_type=$(detect_os)
318364
check_permissions "$pkg_type"
319365
install_cli
320366

321367
if [ "$SKIP_NIXOPUS_INSTALL" = false ]; then
368+
# Add repository and branch info to nixopus install command
369+
local cli_args=()
370+
if [ "$REPO_URL" != "$DEFAULT_REPO_URL" ]; then
371+
cli_args+=("--repo" "$REPO_URL")
372+
fi
373+
if [ "$BRANCH" != "$DEFAULT_BRANCH" ]; then
374+
cli_args+=("--branch" "$BRANCH")
375+
fi
376+
cli_args+=("${NIXOPUS_INSTALL_ARGS[@]}")
377+
322378
if [ -n "$SUBCOMMAND" ]; then
323-
log_info "Running 'nixopus install' with subcommand and options: ${NIXOPUS_INSTALL_ARGS[*]}"
379+
log_info "Running 'nixopus install' with subcommand and options: ${cli_args[*]}"
324380
else
325-
log_info "Running 'nixopus install' with options: ${NIXOPUS_INSTALL_ARGS[*]}"
381+
log_info "Running 'nixopus install' with options: ${cli_args[*]}"
326382
fi
327-
nixopus install "${NIXOPUS_INSTALL_ARGS[@]}"
383+
nixopus install "${cli_args[@]}"
328384
log_success "nixopus install completed successfully!"
329385
else
330386
log_info "Skipping 'nixopus install' as requested..."

0 commit comments

Comments
 (0)