|
| 1 | +#!/bin/bash |
| 2 | +# Don't use set -e to allow script to continue even if some steps fail |
| 3 | +# set -e |
| 4 | + |
| 5 | +export DEBIAN_FRONTEND=noninteractive |
| 6 | +export GIT_TERMINAL_PROMPT=0 |
| 7 | + |
| 8 | +# Ensure we're running as root (no sudo needed) |
| 9 | +if [ "$(id -u)" -ne 0 ]; then |
| 10 | + echo "⚠️ Warning: Not running as root. Some operations may fail." |
| 11 | +fi |
| 12 | + |
| 13 | +echo "🚀 Setting up Terraform Provider GitHubx development environment..." |
| 14 | + |
| 15 | +# Install bash-completion if not already installed |
| 16 | +if ! command -v bash-completion &> /dev/null && [ ! -f /usr/share/bash-completion/bash_completion ]; then |
| 17 | + echo "📦 Installing bash-completion..." |
| 18 | + apt-get update -y && apt-get install -y bash-completion && rm -rf /var/lib/apt/lists/* |
| 19 | +fi |
| 20 | + |
| 21 | +# Setup bashrc with aliases and history settings |
| 22 | +echo "⚙️ Configuring bash environment..." |
| 23 | +cat >> /root/.bashrc << 'BASHRC_EOF' |
| 24 | +
|
| 25 | +# Devcontainer bashrc configuration |
| 26 | +# History and completion settings |
| 27 | +
|
| 28 | +# Enable arrow key history navigation |
| 29 | +set -o emacs |
| 30 | +bind "\e[A": history-search-backward |
| 31 | +bind "\e[B": history-search-forward |
| 32 | +
|
| 33 | +# History settings |
| 34 | +HISTCONTROL=ignoredups:erasedups |
| 35 | +HISTSIZE=10000 |
| 36 | +HISTFILESIZE=20000 |
| 37 | +shopt -s histappend |
| 38 | +
|
| 39 | +# Save and reload history after each command |
| 40 | +PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND" |
| 41 | +
|
| 42 | +# Enable bash completion |
| 43 | +if [ -f /usr/share/bash-completion/bash_completion ]; then |
| 44 | + . /usr/share/bash-completion/bash_completion |
| 45 | +elif [ -f /etc/bash_completion ]; then |
| 46 | + . /etc/bash_completion |
| 47 | +fi |
| 48 | +
|
| 49 | +# Additional useful aliases |
| 50 | +alias ll='ls -alF' |
| 51 | +alias la='ls -A' |
| 52 | +alias l='ls -CF' |
| 53 | +alias ..='cd ..' |
| 54 | +alias ...='cd ../..' |
| 55 | +
|
| 56 | +# Git aliases |
| 57 | +alias gs='git status' |
| 58 | +alias ga='git add' |
| 59 | +alias gc='git commit' |
| 60 | +alias gp='git push' |
| 61 | +alias gl='git log --oneline --graph --decorate' |
| 62 | +
|
| 63 | +# Terraform aliases |
| 64 | +alias tf='terraform' |
| 65 | +alias tfi='terraform init' |
| 66 | +alias tfa='terraform apply' |
| 67 | +alias tfaa='terraform apply -auto-approve' |
| 68 | +alias tfp='terraform plan' |
| 69 | +alias tfd='terraform destroy' |
| 70 | +alias tfda='terraform destroy -auto-approve' |
| 71 | +
|
| 72 | +alias rmtl='rm -rf .terraform.lock.hcl' |
| 73 | +
|
| 74 | +BASHRC_EOF |
| 75 | +echo "✅ Bash environment configured" |
| 76 | + |
| 77 | +# Display system information |
| 78 | +echo "📋 System Information:" |
| 79 | +uname -a |
| 80 | +if command -v go &> /dev/null; then |
| 81 | + echo "Go version: $(go version)" |
| 82 | + echo "Go path: $(go env GOPATH)" |
| 83 | +else |
| 84 | + echo "⚠️ Go not found - will be installed by devcontainer feature" |
| 85 | +fi |
| 86 | + |
| 87 | +# Verify Terraform installation |
| 88 | +echo "🔧 Verifying Terraform..." |
| 89 | +if command -v terraform &> /dev/null; then |
| 90 | + echo "✅ Terraform installed: $(terraform version)" |
| 91 | +else |
| 92 | + echo "⚠️ Terraform not found - will be installed by devcontainer feature" |
| 93 | +fi |
| 94 | + |
| 95 | +# Setup GitHub CLI authentication from host OS |
| 96 | +echo "🔧 Setting up GitHub CLI authentication..." |
| 97 | +if command -v gh &> /dev/null; then |
| 98 | + echo "✅ GitHub CLI installed: $(gh --version | head -n 1)" |
| 99 | + |
| 100 | + # Ensure the config directory exists |
| 101 | + GH_CONFIG_DIR="/root/.config/gh" |
| 102 | + mkdir -p "${GH_CONFIG_DIR}" |
| 103 | + |
| 104 | + # Check if host config is mounted (from devcontainer mount) |
| 105 | + # The devcontainer.json should mount ${localEnv:HOME}/.config/gh to /root/.config/gh |
| 106 | + if [ -d "${GH_CONFIG_DIR}" ] && [ -n "$(ls -A ${GH_CONFIG_DIR} 2>/dev/null)" ]; then |
| 107 | + echo "📁 Found mounted GitHub CLI config from host OS" |
| 108 | + # Ensure proper permissions (config files should be readable) |
| 109 | + chmod -R u+rw "${GH_CONFIG_DIR}" 2>/dev/null || true |
| 110 | + find "${GH_CONFIG_DIR}" -type f -name "*.yaml" -exec chmod 600 {} \; 2>/dev/null || true |
| 111 | + |
| 112 | + # Verify authentication |
| 113 | + if gh auth status &> /dev/null; then |
| 114 | + echo "✅ GitHub CLI is authenticated (using host OS auth)" |
| 115 | + gh auth status 2>&1 | head -n 3 || true |
| 116 | + else |
| 117 | + echo "⚠️ GitHub CLI config found but not authenticated." |
| 118 | + echo " Please run 'gh auth login' on your host OS to authenticate." |
| 119 | + fi |
| 120 | + else |
| 121 | + echo "⚠️ GitHub CLI config not found at ${GH_CONFIG_DIR}" |
| 122 | + echo " The devcontainer should mount your host's ~/.config/gh directory." |
| 123 | + echo " If the mount failed, ensure you have authenticated with 'gh auth login' on your host OS." |
| 124 | + echo " You can also run 'gh auth login' inside the container, but it won't persist across rebuilds." |
| 125 | + |
| 126 | + # Check if auth works anyway (might be using a different method) |
| 127 | + if gh auth status &> /dev/null; then |
| 128 | + echo "✅ GitHub CLI is authenticated (using alternative method)" |
| 129 | + gh auth status 2>&1 | head -n 3 || true |
| 130 | + fi |
| 131 | + fi |
| 132 | +else |
| 133 | + echo "⚠️ GitHub CLI not found" |
| 134 | +fi |
| 135 | + |
| 136 | +# Install Terraform Plugin Framework docs generator |
| 137 | +echo "📚 Installing Terraform Plugin Framework documentation generator..." |
| 138 | +go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest || echo "⚠️ Failed to install tfplugindocs (may retry later)" |
| 139 | + |
| 140 | +# Verify Go tools (non-blocking, may not be installed yet) |
| 141 | +echo "🔧 Verifying Go tools..." |
| 142 | +command -v golangci-lint >/dev/null && echo "golangci-lint: $(golangci-lint version)" || echo "⚠️ golangci-lint not found" |
| 143 | +command -v goimports >/dev/null && echo "goimports: $(which goimports)" || echo "⚠️ goimports not found" |
| 144 | +command -v gopls >/dev/null && echo "gopls: $(which gopls)" || echo "⚠️ gopls not found" |
| 145 | + |
| 146 | +# Download Go dependencies |
| 147 | +echo "📥 Downloading Go dependencies..." |
| 148 | +cd /workspaces/terraform-provider-githubx || cd /workspace |
| 149 | +go mod download || echo "⚠️ Go mod download failed (may retry later)" |
| 150 | +go mod verify || echo "⚠️ Go mod verify failed" |
| 151 | + |
| 152 | +# Build the provider to verify everything works |
| 153 | +echo "🔨 Building provider..." |
| 154 | +go build -buildvcs=false -o terraform-provider-githubx || echo "⚠️ Build failed (may retry later)" |
| 155 | + |
| 156 | +# Install provider locally for Terraform to use (only if build succeeded) |
| 157 | +echo "📦 Installing provider locally for Terraform..." |
| 158 | +if [ -f terraform-provider-githubx ]; then |
| 159 | +VERSION="0.1.0" |
| 160 | +PLATFORM="linux_amd64" |
| 161 | +PLUGIN_DIR="${HOME}/.terraform.d/plugins/registry.terraform.io/tfstack/githubx/${VERSION}/${PLATFORM}" |
| 162 | +mkdir -p "${PLUGIN_DIR}" |
| 163 | + cp terraform-provider-githubx "${PLUGIN_DIR}/" && echo "✅ Provider installed to ${PLUGIN_DIR}" || echo "⚠️ Failed to install provider" |
| 164 | +else |
| 165 | + echo "⚠️ Provider binary not found, skipping installation" |
| 166 | +fi |
| 167 | + |
| 168 | +# Initialize Terraform in examples (non-blocking, may fail if variables needed) |
| 169 | +echo "🔧 Initializing Terraform examples..." |
| 170 | +for dir in examples/data-sources/*/ examples/resources/*/ examples/provider/; do |
| 171 | + if [ -f "${dir}data-source.tf" ] || [ -f "${dir}resource.tf" ] || [ -f "${dir}provider.tf" ] || [ -f "${dir}main.tf" ] || [ -f "${dir}"*.tf ]; then |
| 172 | + echo " Initializing ${dir}..." |
| 173 | + (cd "${dir}" && terraform init -upgrade -input=false > /dev/null 2>&1 && echo " ✅ ${dir} initialized" || echo " ⚠️ ${dir} skipped (may need variables)") |
| 174 | + fi |
| 175 | + done |
| 176 | + |
| 177 | +# Load .env file if it exists |
| 178 | +echo "🔐 Loading environment variables from .env file..." |
| 179 | +if [ -f /workspaces/terraform-provider-githubx/.env ]; then |
| 180 | + set -a |
| 181 | + source /workspaces/terraform-provider-githubx/.env |
| 182 | + set +a |
| 183 | + echo "✅ Environment variables loaded from .env" |
| 184 | +elif [ -f /workspace/.env ]; then |
| 185 | + set -a |
| 186 | + source /workspace/.env |
| 187 | + set +a |
| 188 | + echo "✅ Environment variables loaded from .env" |
| 189 | +else |
| 190 | + echo "⚠️ No .env file found. Create one from .env.example if needed." |
| 191 | +fi |
| 192 | + |
| 193 | +echo "" |
| 194 | +echo "✅ Development environment setup complete!" |
| 195 | +echo "" |
| 196 | +echo "Available commands:" |
| 197 | +echo " make build - Build the provider" |
| 198 | +echo " make install - Install the provider" |
| 199 | +echo " make install-local - Install provider locally for Terraform testing" |
| 200 | +echo " make init-examples - Initialize Terraform in all examples" |
| 201 | +echo " make init-example - Initialize a specific example (EXAMPLE=path)" |
| 202 | +echo " make test - Run tests" |
| 203 | +echo " make fmt - Format code" |
| 204 | +echo " make docs - Generate documentation" |
| 205 | +echo "" |
| 206 | +echo "💡 The provider is already installed locally and examples are initialized!" |
| 207 | +echo " Navigate to any example directory and run 'terraform plan' or 'terraform apply'." |
0 commit comments