Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.4
version: v2.7
25 changes: 21 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
version: "2"

linters:
default: none
enable:
- bodyclose
- depguard
# - depguard # TODO: Re-enable later
- dogsled
- dupl
- errcheck
- exhaustive
- funlen
- gochecknoinits
- goconst
- gocritic
Expand All @@ -17,9 +17,9 @@ linters:
- gosec
- govet
- ineffassign
- lll
# - lll # TODO: Re-enable later
- misspell
- mnd
# - mnd # TODO: Re-enable later
- nakedret
- noctx
- nolintlint
Expand All @@ -40,6 +40,14 @@ linters:
- opinionated
- performance
- style
disabled-checks:
- paramTypeCombine # I'm not sure it makes code more readable
- hugeParam # TODO: Re-consider this later
staticcheck:
checks:
- all
- "-ST1003" # TODO: Deal with variable names later
- "-ST1005" # TODO: Re-think error messages policy later
exclusions:
generated: lax
presets:
Expand All @@ -51,6 +59,12 @@ linters:
- third_party$
- builtin$
- examples$
rules:
- path: "(.+)_test\\.go"
linters:
- goconst
- mnd

formatters:
enable:
- gofmt
Expand All @@ -61,3 +75,6 @@ formatters:
- third_party$
- builtin$
- examples$

issues:
max-issues-per-linter: 0
7 changes: 3 additions & 4 deletions acceptance/new/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package new_test

import (
"encoding/json"
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"

"github.com/jay7x/pct/acceptance/testutils"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

const APP = "pct"

var templatePath string

func TestMain(m *testing.M) {
log.Logger = zerolog.New(ioutil.Discard).With().Timestamp().Logger()
log.Logger = zerolog.New(io.Discard).With().Timestamp().Logger()

templatePath, _ = filepath.Abs("../../internal/pkg/pct/testdata/examples")

Expand Down
13 changes: 10 additions & 3 deletions acceptance/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package testutils

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"

"github.com/rs/zerolog/log"
)
Expand All @@ -27,20 +29,25 @@ func SkipAcceptanceTest(t *testing.T) {
// Run Command takes a command to execute and the directory in which to execute the command.
// if wd is and empty string it will default to the current working directory
func RunCommand(cmdString string, wd string) (stdout string, stderr string, exitCode int) {
cmds := strings.Split(cmdString, " ")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

cmds := strings.Split(cmdString, " ")
cmds = toolArgsAsSingleArg(cmds) // Remove when GH-52 is resolved

cmd := exec.Command(cmds[0], cmds[1:]...) // #nosec // used only for testing
cmd := exec.CommandContext(ctx, cmds[0], cmds[1:]...) // #nosec // used only for testing
if wd != "" {
cmd.Dir = wd
}
out, err := cmd.CombinedOutput()
exitCode = 0

if err != nil {
if ctx.Err() == context.DeadlineExceeded {
panic("Command timed out")
}
stderr = err.Error()
// todo: double check that error statuss work on Windows
// todo: double check that error status work on Windows
if msg, ok := err.(*exec.ExitError); ok { // there is error code
exitCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
}
Expand Down
1 change: 0 additions & 1 deletion cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (bc *BuildCommand) CreateCommand() *cobra.Command {
}

func (bc *BuildCommand) preExecute(cmd *cobra.Command, args []string) error {

wd, err := os.Getwd()
log.Trace().Msgf("WD: %v", wd)

Expand Down
7 changes: 3 additions & 4 deletions cmd/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package build_test

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/jay7x/pct/cmd/build"
"github.com/jay7x/pct/pkg/mock"
"github.com/stretchr/testify/assert"
)

func TestCreateBuildCommand(t *testing.T) {
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestCreateBuildCommand(t *testing.T) {
t.Errorf("Unexpected error when none wanted: %v", err)
return
} else {
out, _ := ioutil.ReadAll(b)
out, _ := io.ReadAll(b)
assert.Regexp(t, tt.expectedErrorMatch, string(out))
}
} else if tt.expectedErrorMatch != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ PowerShell:
PS> pct completion powershell > pct.ps1
# and source this file from your PowerShell profile.`,
ValidArgs: []string{"bash", "fish", "pwsh", "zsh"},
Args: cobra.ExactValidArgs(1),
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
var err error
switch args[0] {
Expand Down
8 changes: 4 additions & 4 deletions cmd/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ func flagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]str
}

func execute(cmd *cobra.Command, args []string) error {
docs := docsApi.ParsedDocsCache
myDocs := docsApi.ParsedDocsCache
if listTopics {
if format == "" {
format = "table"
}
if category != "" {
docs = docsApi.FilterByCategory(category, docs)
myDocs = docsApi.FilterByCategory(category, myDocs)
}
if tag != "" {
docs = docsApi.FilterByTag(tag, docs)
myDocs = docsApi.FilterByTag(tag, myDocs)
}
// If there's only one match, should we render the matching doc?
docsApi.FormatFrontMatter(format, docs)
docsApi.FormatFrontMatter(format, myDocs)
} else if topic != "" {
doc, err := docsApi.SelectDocument(topic, docsApi.ParsedDocsCache)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package install_test

import (
"bytes"
"io/ioutil"
"io"
"testing"

"github.com/spf13/afero"

"github.com/jay7x/pct/cmd/install"
"github.com/jay7x/pct/pkg/mock"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -85,7 +84,8 @@ func TestCreateinstallCommand(t *testing.T) {
installCmd := cmd.CreateCommand()

b := bytes.NewBufferString("")
installCmd.SetOutput(b)
installCmd.SetOut(b)
installCmd.SetErr(b)

installCmd.SetArgs(tt.args)
err := installCmd.Execute()
Expand All @@ -96,10 +96,9 @@ func TestCreateinstallCommand(t *testing.T) {
}

if (err != nil) && tt.expectError {
out, _ := ioutil.ReadAll(b)
out, _ := io.ReadAll(b)
assert.Contains(t, string(out), tt.expectedOutput)
}

})
}
}
4 changes: 2 additions & 2 deletions cmd/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ func flagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]str
}
localTemplatePath = viper.GetString("templatepath")

return completeName(localTemplatePath, toComplete), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
return completeName(toComplete), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
}

func completeName(cache string, match string) []string {
func completeName(match string) []string {
var names []string
for _, tmpl := range cachedTemplates {
namespacedTemplate := fmt.Sprintf("%s/%s", tmpl.Author, tmpl.Id)
Expand Down
4 changes: 2 additions & 2 deletions cmd/new/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package new

import (
"bytes"
"io/ioutil"
"io"
"regexp"
"testing"

Expand Down Expand Up @@ -59,7 +59,7 @@ func TestCreateCommand(t *testing.T) {
return
}

out, err := ioutil.ReadAll(b)
out, err := io.ReadAll(b)
if err != nil {
t.Errorf("Failed to read stdout: %v", err)
return
Expand Down
4 changes: 1 addition & 3 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var (
LocalTemplateCache string

debug bool
// format string
)

func InitLogger() {
Expand Down Expand Up @@ -64,7 +63,6 @@ func CreateRootCommand() *cobra.Command {
cobra.CheckErr(err)

tmp.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug output")
// tmp.PersistentFlags().StringVarP(&format, "format", "f", "junit", "formating (default is junit)")

return tmp
}
Expand All @@ -91,7 +89,7 @@ func InitConfig() {
// and also the fully formatted command as passed with arguments/flags.
// Idea borrowed from carolynvs/porter:
// https://github.com/carolynvs/porter/blob/ccca10a63627e328616c1006600153da8411a438/cmd/porter/main.go
func GetCalledCommand(cmd *cobra.Command) (string, string) {
func GetCalledCommand(cmd *cobra.Command) (calledCmd string, calledArgs string) {
if len(os.Args) < 2 {
return "", ""
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.11.1
golang.org/x/text v0.28.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -67,7 +68,6 @@ require (
golang.org/x/net v0.43.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/term v0.34.0 // indirect
golang.org/x/text v0.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading