Skip to content

Commit

Permalink
refactor: move cobra/cmd/* to cobra/ (spf13#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Nov 4, 2021
1 parent 141ddbe commit 2fd2063
Show file tree
Hide file tree
Showing 24 changed files with 80 additions and 107 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 (
"encoding/json"
Expand Down
4 changes: 2 additions & 2 deletions cobra/cmd/init_test.go → cobra/init_test.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand All @@ -17,7 +17,7 @@ func getProject() *Project {
Legal: getLicense(),
Copyright: copyrightLine(),
AppName: "cmd",
PkgName: "github.com/spf13/cobra/cobra/cmd/cmd",
PkgName: "github.com/spf13/cobra/cobra/cmd",
Viper: true,
}
}
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
60 changes: 56 additions & 4 deletions cobra/main.go
@@ -1,4 +1,4 @@
// Copyright © 2015 Steve Francia <spf@spf13.com>.
// Copyright © 2021 Steve Francia <spf@spf13.com>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -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", false, "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", "none")

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.
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/
package main

import "github.com/spf13/cobra/cobra/cmd/cmd"
import "github.com/spf13/cobra/cobra/cmd"

func main() {
cmd.Execute()
Expand Down
Expand Up @@ -27,7 +27,7 @@ var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cmd",
Use: "cobra",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Expand All @@ -53,7 +53,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -70,10 +70,10 @@ func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

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

viper.AutomaticEnv() // read in environment variables that match
Expand Down
File renamed without changes.

0 comments on commit 2fd2063

Please sign in to comment.