|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Determine the zinit binary path |
| 4 | +ZINIT_BIN="./target/release/zinit" # Assuming zinit is built in release mode in the current directory |
| 5 | + |
| 6 | +# Determine the configuration directory based on OS |
| 7 | +if [[ "$(uname)" == "Darwin" ]]; then |
| 8 | + # macOS |
| 9 | + ZINIT_CONFIG_DIR="$HOME/hero/cfg/zinit" |
| 10 | +else |
| 11 | + # Linux or other |
| 12 | + ZINIT_CONFIG_DIR="/etc/zinit" |
| 13 | +fi |
| 14 | + |
| 15 | +SERVICE_NAME="test_service" |
| 16 | +SERVICE_FILE="$ZINIT_CONFIG_DIR/$SERVICE_NAME.yaml" |
| 17 | + |
| 18 | +echo "--- Zinit Example Script ---" |
| 19 | +echo "Zinit binary path: $ZINIT_BIN" |
| 20 | +echo "Zinit config directory: $ZINIT_CONFIG_DIR" |
| 21 | + |
| 22 | +# Step 1: Ensure zinit config directory exists |
| 23 | +echo "Ensuring zinit config directory exists..." |
| 24 | +mkdir -p "$ZINIT_CONFIG_DIR" |
| 25 | +if [ $? -ne 0 ]; then |
| 26 | + echo "Error: Failed to create config directory $ZINIT_CONFIG_DIR. Exiting." |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | +echo "Config directory $ZINIT_CONFIG_DIR is ready." |
| 30 | + |
| 31 | +# Step 2: Check if zinit daemon is running, if not, start it in background |
| 32 | +echo "Checking if zinit daemon is running..." |
| 33 | +if "$ZINIT_BIN" list > /dev/null 2>&1; then |
| 34 | + echo "Zinit daemon is already running." |
| 35 | +else |
| 36 | + echo "Zinit daemon not running. Starting it in background..." |
| 37 | + # Start zinit init in a new process group to avoid it being killed by script exit |
| 38 | + # and redirecting output to /dev/null |
| 39 | + nohup "$ZINIT_BIN" init > /dev/null 2>&1 & |
| 40 | + ZINIT_PID=$! |
| 41 | + echo "Zinit daemon started with PID: $ZINIT_PID" |
| 42 | + sleep 2 # Give zinit a moment to start up and create the socket |
| 43 | + if ! "$ZINIT_BIN" list > /dev/null 2>&1; then |
| 44 | + echo "Error: Zinit daemon failed to start. Exiting." |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + echo "Zinit daemon successfully started." |
| 48 | +fi |
| 49 | + |
| 50 | +# Step 3: Create a sample zinit service file |
| 51 | +echo "Creating sample service file: $SERVICE_FILE" |
| 52 | +cat <<EOF > "$SERVICE_FILE" |
| 53 | +name: $SERVICE_NAME |
| 54 | +exec: /bin/bash -c "while true; do echo 'Hello from $SERVICE_NAME!'; sleep 5; done" |
| 55 | +log: stdout |
| 56 | +EOF |
| 57 | + |
| 58 | +if [ $? -ne 0 ]; then |
| 59 | + echo "Error: Failed to create service file $SERVICE_FILE. Exiting." |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | +echo "Service file created." |
| 63 | + |
| 64 | +# Step 4: Tell zinit to monitor the new service |
| 65 | +echo "Telling zinit to monitor the service..." |
| 66 | +"$ZINIT_BIN" monitor "$SERVICE_NAME" |
| 67 | + |
| 68 | +# Step 5: List services to verify the new service is recognized |
| 69 | +echo "Listing zinit services to verify..." |
| 70 | +"$ZINIT_BIN" list |
| 71 | + |
| 72 | +# Step 6: Clean up (optional, but good for examples) |
| 73 | +echo "Cleaning up: stopping and forgetting $SERVICE_NAME..." |
| 74 | +"$ZINIT_BIN" stop "$SERVICE_NAME" > /dev/null 2>&1 |
| 75 | +"$ZINIT_BIN" forget "$SERVICE_NAME" > /dev/null 2>&1 |
| 76 | +rm -f "$SERVICE_FILE" |
| 77 | +echo "Cleanup complete." |
| 78 | + |
| 79 | +echo "--- Script Finished ---" |
0 commit comments