Fine-tune the formatting of Cobra’s generated code

This commit is contained in:
Anthony Fok 2015-11-20 15:28:40 -07:00
parent c7feee9f20
commit 1e63ad8ed1
5 changed files with 83 additions and 81 deletions

View File

@ -33,10 +33,12 @@ var addCmd = &cobra.Command{
Use: "add [command name]",
Aliases: []string{"command"},
Short: "Add a command to a Cobra Application",
Long: `Add will create a new command, with a license and the appropriate structure
for a Cobra based CLI application and register it to it's parent (default RootCmd).
Long: `Add (cobra add) will create a new command, with a license and
the appropriate structure for a Cobra-based CLI application,
and register it to its parent (default RootCmd).
If you want your command to be public, pass in the command name with an initial upper case letter.
If you want your command to be public, pass in the command name
with an initial uppercase letter.
Example: cobra add server -> resulting in a new cmd/server.go
`,
@ -66,8 +68,8 @@ func createCmdFile(cmdName string) {
lic := getLicense()
template := `{{ comment .copyright }}
//
{{ comment .license }}
package cmd
import (
@ -83,9 +85,9 @@ var {{ .cmdName }}Cmd = &cobra.Command{
Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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.` + "`" + `,
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.` + "`" + `,
Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
fmt.Println("{{ .cmdName }} called")
@ -95,12 +97,14 @@ application.` + "`" + `,
func init() {
{{ .parentName }}.AddCommand({{ .cmdName }}Cmd)
// Here you will define your flags and configuration settings
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command and all subcommands
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command is called directly
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -141,18 +141,19 @@ func guessProjectPath() {
}
srcPath := getSrcPath()
// if provided inspect for logical locations
// if provided, inspect for logical locations
if strings.ContainsRune(inputPath, os.PathSeparator) {
if filepath.IsAbs(inputPath) {
// if Absolute, use it
projectPath = filepath.Clean(inputPath)
return
}
// If not absolute but contains slashes.. assuming it means create it from $GOPATH
// If not absolute but contains slashes,
// assuming it means create it from $GOPATH
count := strings.Count(inputPath, string(os.PathSeparator))
switch count {
// If only one directory deep assume "github.com"
// If only one directory deep, assume "github.com"
case 1:
projectPath = filepath.Join(srcPath, "github.com", inputPath)
return
@ -178,7 +179,7 @@ func guessProjectPath() {
}
}
// IsEmpty checks if a given path is empty.
// isEmpty checks if a given path is empty.
func isEmpty(path string) (bool, error) {
if b, _ := exists(path); !b {
return false, fmt.Errorf("%q path does not exist", path)
@ -202,7 +203,7 @@ func isEmpty(path string) (bool, error) {
return fi.Size() == 0, nil
}
// IsDir checks if a given path is a directory.
// isDir checks if a given path is a directory.
func isDir(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
@ -211,7 +212,7 @@ func isDir(path string) (bool, error) {
return fi.IsDir(), nil
}
// DirExists checks if a path exists and is a directory.
// dirExists checks if a path exists and is a directory.
func dirExists(path string) (bool, error) {
fi, err := os.Stat(path)
if err == nil && fi.IsDir() {
@ -308,13 +309,13 @@ func getLicense() License {
}
func whichLicense() string {
// if explicitly flagged use that
// if explicitly flagged, use that
if userLicense != "" {
return matchLicense(userLicense)
}
// if already present in the project, use that
// TODO:Inpect project for existing license
// TODO: Inspect project for existing license
// default to viper's setting
@ -333,7 +334,11 @@ func commentifyString(in string) string {
lines := strings.Split(in, "\n")
for _, x := range lines {
if !strings.HasPrefix(x, "//") {
if x != "" {
newlines = append(newlines, "// "+x)
} else {
newlines = append(newlines, "//")
}
} else {
newlines = append(newlines, x)
}

View File

@ -30,15 +30,15 @@ var initCmd = &cobra.Command{
Use: "init [name]",
Aliases: []string{"initialize", "initalise", "create"},
Short: "Initalize a Cobra Application",
Long: `Initalize will create a new application, with a license and the appropriate structure
for a Cobra based CLI application.
Long: `Initialize (cobra init) will create a new application, with a license
and the appropriate structure for a Cobra-based CLI application.
If a name is provided it will create it in the current directory.
If no name is provided it will assume the current directory.
If a relative path is provided it will create it inside of $GOPATH (eg github.com/spf13/hugo).
If an absolute path is provided it will create it.
If the directory already exists but is empty it will use it.
* If a name is provided, it will be created in the current directory;
* If no name is provided, the current directory will be assumed;
* If a relative path is provided, it will be created inside $GOPATH
(e.g. github.com/spf13/hugo);
* If an absolute path is provided, it will be created;
* If the directory already exists but is empty, it will be used.
Init will not use an exiting directory with contents.`,
@ -98,7 +98,6 @@ func createMainFile() {
lic := getLicense()
template := `{{ comment .copyright }}
//
{{ comment .license }}
package main
@ -127,8 +126,8 @@ func createRootCmdFile() {
lic := getLicense()
template := `{{ comment .copyright }}
//
{{ comment .license }}
package cmd
import (
@ -136,29 +135,28 @@ import (
"os"
"github.com/spf13/cobra"
{{ if .viper }}"github.com/spf13/viper"{{ end }}
)
{{ if .viper }} "github.com/spf13/viper"
{{ end }})
{{if .viper}}
var cfgFile string
{{ end }}
// This represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "{{ .appName }}",
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:
Long: ` + "`" + `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
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.` + "`" + `,
// Uncomment the following line if your bare application has an action associated with it
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.` + "`" + `,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
@ -167,21 +165,21 @@ func Execute() {
}
func init() {
{{ if .viper }}cobra.OnInitialize(initConfig){{ end}}
// Here you will define your flags and configuration settings
// Cobra supports Persistent Flags which if defined here will be global for your application
{{ if .viper }} cobra.OnInitialize(initConfig)
{{ end }} // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
{{ if .viper }}
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ else }}
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ end }}
// Cobra also supports local flags which will only run when this action is called directly
{{ end }} // Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
{{ if .viper }}
// Read in config file and ENV variables if set.
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
@ -196,8 +194,7 @@ func initConfig() {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
{{ end }}
`
{{ end }}`
var data map[string]interface{}
data = make(map[string]interface{})

View File

@ -55,8 +55,7 @@ func init() {
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
`,
// limitations under the License.`,
Text: `
Apache License
@ -285,8 +284,7 @@ func init() {
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`,
SOFTWARE.`,
Text: `
The MIT License (MIT)
@ -336,8 +334,7 @@ func init() {
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`,
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.`,
Text: `
All rights reserved.
@ -393,8 +390,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
`,
either expressed or implied, of the FreeBSD Project.`,
Text: `
All rights reserved.

View File

@ -28,9 +28,9 @@ var userLicense string
var 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.`,
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.`,
}
//Execute adds all child commands to the root command sets flags appropriately.
@ -44,7 +44,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/")
RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")