Skip to content

Commit

Permalink
refactor: mv cmd/* ./
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Oct 4, 2022
1 parent e9c980d commit e968820
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 114 deletions.
2 changes: 1 addition & 1 deletion cmd/add.go → add.go
Original file line number Diff line number Diff line change
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 cmd/add_test.go → add_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

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

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/golden_test.go → golden_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cmd/helpers.go → helpers.go
Original file line number Diff line number Diff line change
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 cmd/helpers_test.go → helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go → init.go
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion cmd/init_test.go → init_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/license_agpl.go → license_agpl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

func initAgpl() {
Licenses["agpl"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cmd/license_apache_2.go → license_apache_2.go
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion cmd/license_bsd_clause_2.go → license_bsd_clause_2.go
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion cmd/license_bsd_clause_3.go → license_bsd_clause_3.go
Original file line number Diff line number Diff line change
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 cmd/license_gpl_2.go → license_gpl_2.go
Original file line number Diff line number Diff line change
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 cmd/license_gpl_3.go → license_gpl_3.go
Original file line number Diff line number Diff line change
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 cmd/license_lgpl.go → license_lgpl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

func initLgpl() {
Licenses["lgpl"] = License{
Expand Down
2 changes: 1 addition & 1 deletion cmd/license_mit.go → license_mit.go
Original file line number Diff line number Diff line change
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 cmd/licenses.go → licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

package cmd
package main

import (
"fmt"
Expand Down
68 changes: 66 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,77 @@
package main

import (
"errors"
"fmt"
"os"

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

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

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

rootCmd = &cobra.Command{
Use: "cobra-cli",
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()

err := viper.ReadInConfig()

notFound := &viper.ConfigFileNotFoundError{}
switch {
case err != nil && !errors.As(err, notFound):
cobra.CheckErr(err)
case err != nil && errors.As(err, notFound):
// The config file is optional, we shouldn't exit when the config is not found
break
default:
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
2 changes: 1 addition & 1 deletion cmd/project.go → project.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/project_test.go → project_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd
package main

/* todo: write tests */
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/testdata/main.go.golden → testdata/main.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/
package main

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

func main() {
cmd.Execute()
Expand Down
8 changes: 4 additions & 4 deletions cmd/testdata/root.go.golden → testdata/root.go.golden
Original file line number Diff line number Diff line change
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-cli",
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 Down Expand Up @@ -56,7 +56,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-cli.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -73,10 +73,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-cli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".cmd")
viper.SetConfigName(".cobra-cli")
}

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

0 comments on commit e968820

Please sign in to comment.