Skip to content

Commit

Permalink
feat: go template with cobra and viper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystic committed Feb 1, 2024
1 parent 87826fb commit 08a7989
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 10 deletions.
2 changes: 1 addition & 1 deletion template/go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Go template
# Binaries for programs and plugins
bin
*.exe
*.exe~
*.dll
Expand All @@ -18,4 +19,3 @@

# Dependency directories (remove the comment below to include it)
# vendor/

11 changes: 9 additions & 2 deletions template/go/Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
64 changes: 64 additions & 0 deletions template/go/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions template/go/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Empty file added template/go/internal/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions template/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/{{gh_uname}}/{{project-name}}/cmd"
)

func main() {
cmd.Execute()
}
7 changes: 0 additions & 7 deletions template/go/src/main.go

This file was deleted.

0 comments on commit 08a7989

Please sign in to comment.