mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Merge remote-tracking branch 'upstream/master' into disable-update
This commit is contained in:
commit
4979e99599
3 changed files with 125 additions and 44 deletions
|
@ -88,12 +88,17 @@ author: Steve Francia <spf@spf13.com>
|
||||||
license: MIT
|
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
|
You can specify no license by setting `license` to `none` or you can specify
|
||||||
a custom license:
|
a custom license:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
author: Steve Francia <spf@spf13.com>
|
||||||
|
year: 2020
|
||||||
license:
|
license:
|
||||||
header: This file is part of {{ .appName }}.
|
header: This file is part of CLI application foo.
|
||||||
text: |
|
text: |
|
||||||
{{ .copyright }}
|
{{ .copyright }}
|
||||||
|
|
||||||
|
@ -102,5 +107,23 @@ license:
|
||||||
master my life.
|
master my life.
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
|
In the above custom license configuration the `copyright` line in the License
|
||||||
**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.
|
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.
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
|
@ -32,42 +32,17 @@ var (
|
||||||
Long: `Initialize (cobra init) will create a new application, with a license
|
Long: `Initialize (cobra init) will create a new application, with a license
|
||||||
and the appropriate structure for a Cobra-based CLI application.
|
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 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) {
|
projectPath, err := initializeProject(args)
|
||||||
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Your Cobra application is ready at\n%s\n", projectPath)
|
||||||
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)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -76,3 +51,31 @@ func init() {
|
||||||
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
|
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
|
||||||
initCmd.MarkFlagRequired("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
|
||||||
|
}
|
||||||
|
|
|
@ -2,9 +2,12 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getProject() *Project {
|
func getProject() *Project {
|
||||||
|
@ -20,20 +23,72 @@ func getProject() *Project {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGoldenInitCmd(t *testing.T) {
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
|
tests := []struct {
|
||||||
for _, f := range expectedFiles {
|
name string
|
||||||
generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
|
args []string
|
||||||
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
|
pkgName string
|
||||||
err := compareFiles(generatedFile, goldenFile)
|
expectErr bool
|
||||||
if err != nil {
|
}{
|
||||||
t.Fatal(err)
|
{
|
||||||
}
|
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", projectPath, f)
|
||||||
|
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
|
||||||
|
err := compareFiles(generatedFile, goldenFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue