Last active
May 28, 2023 11:34
-
-
Save rudifa/49b3530a0bc9536a67fda44a1a14eacf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create a new Go project with a main.go file and a Go module | |
# Option -c, --cobra: Initialize a Cobra CLI project in the module | |
while getopts ":c" opt; do | |
case $opt in | |
c) | |
c_flag=true | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ -z "$1" ]; then | |
echo "Usage: $(basename "$0") [-c] dir_name" | |
echo "Creates a new directory, initializes a Go module, and creates a main.go file." | |
echo "Options:" | |
echo " -c, --cobra Initialize a Cobra CLI project in the module" | |
else | |
dir_name="$1" | |
mkdir "$dir_name" && cd "$dir_name" | |
# Initialize Go module | |
go mod init "$dir_name" | |
cat <<EOF >main.go | |
package main | |
import "fmt" | |
func main() { | |
fmt.Println("Here we go") | |
} | |
EOF | |
if [ "$c_flag" = true ]; then | |
# Initialize cobra-cli | |
cobra-cli init | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment