diff --git a/template/go/.gitignore b/template/go/.gitignore index a1013d8..24f6fa8 100644 --- a/template/go/.gitignore +++ b/template/go/.gitignore @@ -4,6 +4,7 @@ ### Go template # Binaries for programs and plugins +bin *.exe *.exe~ *.dll @@ -18,4 +19,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ - diff --git a/template/go/Makefile b/template/go/Makefile index 768eebf..da693c9 100644 --- a/template/go/Makefile +++ b/template/go/Makefile @@ -1,13 +1,20 @@ -.PHONY: help +.PHONY: help image run build .DEFAULT_GOAL := help APP_NAME := {{project-name}} +APP_PATH=. # build image image: @docker image build -t $(APP_NAME) . -# buildx cross +# run +run: + @go run $(APP_PATH)/main.go + +# build +build: + @mkdir -p bin; go build -trimpath -ldflags="-w -s" -o bin/ $(APP_PATH) # Show help help: diff --git a/template/go/cmd/root.go b/template/go/cmd/root.go new file mode 100644 index 0000000..5bc2f04 --- /dev/null +++ b/template/go/cmd/root.go @@ -0,0 +1,64 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/viper" + "os" + + "github.com/spf13/cobra" +) + +var cfgFile string + +var rootCmd = &cobra.Command{ + Use: "{{project-name}}", + Short: "A short desc for the {{project-name}}", + Long: `A long desc for the {{project-name}}. +Such as: +- Feature1 +- Feature2 +- Feature3 +`, +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + cobra.OnInitialize(initConfig) +} + +// initConfig reads in config file and ENV variables if set. +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + viper.AddConfigPath("$HOME/.config/blog-go") + viper.AddConfigPath("./config/") + + viper.SetConfigName("config") + viper.SetConfigType("yaml") + } + + // set the default values + setDefaultConfig() + + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err != nil { + fmt.Printf("%s\n\n\n", err.Error()) + _ = rootCmd.Help() + os.Exit(1) + } + fmt.Printf("======== Using config file: %s ========\n", viper.ConfigFileUsed()) +} + +func setDefaultConfig() { + // Some default values +} diff --git a/template/go/go.mod b/template/go/go.mod index 6e4e72c..3f492ee 100644 --- a/template/go/go.mod +++ b/template/go/go.mod @@ -1,3 +1,31 @@ module github.com/{{gh_uname}}/{{project-name}} go {{go_version}} + +require ( + github.com/spf13/cobra v1.8.0 + github.com/spf13/viper v1.18.2 +) + +require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/template/go/internal/.gitkeep b/template/go/internal/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/template/go/main.go b/template/go/main.go new file mode 100644 index 0000000..2fd6b9d --- /dev/null +++ b/template/go/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/{{gh_uname}}/{{project-name}}/cmd" +) + +func main() { + cmd.Execute() +} diff --git a/template/go/src/main.go b/template/go/src/main.go deleted file mode 100644 index 635db7a..0000000 --- a/template/go/src/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("hello, world") -}