-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForge.sh
More file actions
136 lines (115 loc) Β· 3.3 KB
/
Copy pathForge.sh
File metadata and controls
136 lines (115 loc) Β· 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Function to detect OS
detect_os() {
OS="$(uname -s)"
case "$OS" in
Linux*) echo "linux";;
Darwin*) echo "mac";;
CYGWIN*|MINGW32*|MSYS*|MINGW*) echo "windows";;
*) echo "unknown";;
esac
}
OS_TYPE=$(detect_os)
# Ask for project name
read -p "π¦ Enter your project name (default: go-app): " PROJECT_NAME
PROJECT_NAME=${PROJECT_NAME:-go-app}
# Ask for Git repository URL
read -p "π Enter your Git repository URL (leave empty to skip cloning): " REPO_URL
# Check if Go is installed
if ! command -v go &> /dev/null; then
echo "π Go is not installed. Installing now..."
case "$OS_TYPE" in
linux) curl -OL https://golang.org/dl/go1.21.0.linux-amd64.tar.gz && \
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz && \
export PATH=$PATH:/usr/local/go/bin ;;
mac) brew install go ;;
windows) echo "π΄ Please install Go manually from https://go.dev/dl/"; exit 1 ;;
esac
echo "β
Go installed successfully!"
else
echo "β
Go is already installed."
fi
# Set up Go environment
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
# Clone project repository if provided
if [ -n "$REPO_URL" ]; then
if [ -d "$PROJECT_NAME" ]; then
echo "π Project already exists. Pulling latest changes..."
cd "$PROJECT_NAME" && git pull
else
echo "π¦ Cloning project..."
git clone "$REPO_URL" "$PROJECT_NAME"
cd "$PROJECT_NAME"
fi
else
mkdir -p "$PROJECT_NAME"
cd "$PROJECT_NAME"
echo "π Created project directory: $PROJECT_NAME"
fi
# Initialize Go module
if [ ! -f "go.mod" ]; then
echo "π Initializing Go module..."
go mod init "$PROJECT_NAME"
fi
# Load environment variables if .env exists
if [ -f ".env" ]; then
echo "π Loading environment variables..."
export $(grep -v '^#' .env | xargs)
else
echo "β οΈ .env file not found! Using default settings."
fi
# Install dependencies
echo "π¦ Installing dependencies..."
go mod tidy
# Create a basic main.go file if not exists
if [ ! -f "main.go" ]; then
cat <<EOF > main.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Go App!")
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("π Server is running on port 8080")
http.ListenAndServe(":8080", nil)
}
EOF
echo "β
Created a basic main.go file"
fi
# Build the project
echo "π¨ Building project..."
go build -o app main.go
# Run the project
echo "π Starting application..."
./app &
# Docker Support
read -p "π³ Do you want to build and run the project in Docker? (y/n): " RUN_DOCKER
if [[ "$RUN_DOCKER" == "y" ]]; then
# Create a basic Dockerfile if not exists
if [ ! -f "Dockerfile" ]; then
cat <<EOF > Dockerfile
# Use official Golang image
FROM golang:1.21 AS builder
WORKDIR /app
COPY . .
RUN go mod tidy
RUN go build -o app
# Run the app
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/app .
CMD ["./app"]
EOF
echo "β
Created a basic Dockerfile"
fi
echo "π³ Building Docker image..."
docker build -t "$PROJECT_NAME" .
echo "π³ Running Docker container..."
docker run -p 8080:8080 "$PROJECT_NAME"
fi
echo "π Setup complete!"