Last active
May 17, 2023 09:23
-
-
Save rudifa/6c26635c9da06744e0764e9b34fc40de to your computer and use it in GitHub Desktop.
This file contains hidden or 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: $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 |
Author
rudifa
commented
May 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment