-
-
Save kwo/d53a6f9c375707e761d11c39844c4a40 to your computer and use it in GitHub Desktop.
go run wrapper to run a go program which is either in the current directory or in the cmd/<modulename> subdirectory
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 | |
# check if main.go file exists | |
if [ -f "main.go" ]; then | |
echo "go run ." | |
go run . | |
exit $? | |
fi | |
# check if go.mod file exists | |
if [ ! -f "go.mod" ]; then | |
echo "go.mod file not found" | |
exit 1 | |
fi | |
# read the first line of the "go.mod" file | |
first_line=$(head -n 1 go.mod) | |
# check if the line is empty | |
if [[ -z "$first_line" ]]; then | |
echo "module name not found" | |
exit 1 | |
fi | |
# split the first line by whitespace and extract the second element (assuming "module" is the first) | |
parts=($first_line) | |
if [[ ${#parts[@]} -lt 2 || "${parts[0]}" != "module" ]]; then | |
echo "module name not found" | |
exit 1 | |
fi | |
# print the module name | |
module_name="${parts[1]}" | |
#echo "${module_name}" | |
last_path_element="${module_name##*/}" | |
#echo "${last_path_element}" | |
# check if cmd subdirectory exists | |
if [ ! -d "cmd" ]; then | |
echo "cmd subdirectory not found" | |
exit 1 | |
fi | |
# check if cmd subdirectory exists | |
if [ ! -d "cmd/$last_path_element" ]; then | |
echo "cmd/$last_path_element subdirectory not found" | |
exit 1 | |
fi | |
# run the command | |
cmd_name="$module_name/cmd/$last_path_element" | |
echo "go run $cmd_name" | |
go run $cmd_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment