Skip to main content

Projects and Engines

Demonstrates project, engine, and context operations with the AIchor CLI.

Useful as a first step to orient yourself: see what projects and engines are available and set the CLI context so you don't have to pass flags every time.

Usage

./01-projects-and-engines.sh [--project-name NAME] [--engine-name NAME]

Full Script

01-projects-and-engines.sh
#!/usr/bin/env bash
# ==============================================================================
# 01-projects-and-engines.sh
#
# Demonstrates project, engine, and context operations with the AIchor CLI.
# Useful as a first step to orient yourself: see what projects and engines are
# available and set the CLI context so you don't have to pass flags every time.
#
# Usage:
# ./01-projects-and-engines.sh [options]
#
# Options (all optional — omit to just list everything):
# --project-name NAME Name of a specific project to inspect
# --engine-name NAME Name of a specific engine to inspect
#
# Environment variables:
# AICHOR_API_KEY API key for authentication (required only if not already authenticated)
# ==============================================================================

set -eo pipefail

# ---------------------------------------------------------------------------
# Defaults
# ---------------------------------------------------------------------------
PROJECT_NAME=""
ENGINE_NAME=""

while [[ $# -gt 0 ]]; do
case "$1" in
--project-name) PROJECT_NAME="$2"; shift 2 ;;
--engine-name) ENGINE_NAME="$2"; shift 2 ;;
-h|--help)
sed -n '2,/^# =\+$/p' "$0" | grep '^#' | sed 's/^# \?//'
exit 0
;;
*)
echo "Unknown argument: $1" >&2
echo "Usage: ./01-projects-and-engines.sh [--project-name NAME] [--engine-name NAME]" >&2
exit 1
;;
esac
done

# Read current context; use as fallback for display in sections 4/6.
_ctx=$(aichor context list 2>/dev/null || true)
_ctx_project=$(echo "$_ctx" | jq -r '.projectName // empty')
_ctx_engine=$(echo "$_ctx" | jq -r '.engineName // empty')
[[ -z "$PROJECT_NAME" ]] && PROJECT_NAME="$_ctx_project"
[[ -z "$ENGINE_NAME" ]] && ENGINE_NAME="$_ctx_engine"

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
section() {
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " $*"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}

# ---------------------------------------------------------------------------
# 1. Authentication
# ---------------------------------------------------------------------------
section "1. Authentication check"

# A lightweight way to test auth: list projects and check the exit code.
if aichor projects list --output json &>/dev/null; then
echo "Already authenticated."
else
echo "Not authenticated. Logging in with AICHOR_API_KEY..."
aichor auth key --apikey "${AICHOR_API_KEY:?AICHOR_API_KEY environment variable is not set}"
echo "Login successful."
fi

# ---------------------------------------------------------------------------
# 2. Current CLI context
# ---------------------------------------------------------------------------
section "2. Current CLI context (default project and engine)"

# The context is stored in ~/.aichor/aichor_config.json.
# Commands use these defaults so you don't need to pass --project-name and
# --engine-name every time.
aichor context list

# ---------------------------------------------------------------------------
# 3. List all projects
# ---------------------------------------------------------------------------
section "3. All accessible projects"
aichor projects list --output table

# ---------------------------------------------------------------------------
# 4. Inspect a specific project (if provided)
# ---------------------------------------------------------------------------
if [[ -n "$PROJECT_NAME" ]]; then
section "4a. Details for project: $PROJECT_NAME"
aichor projects list "$PROJECT_NAME" --output json

section "4b. Cost breakdown for: $PROJECT_NAME"
# Available flags: --total --gpu --cpu --memory --pvc --buckets --accelerators
aichor projects costs "$PROJECT_NAME" \
--total --gpu --cpu --memory --pvc \
--output table

section "4c. Engines linked to: $PROJECT_NAME"
aichor projects linked-engines "$PROJECT_NAME" --output table
else
echo
echo "(Skip sections 4a-4c: no project name provided and no project set in CLI context)"
fi

# ---------------------------------------------------------------------------
# 5. List all engines
# ---------------------------------------------------------------------------
section "5. All available engines"
aichor engines list --output table

# ---------------------------------------------------------------------------
# 6. Inspect a specific engine (if provided)
# ---------------------------------------------------------------------------
if [[ -n "$ENGINE_NAME" ]]; then
section "6. Details for engine: $ENGINE_NAME"
aichor engines list "$ENGINE_NAME" --output json
else
echo
echo "(Skip section 6: no engine name provided and no engine set in CLI context)"
fi

# ---------------------------------------------------------------------------
# 7. Set CLI context (only for values not already set)
# ---------------------------------------------------------------------------
section "7. Setting CLI context"
_updated=false

if [[ -n "$_ctx_project" ]]; then
echo "Project already set in context: $_ctx_project"
else
if [[ -z "$PROJECT_NAME" ]]; then
read -r -p "Enter project name to set in context: " PROJECT_NAME
fi
if [[ -n "$PROJECT_NAME" ]]; then
echo "Setting default project to: $PROJECT_NAME"
aichor context set project "$PROJECT_NAME"
_updated=true
fi
fi

if [[ -n "$_ctx_engine" ]]; then
echo "Engine already set in context: $_ctx_engine"
else
if [[ -z "$ENGINE_NAME" ]]; then
read -r -p "Enter engine name to set in context: " ENGINE_NAME
fi
if [[ -n "$ENGINE_NAME" ]]; then
echo "Setting default engine to: $ENGINE_NAME"
aichor context set engine "$ENGINE_NAME"
_updated=true
fi
fi

if [[ "$_updated" == true ]]; then
echo
echo "Updated context:"
aichor context list
echo
echo "Context is now stored locally. Subsequent aichor commands "
echo "will use these defaults."
fi