Skip to content

Commit be514ca

Browse files
adding example
Signed-off-by: Christopher Hein <[email protected]>
1 parent 621eb71 commit be514ca

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

example/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package main
17+
18+
func main() {
19+
Execute()
20+
}

example/root.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package main
17+
18+
import (
19+
"fmt"
20+
"os"
21+
"github.com/spf13/cobra"
22+
23+
homedir "github.com/mitchellh/go-homedir"
24+
"github.com/spf13/viper"
25+
26+
)
27+
28+
29+
var cfgFile string
30+
31+
32+
// rootCmd represents the base command when called without any subcommands
33+
var rootCmd = &cobra.Command{
34+
Use: "example",
35+
Short: "A brief description of your application",
36+
Long: `A longer description that spans multiple lines and likely contains
37+
examples and usage of using your application. For example:
38+
39+
Cobra is a CLI library for Go that empowers applications.
40+
This application is a tool to generate the needed files
41+
to quickly create a Cobra application.`,
42+
// Uncomment the following line if your bare application
43+
// has an action associated with it:
44+
// Run: func(cmd *cobra.Command, args []string) { },
45+
}
46+
47+
// Execute adds all child commands to the root command and sets flags appropriately.
48+
// This is called by main.main(). It only needs to happen once to the rootCmd.
49+
func Execute() {
50+
if err := rootCmd.Execute(); err != nil {
51+
fmt.Println(err)
52+
os.Exit(1)
53+
}
54+
}
55+
56+
func init() {
57+
cobra.OnInitialize(initConfig)
58+
59+
// Here you will define your flags and configuration settings.
60+
// Cobra supports persistent flags, which, if defined here,
61+
// will be global for your application.
62+
63+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.example.yaml)")
64+
65+
66+
// Cobra also supports local flags, which will only run
67+
// when this action is called directly.
68+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
69+
}
70+
71+
72+
// initConfig reads in config file and ENV variables if set.
73+
func initConfig() {
74+
if cfgFile != "" {
75+
// Use config file from the flag.
76+
viper.SetConfigFile(cfgFile)
77+
} else {
78+
// Find home directory.
79+
home, err := homedir.Dir()
80+
if err != nil {
81+
fmt.Println(err)
82+
os.Exit(1)
83+
}
84+
85+
// Search config in home directory with name ".example" (without extension).
86+
viper.AddConfigPath(home)
87+
viper.SetConfigName(".example")
88+
}
89+
90+
viper.AutomaticEnv() // read in environment variables that match
91+
92+
// If a config file is found, read it in.
93+
if err := viper.ReadInConfig(); err == nil {
94+
fmt.Println("Using config file:", viper.ConfigFileUsed())
95+
}
96+
}
97+

example/version.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package main
17+
18+
import (
19+
"github.com/spf13/cobra"
20+
goVersion "go.hein.dev/go-version"
21+
)
22+
23+
var (
24+
shortened = false
25+
version = "dev"
26+
commit = "none"
27+
date = "unknown"
28+
versionCmd = &cobra.Command{
29+
Use: "version",
30+
Short: "Version will output the current build information",
31+
Long: ``,
32+
Run: goVersion.Func(shortened, version, commit, date),
33+
}
34+
)
35+
36+
func init() {
37+
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
38+
rootCmd.AddCommand(versionCmd)
39+
}

hack/build_example.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
commitSHA=$(git describe --dirty --always)
4+
dateStr=$(date +%s)
5+
6+
go build -ldflags "-X main.commit=${commitSHA} -X main.date=${dateStr}" -o ./example/example ./example/

0 commit comments

Comments
 (0)