-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: go template with cobra and viper
- Loading branch information
Mystic
committed
Feb 1, 2024
1 parent
87826fb
commit 08a7989
Showing
7 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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 | ||
} |
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
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.
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
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() | ||
} |
This file was deleted.
Oops, something went wrong.