Repository: spf13/cobra
Pull request #1097 (2020-04-21 13:21:53)

replace labeler action with periodic-labeler
this replaces the default labeler as it
does not label PRs from forks properly.

with periodic-labeler it should apply labels on
a cron to any PR and resolve the below bug:

https://github.com/spf13/cobra/issues/1092

xchapter7x: @jharshman @jpmcb 

Thoughts on this PR as a fix for https://github.com/spf13/cobra/issues/1092 ?

Patch:
diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml
deleted file mode 100644
index e90b599..0000000
--- a/.github/workflows/label.yml
+++ /dev/null
@@ -1,19 +0,0 @@
-# This workflow will triage pull requests and apply a label based on the
-# paths that are modified in the pull request.
-#
-# To use this workflow, you will need to set up a .github/labeler.yml
-# file with configuration.  For more information, see:
-# https://github.com/actions/labeler/blob/master/README.md
-
-name: Labeler
-on: [pull_request]
-
-jobs:
-  label:
-
-    runs-on: ubuntu-latest
-
-    steps:
-    - uses: actions/labeler@v2
-      with:
-        repo-token: "${{ secrets.GITHUB_TOKEN }}"
diff --git a/.github/workflows/periodic-labeler.yml b/.github/workflows/periodic-labeler.yml
new file mode 100644
index 0000000..dfbb8e4
--- /dev/null
+++ b/.github/workflows/periodic-labeler.yml
@@ -0,0 +1,17 @@
+---
+name: Pull request labeler
+on:
+  schedule:
+    - cron: '*/5 * * * *'
+jobs:
+  labeler:
+    runs-on: ubuntu-latest
+    steps:
+      # if we are to change the labeler version from v0.0.2
+      # we must review the code for that version
+      # to make sure there are no leaks or exploits
+      - uses: paulfantom/periodic-labeler@v0.0.2
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          GITHUB_REPOSITORY: ${{ github.repository }}
+          LABEL_MAPPINGS_FILE: .github/labeler.yml
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..742d6d6
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,22 @@
+# Cobra Changelog
+
+## Pending
+* Fix man page doc generation - no auto generated tag when `cmd.DisableAutoGenTag = true` @jpmcb
+
+## v1.0.0
+Announcing v1.0.0 of Cobra. 🎉
+**Notable Changes**
+* Fish completion (including support for Go custom completion) @marckhouzam
+* API (urgent): Rename BashCompDirectives to ShellCompDirectives @marckhouzam
+* Remove/replace SetOutput on Command - deprecated @jpmcb
+* add support for autolabel stale PR @xchapter7x
+* Add Labeler Actions @xchapter7x
+* Custom completions coded in Go (instead of Bash) @marckhouzam
+* Partial Revert of #922 @jharshman
+* Add Makefile to project @jharshman
+* Correct documentation for InOrStdin @desponda
+* Apply formatting to templates @jharshman
+* Revert change so help is printed on stdout again @marckhouzam
+* Update md2man to v2.0.0 @pdf
+* update viper to v1.4.0 @umarcor
+* Update cmd/root.go example in README.md @jharshman
diff --git a/README.md b/README.md
index a371366..a932dd7 100644
--- a/README.md
+++ b/README.md
@@ -718,11 +718,7 @@ Run 'kubectl help' for usage.
 
 ## Generating documentation for your command
 
-Cobra can generate documentation based on subcommands, flags, etc. in the following formats:
-
-- [Markdown](doc/md_docs.md)
-- [ReStructured Text](doc/rest_docs.md)
-- [Man Page](doc/man_docs.md)
+Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).
 
 ## Generating bash completions
 
diff --git a/bash_completions.go b/bash_completions.go
index 1e27188..d8341cc 100644
--- a/bash_completions.go
+++ b/bash_completions.go
@@ -181,10 +181,9 @@ __%[1]s_handle_reply()
     local completions
     completions=("${commands[@]}")
     if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
-        completions=("${must_have_one_noun[@]}")
+        completions+=("${must_have_one_noun[@]}")
     elif [[ -n "${has_completion_function}" ]]; then
         # if a go completion function is provided, defer to that function
-        completions=()
         __%[1]s_handle_go_custom_completion
     fi
     if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
diff --git a/cobra/README.md b/cobra/README.md
index c360fda..0f718c2 100644
--- a/cobra/README.md
+++ b/cobra/README.md
@@ -88,12 +88,17 @@ author: Steve Francia <spf@spf13.com>
 license: MIT
 ```
 
+You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
+**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.
+
 You can specify no license by setting `license` to `none` or you can specify
 a custom license:
 
 ```yaml
+author: Steve Francia <spf@spf13.com>
+year: 2020
 license:
-  header: This file is part of {{ .appName }}.
+  header: This file is part of CLI application foo.
   text: |
     {{ .copyright }}
 
@@ -102,5 +107,23 @@ license:
     master my life.
 ```
 
-You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
-**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.
+In the above custom license configuration the `copyright` line in the License
+text is generated from the `author` and `year` properties. The content of the
+`LICENSE` file is
+
+```
+Copyright © 2020 Steve Francia <spf@spf13.com>
+
+This is my license. There are many like it, but this one is mine.
+My license is my best friend. It is my life. I must master it as I must
+master my life.
+```
+
+The `header` property is used as the license header files. No interpolation is
+done. This is the example of the go file header.
+```
+/*
+Copyright © 2020 Steve Francia <spf@spf13.com>
+This file is part of CLI application foo.
+*/
+```
diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go
index edddc2c..504a478 100644
--- a/cobra/cmd/init.go
+++ b/cobra/cmd/init.go
@@ -32,42 +32,17 @@ var (
 		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 be created in the current directory;
+  * If a name is provided, a directory with that name 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 existing directory with contents.`,
+		Run: func(_ *cobra.Command, args []string) {
 
-		Run: func(cmd *cobra.Command, args []string) {
-
-			wd, err := os.Getwd()
+			projectPath, err := initializeProject(args)
 			if err != nil {
 				er(err)
 			}
-
-			if len(args) > 0 {
-				if args[0] != "." {
-					wd = fmt.Sprintf("%s/%s", wd, args[0])
-				}
-			}
-
-			project := &Project{
-				AbsolutePath: wd,
-				PkgName:      pkgName,
-				Legal:        getLicense(),
-				Copyright:    copyrightLine(),
-				Viper:        viper.GetBool("useViper"),
-				AppName:      path.Base(pkgName),
-			}
-
-			if err := project.Create(); err != nil {
-				er(err)
-			}
-
-			fmt.Printf("Your Cobra application is ready at\n%s\n", project.AbsolutePath)
+			fmt.Printf("Your Cobra application is ready at\n%s\n", projectPath)
 		},
 	}
 )
@@ -76,3 +51,31 @@ func init() {
 	initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
 	initCmd.MarkFlagRequired("pkg-name")
 }
+
+func initializeProject(args []string) (string, error) {
+	wd, err := os.Getwd()
+	if err != nil {
+		return "", err
+	}
+
+	if len(args) > 0 {
+		if args[0] != "." {
+			wd = fmt.Sprintf("%s/%s", wd, args[0])
+		}
+	}
+
+	project := &Project{
+		AbsolutePath: wd,
+		PkgName:      pkgName,
+		Legal:        getLicense(),
+		Copyright:    copyrightLine(),
+		Viper:        viper.GetBool("useViper"),
+		AppName:      path.Base(pkgName),
+	}
+
+	if err := project.Create(); err != nil {
+		return "", err
+	}
+
+	return project.AbsolutePath, nil
+}
diff --git a/doc/README.md b/doc/README.md
new file mode 100644
index 0000000..6ea4eb6
--- /dev/null
+++ b/doc/README.md
@@ -0,0 +1,12 @@
+# Documentation generation
+
+- [Man page docs](./man_docs.md)
+- [Markdown docs](./md_docs.md)
+- [Rest docs](./rest_docs.md)
+- [Yaml docs](./yaml_docs.md)
+
+## Options
+### `DisableAutoGenTag`
+You may set `cmd.DisableAutoGenTag = true`
+to _entirely_ remove the auto generated string "Auto generated by spf13/cobra..."
+from any documentation source.
diff --git a/doc/man_docs.go b/doc/man_docs.go
index 8c7fba4..b29a677 100644
--- a/doc/man_docs.go
+++ b/doc/man_docs.go
@@ -105,7 +105,7 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
 	if header == nil {
 		header = &GenManHeader{}
 	}
-	if err := fillHeader(header, cmd.CommandPath()); err != nil {
+	if err := fillHeader(header, cmd.CommandPath(), cmd.DisableAutoGenTag); err != nil {
 		return err
 	}
 
@@ -114,7 +114,7 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
 	return err
 }
 
-func fillHeader(header *GenManHeader, name string) error {
+func fillHeader(header *GenManHeader, name string, disableAutoGen bool) error {
 	if header.Title == "" {
 		header.Title = strings.ToUpper(strings.Replace(name, " ", "\\-", -1))
 	}
@@ -133,7 +133,7 @@ func fillHeader(header *GenManHeader, name string) error {
 		header.Date = &now
 	}
 	header.date = (*header.Date).Format("Jan 2006")
-	if header.Source == "" {
+	if header.Source == "" && !disableAutoGen {
 		header.Source = "Auto generated by spf13/cobra"
 	}
 	return nil
diff --git a/projects_using_cobra.md b/projects_using_cobra.md
index 58dd639..aa79107 100644
--- a/projects_using_cobra.md
+++ b/projects_using_cobra.md
@@ -8,6 +8,7 @@
 - [Giant Swarm's gsctl](https://github.com/giantswarm/gsctl)
 - [Github CLI](https://github.com/cli/cli)
 - [GopherJS](http://www.gopherjs.org/)
+- [Helm](https://helm.sh)
 - [Hugo](https://gohugo.io)
 - [Istio](https://istio.io)
 - [Kubernetes](http://kubernetes.io/)


Test patch:
diff --git a/cobra/cmd/init_test.go b/cobra/cmd/init_test.go
index 8ee3910..c4b3f09 100644
--- a/cobra/cmd/init_test.go
+++ b/cobra/cmd/init_test.go
@@ -2,9 +2,12 @@ package cmd
 
 import (
 	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"testing"
+
+	"github.com/spf13/viper"
 )
 
 func getProject() *Project {
@@ -20,20 +23,72 @@ func getProject() *Project {
 }
 
 func TestGoldenInitCmd(t *testing.T) {
-	project := getProject()
-	defer os.RemoveAll(project.AbsolutePath)
 
-	if err := project.Create(); err != nil {
+	dir, err := ioutil.TempDir("", "cobra-init")
+	if err != nil {
 		t.Fatal(err)
 	}
+	defer os.RemoveAll(dir)
+
+	tests := []struct {
+		name      string
+		args      []string
+		pkgName   string
+		expectErr bool
+	}{
+		{
+			name:      "successfully creates a project with name",
+			args:      []string{"testproject"},
+			pkgName:   "github.com/spf13/testproject",
+			expectErr: false,
+		},
+		{
+			name:      "returns error when passing an absolute path for project",
+			args:      []string{dir},
+			pkgName:   "github.com/spf13/testproject",
+			expectErr: true,
+		},
+		{
+			name:      "returns error when passing an relative path for project",
+			args:      []string{"github.com/spf13/testproject"},
+			pkgName:   "github.com/spf13/testproject",
+			expectErr: true,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+
+			initCmd.Flags().Set("pkg-name", tt.pkgName)
+			viper.Set("useViper", true)
+			projectPath, err := initializeProject(tt.args)
+			defer func() {
+				if projectPath != "" {
+					os.RemoveAll(projectPath)
+				}
+			}()
+
+			if !tt.expectErr && err != nil {
+				t.Fatalf("did not expect an error, got %s", err)
+			}
+			if tt.expectErr {
+				if err == nil {
+					t.Fatal("expected an error but got none")
+				} else {
+					// got an expected error nothing more to do
+					return
+				}
+			}
 
-	expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
-	for _, f := range expectedFiles {
-		generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
-		goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
-		err := compareFiles(generatedFile, goldenFile)
-		if err != nil {
-			t.Fatal(err)
-		}
+			expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
+			for _, f := range expectedFiles {
+				generatedFile := fmt.Sprintf("%s/%s", projectPath, f)
+				goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
+				err := compareFiles(generatedFile, goldenFile)
+				if err != nil {
+					t.Fatal(err)
+				}
+			}
+		})
 	}
 }
diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go
index 2c400f5..ee9b875 100644
--- a/doc/man_docs_test.go
+++ b/doc/man_docs_test.go
@@ -101,6 +101,8 @@ func TestGenManNoGenTag(t *testing.T) {
 
 	unexpected := translate("#HISTORY")
 	checkStringOmits(t, output, unexpected)
+	unexpected = translate("Auto generated by spf13/cobra")
+	checkStringOmits(t, output, unexpected)
 }
 
 func TestGenManSeeAlso(t *testing.T) {