Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move cobra/cmd/* to cobra/ #1430

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
61 changes: 58 additions & 3 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,68 @@
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 {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

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.Fprintln(os.Stderr, "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 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.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" (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.