Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions commands/host/1x-token-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash
## #ddev-generated
# Description: Setup GitLab tokens and configure ddev environments
# Usage: 1x-token-setup

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure we stop if this is not supported by the Host OS.

set -e

# Stop the current DDEV project (if running).
if ddev describe > /dev/null 2>&1; then
ddev stop
fi

# Change to project root
cd $DDEV_APPROOT

# Ensure the DDEV homeadditions directory exists on your host.
mkdir -p ~/.ddev/homeadditions/

# On your host, create or ensure ~/.composer and ~/.npmrc exist.
mkdir -p ~/.composer
if [ ! -f ~/.composer/auth.json ]; then
echo '{}' > ~/.composer/auth.json
fi
touch ~/.npmrc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test here to check that we have these files/directories exist.

Or rather: we should state their existence or non-existence as iirc this will work as well w/o.

# Symlink your local config into DDEV's homeadditions
# Using -f to force overwrite if symlink exists
ln -sf "$(realpath ~/.composer)" ~/.ddev/homeadditions/.composer
ln -sf "$(realpath ~/.npmrc)" ~/.ddev/homeadditions/.npmrc

# Also directly ensure homeadditions has valid JSON structure if symlinks fail or for redundancy
# (Though symlinks should cover this, script follows user request logic)
mkdir -p ~/.ddev/homeadditions/.composer
if [ ! -f ~/.ddev/homeadditions/.composer/auth.json ]; then
echo '{}' > ~/.ddev/homeadditions/.composer/auth.json
fi
touch ~/.ddev/homeadditions/.npmrc

# Set your personal access token
# Check if PERSONAL_ACCESS_TOKEN is set, otherwise prompt
if [ -z "$PERSONAL_ACCESS_TOKEN" ]; then
echo "Please enter your GitLab Personal Access Token (must have read_api & read_repository):"
read -s PERSONAL_ACCESS_TOKEN
fi

if [ -z "$PERSONAL_ACCESS_TOKEN" ]; then
echo "Error: PERSONAL_ACCESS_TOKEN is required."
exit 1
fi

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validating that this token can is correct and works here would be a nice touch. (Is there a way to test this without introducing e.g. a glab dependency on the host - or should we mandate glab on the host?)

# Configure NPM in homeadditions so the container picks it up.
# We append if not already present to avoid duplication
if ! grep -q "@dxp:registry=https://git.1xinternet.de/api/v4/groups/392/-/packages/npm/" ~/.ddev/homeadditions/.npmrc; then
echo "@dxp:registry=https://git.1xinternet.de/api/v4/groups/392/-/packages/npm/" >> ~/.ddev/homeadditions/.npmrc
fi

# Remove old token if exists to replace with new one, or just append
# A simple append might create duplicates, so let's try to be a bit smarter or just append as requested
# User request: echo "//git.1xinternet.de/api/v4/groups/392/-/packages/npm/:_authToken=${PERSONAL_ACCESS_TOKEN}" >> ~/.ddev/homeadditions/.npmrc
# We will use sed to remove existing token line if it matches the registry to avoid clutter
sed -i '/\/\/git.1xinternet.de\/api\/v4\/groups\/392\/-\/packages\/npm\/:_authToken=/d' ~/.ddev/homeadditions/.npmrc
echo "//git.1xinternet.de/api/v4/groups/392/-/packages/npm/:_authToken=${PERSONAL_ACCESS_TOKEN}" >> ~/.ddev/homeadditions/.npmrc

# Start the DDEV project (so containers run).
ddev start

# Ensure valid JSON inside the container.
ddev exec "test -e \$HOME/.composer/auth.json || echo '{}' > \$HOME/.composer/auth.json"

# Verify it directly from your host using ddev exec:
echo "Verifying auth.json in container..."
ddev exec "cat \$HOME/.composer/auth.json"

# Now safely configure Composer with your GitLab token globally (in the container):
ddev composer config --global --auth gitlab-token.git.1xinternet.de "${PERSONAL_ACCESS_TOKEN}"

# Restart so everything is fully synced.
ddev stop
ddev start

echo "Setup complete!"

# Note: if
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left this out - maybe people want that ... hide behind a prompt.

# Persist the new auth.json from the container back to your host (if desired):
# ddev exec 'cat $HOME/.composer/auth.json' > ~/.composer/auth.json