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

remove the use of depreceated pacakge ioutil and comply with gopls linter #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions cmd/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"time"
)

func init() {
Expand All @@ -25,29 +26,33 @@ func ensureLF(content []byte) []byte {
// If contents are equal, it returns nil.
// If not, it returns which files are not equal
// and diff (if system has diff command) between these files.
func compareFiles(pathA, pathB string) error {
contentA, err := ioutil.ReadFile(pathA)
func compareFiles(generated, golden string) error {
contentA, err := os.ReadFile(generated)
if err != nil {
return err
}
contentB, err := ioutil.ReadFile(pathB)
contentB, err := os.ReadFile(golden)
if err != nil {
return err
}

// change year to current year
contentB = bytes.ReplaceAll(contentB, []byte("2022"), []byte(time.Now().Format("2006")))

if !bytes.Equal(ensureLF(contentA), ensureLF(contentB)) {
output := new(bytes.Buffer)
output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", pathA, pathB))
output.WriteString(fmt.Sprintf("%q and %q are not equal!\n\n", generated, golden))

diffPath, err := exec.LookPath("diff")
if err != nil {
// Don't execute diff if it can't be found.
return nil
}
diffCmd := exec.Command(diffPath, "-u", "--strip-trailing-cr", pathA, pathB)
diffCmd := exec.Command(diffPath, "-u", "--strip-trailing-cr", generated, golden)
diffCmd.Stdout = output
diffCmd.Stderr = output

output.WriteString("$ diff -u " + pathA + " " + pathB + "\n")
output.WriteString("$ diff -u " + generated + " " + golden + "\n")
if err := diffCmd.Run(); err != nil {
output.WriteString("\n" + err.Error())
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -23,11 +22,7 @@ func getProject() *Project {
}

func TestGoldenInitCmd(t *testing.T) {

dir, err := ioutil.TempDir("", "cobra-init")
if err != nil {
t.Fatal(err)
}
dir := filepath.Join(os.TempDir(), "cobra-init")
defer os.RemoveAll(dir)

tests := []struct {
Expand All @@ -46,7 +41,6 @@ func TestGoldenInitCmd(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

viper.Set("useViper", true)
viper.Set("license", "apache")
projectPath, err := initializeProject(tt.args)
Expand Down
10 changes: 6 additions & 4 deletions cmd/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package cmd

import (
"fmt"
"errors"
"strings"
"time"

Expand Down Expand Up @@ -64,8 +64,10 @@ func getLicense() License {

// If user wants to have custom license, use that.
if viper.IsSet("license.header") || viper.IsSet("license.text") {
return License{Header: viper.GetString("license.header"),
Text: viper.GetString("license.text")}
return License{
Header: viper.GetString("license.header"),
Text: viper.GetString("license.text"),
}
}

// If user wants to have built-in license, use that.
Expand Down Expand Up @@ -94,7 +96,7 @@ func copyrightLine() string {
func findLicense(name string) License {
found := matchLicense(name)
if found == "" {
cobra.CheckErr(fmt.Errorf("unknown license: " + name))
cobra.CheckErr(errors.New("unknown license: " + name))
}
return Licenses[found]
}
Expand Down