-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge-module.sh
More file actions
52 lines (44 loc) · 1.4 KB
/
forge-module.sh
File metadata and controls
52 lines (44 loc) · 1.4 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
#!/bin/bash
# List directories in the current folder
echo "📂 Available directories:"
select DIR in */ "Current (.)"; do
if [[ "$DIR" == "Current (.)" ]]; then
TARGET_DIR="."
elif [[ -n "$DIR" ]]; then
TARGET_DIR="${DIR%/}" # Remove trailing slash
else
echo "❌ Invalid selection. Try again."
continue
fi
break
done
# Ensure selected directory contains a Go module
if [ ! -f "$TARGET_DIR/go.mod" ]; then
echo "❌ No 'go.mod' file found in '$TARGET_DIR'! Make sure it's a Go module."
exit 1
fi
# Ask user for the new package name
read -p "Enter the name of your new package: " PACKAGE_NAME
# Create package directory inside the target directory
mkdir -p "$TARGET_DIR/$PACKAGE_NAME"
# Create a sample file inside the package
cat > "$TARGET_DIR/$PACKAGE_NAME/$PACKAGE_NAME.go" <<EOL
package $PACKAGE_NAME
import "fmt"
// ExampleFunc prints a message from $PACKAGE_NAME.
func ExampleFunc() {
fmt.Println("Hello from $PACKAGE_NAME package!")
}
EOL
# Suggest usage example in main.go
if [ -f "$TARGET_DIR/main.go" ]; then
echo "✅ Package '$PACKAGE_NAME' created in '$TARGET_DIR'!"
echo "🔹 To use it, add this to 'main.go':"
echo ""
echo " import \"./$PACKAGE_NAME\""
echo " func main() {"
echo " $PACKAGE_NAME.ExampleFunc()"
echo " }"
else
echo "✅ Package '$PACKAGE_NAME' created. Add it to your Go files as needed!"
fi