Skip to content

Commit

Permalink
refactor: move cobra/cmd/* to cobra/
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Jul 1, 2021
1 parent 07861c8 commit ae67f41
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 103 deletions.
2 changes: 1 addition & 1 deletion cobra/cmd/add.go → cobra/add.go
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/add_test.go → cobra/add_test.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
79 changes: 0 additions & 79 deletions cobra/cmd/root.go

This file was deleted.

2 changes: 1 addition & 1 deletion cobra/cmd/golden_test.go → cobra/golden_test.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/helpers.go → cobra/helpers.go
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package main

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/helpers_test.go → cobra/helpers_test.go
@@ -1,4 +1,4 @@
package cmd
package main

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/init.go → cobra/init.go
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/init_test.go → cobra/init_test.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/license_agpl.go → cobra/license_agpl.go
@@ -1,4 +1,4 @@
package cmd
package main

func initAgpl() {
Licenses["agpl"] = License{
Expand Down
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initApache2() {
Licenses["apache"] = License{
Expand Down
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initBsdClause2() {
Licenses["freebsd"] = License{
Expand Down
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initBsdClause3() {
Licenses["bsd"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/license_gpl_2.go → cobra/license_gpl_2.go
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initGpl2() {
Licenses["gpl2"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/license_gpl_3.go → cobra/license_gpl_3.go
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initGpl3() {
Licenses["gpl3"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/license_lgpl.go → cobra/license_lgpl.go
@@ -1,4 +1,4 @@
package cmd
package main

func initLgpl() {
Licenses["lgpl"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/license_mit.go → cobra/license_mit.go
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

func initMit() {
Licenses["mit"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/licenses.go → cobra/licenses.go
Expand Up @@ -13,7 +13,7 @@

// Parts inspired by https://github.com/ryanuber/go-license

package cmd
package main

import (
"fmt"
Expand Down
58 changes: 55 additions & 3 deletions cobra/main.go
Expand Up @@ -14,13 +14,65 @@
package main

import (
"fmt"
"os"

"github.com/spf13/cobra/cobra/cmd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func main() {
if err := cmd.Execute(); err != nil {
os.Exit(1)
cobra.CheckErr(rootCmd.Execute())
}

var (
// Used for flags.
cfgFile string
userLicense string

rootCmd = &cobra.Command{
Use: "cobra",
Short: "A generator for Cobra based Applications",
Long: `Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}
)

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")))
cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache")

rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(initCmd)
}

func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".cobra")
}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
2 changes: 1 addition & 1 deletion cobra/cmd/project.go → cobra/project.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cobra/cmd/project_test.go → cobra/project_test.go
@@ -1,3 +1,3 @@
package cmd
package main

/* todo: write tests */
File renamed without changes.
File renamed without changes.
Expand Up @@ -18,8 +18,8 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions cobra/tpl/main.go
Expand Up @@ -25,9 +25,9 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
{{ if .Viper }}
"github.com/spf13/viper"{{ end }}
{{ if .Viper }} "github.com/spf13/viper"{{ end }}
)
{{ if .Viper -}}
Expand Down

0 comments on commit ae67f41

Please sign in to comment.