Skip to content

Instantly share code, notes, and snippets.

@rudifa
Last active May 17, 2023 09:23
Show Gist options
  • Save rudifa/6c26635c9da06744e0764e9b34fc40de to your computer and use it in GitHub Desktop.
Save rudifa/6c26635c9da06744e0764e9b34fc40de to your computer and use it in GitHub Desktop.
#!/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
@rudifa
Copy link
Author

rudifa commented May 16, 2023

cobra-sting % cd ..                                                                   [main L|✚2…3]
LEARN % gopro tmp                                                                     [main L|✚2…3]
go: creating new go.mod: module tmp
tmp % ll                                                                              [main L|✚2…4]
total 16
-rw-r--r--  1 rudifarkas  staff    20B May 16 23:03 go.mod
-rw-r--r--  1 rudifarkas  staff    74B May 16 23:03 main.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment