Skip to content

Commit

Permalink
Merge pull request #17 from hashmap-kz/feature/lint
Browse files Browse the repository at this point in the history
feature/lint
  • Loading branch information
hashmap-kz authored Jan 6, 2025
2 parents b9d1c01 + 95ed974 commit 5676aeb
Show file tree
Hide file tree
Showing 26 changed files with 216 additions and 135 deletions.
121 changes: 121 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
run:
concurrency: 6
timeout: 10m
issues:
exclude-dirs:
- integration/*

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0
linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- durationcheck
- errcheck
- goconst
- gocritic
- gocyclo
- godox
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- makezero
- misspell
- nakedret
- nolintlint
- prealloc
- predeclared
- promlinter
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

linters-settings:
godox:
keywords:
- BUG
- FIXME
- HACK
errcheck:
check-type-assertions: true
check-blank: true
gocritic:
disabled-checks:
- ifElseChain
enabled-checks:
# Diagnostic
- commentedOutCode
- nilValReturn
- sloppyReassign
- weakCond
- octalLiteral

# Performance
- appendCombine
- equalFold
- hugeParam
- indexAlloc
- rangeExprCopy
- rangeValCopy

# Style
- boolExprSimplify
- commentedOutImport
- docStub
- emptyFallthrough
- emptyStringTest
- hexLiteral
- methodExprCall
- stringXbytes
- typeAssertChain
- unlabelStmt
- yodaStyleExpr
# - ifElseChain

# Opinionated
- builtinShadow
- importShadow
- initClause
- nestingReduce
- paramTypeCombine
- ptrToRefParam
- typeUnparen
- unnamedResult
- unnecessaryBlock
nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
allow-unused: false
# Disable to ensure that nolint directives don't have a leading space. Default is true.
# TODO(lint): Enforce machine-readable `nolint` directives
allow-leading-space: true
# Exclude following linters from requiring an explanation. Default is [].
allow-no-explanation: []
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
# TODO(lint): Enforce explanations for `nolint` directives
require-explanation: false
# Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
require-specific: true
20 changes: 9 additions & 11 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package app

import (
"fmt"
"github.com/hashmap-kz/kubectl-envsubst/pkg/cmd"
"io"
"os"
"os/exec"
"strings"

"github.com/hashmap-kz/kubectl-envsubst/pkg/cmd"
)

// runApp executes the plugin, with logic divided into smaller, testable components
func RunApp() error {

// parse all passed cmd arguments without any modification
flags, err := cmd.ParseArgs()
if err != nil {
Expand Down Expand Up @@ -44,15 +44,15 @@ func RunApp() error {

// apply STDIN (if any)
if flags.HasStdin {
err := applyStdin(flags, kubectl)
err := applyStdin(&flags, kubectl)
if err != nil {
return err
}
}

// apply passed files
for _, filename := range files {
err := applyOneFile(flags, kubectl, filename)
err := applyOneFile(&flags, kubectl, filename)
if err != nil {
return err
}
Expand All @@ -62,7 +62,7 @@ func RunApp() error {
}

// applyStdin substitutes content, passed to stdin `kubectl apply -f -`
func applyStdin(flags cmd.CmdArgsRawRecognized, kubectl string) error {
func applyStdin(flags *cmd.ArgsRawRecognized, kubectl string) error {
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
return err
Expand All @@ -78,8 +78,7 @@ func applyStdin(flags cmd.CmdArgsRawRecognized, kubectl string) error {
}

// applyOneFile read file (url, local-path), substitute its content, apply result
func applyOneFile(flags cmd.CmdArgsRawRecognized, kubectl string, filename string) error {

func applyOneFile(flags *cmd.ArgsRawRecognized, kubectl, filename string) error {
// recognize file type

var contentForSubst []byte
Expand Down Expand Up @@ -107,7 +106,7 @@ func applyOneFile(flags cmd.CmdArgsRawRecognized, kubectl string, filename strin
}

// substituteContent runs the subst module for a given content
func substituteContent(flags cmd.CmdArgsRawRecognized, contentForSubst []byte) (string, error) {
func substituteContent(flags *cmd.ArgsRawRecognized, contentForSubst []byte) (string, error) {
envSubst := cmd.NewEnvsubst(flags.EnvsubstAllowedVars, flags.EnvsubstAllowedPrefix, true)
substitutedBuffer, err := envSubst.SubstituteEnvs(string(contentForSubst))
if err != nil {
Expand All @@ -117,12 +116,11 @@ func substituteContent(flags cmd.CmdArgsRawRecognized, contentForSubst []byte) (
}

// execKubectl applies a result buffer, bu running `kubectl apply -f -`
func execKubectl(flags cmd.CmdArgsRawRecognized, kubectl string, substitutedBuffer string) error {
func execKubectl(flags *cmd.ArgsRawRecognized, kubectl, substitutedBuffer string) error {
// prepare kubectl args
args := []string{}
args = append(args, flags.Others...)
args = append(args, "-f")
args = append(args, "-")
args = append(args, "-f", "-")

// pass stream of files to stdin
execCmd, err := cmd.ExecWithStdin(kubectl, []byte(substitutedBuffer), args...)
Expand Down
5 changes: 4 additions & 1 deletion cmd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ data:

// Flush stdout and reset pointer for reading
os.Stdout.Sync()
_, _ = stdoutFile.Seek(0, io.SeekStart)
_, err = stdoutFile.Seek(0, io.SeekStart)
if err != nil {
t.Fatalf("Failed to seek 0. Stdout file.")
}

// Read and validate stdout content
output, err := io.ReadAll(stdoutFile)
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-envsubst.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"fmt"
"github.com/hashmap-kz/kubectl-envsubst/cmd/app"
"os"

"github.com/hashmap-kz/kubectl-envsubst/cmd/app"
)

func main() {

err := app.RunApp()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s", err.Error())
Expand Down
2 changes: 0 additions & 2 deletions integration/apply_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestEnvsubstIntegration_SubstApplyFromFile(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -56,5 +55,4 @@ func TestEnvsubstIntegration_SubstApplyFromFile(t *testing.T) {
if !strings.Contains(string(validateOutput), resourceName) {
t.Errorf("Expected resource %s to exist, got %s", resourceName, string(validateOutput))
}

}
1 change: 0 additions & 1 deletion integration/apply_mixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestEnvsubstIntegration_SubstApplyConfigmapMixed(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down
2 changes: 0 additions & 2 deletions integration/apply_stdin_large_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ data:
`

func TestEnvsubstIntegration_SubstApplyFromStdinWithLargeContent(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -79,5 +78,4 @@ func TestEnvsubstIntegration_SubstApplyFromStdinWithLargeContent(t *testing.T) {
t.Errorf("Expected substituted output to contain '%s', got %s", er, stringOutput)
}
}

}
2 changes: 0 additions & 2 deletions integration/apply_stdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestEnvsubstIntegration_SubstApplyFromStdin(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -81,5 +80,4 @@ spec:
if !strings.Contains(string(validateOutput), resourceName) {
t.Errorf("Expected resource %s to exist, got %s", resourceName, string(validateOutput))
}

}
2 changes: 0 additions & 2 deletions integration/apply_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestEnvsubstIntegration_SubstApplyFromUrl(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -57,5 +56,4 @@ func TestEnvsubstIntegration_SubstApplyFromUrl(t *testing.T) {
if !strings.Contains(string(validateOutput), resourceName) {
t.Errorf("Expected resource %s to exist, got %s", resourceName, string(validateOutput))
}

}
2 changes: 0 additions & 2 deletions integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func printEnvsubstVersionInfo(t *testing.T) {
}
}
}

}

func getDeploymentImageName(t *testing.T, deploymentName string) string {
Expand All @@ -100,7 +99,6 @@ func getDeploymentImageName(t *testing.T, deploymentName string) string {
}

func createTempFile(t *testing.T, content string, extension string) (string, error) {

tempFile, err := os.CreateTemp("", "kubectl-envsubst-tmp-*."+extension)
if err != nil {
return "", fmt.Errorf("failed to create temp file: %w", err)
Expand Down
2 changes: 0 additions & 2 deletions integration/plain_combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestEnvsubstIntegration_NoSubst_MixedManifests(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -49,5 +48,4 @@ func TestEnvsubstIntegration_NoSubst_MixedManifests(t *testing.T) {
t.Errorf("Expected substituted output to contain '%s', got %s", er, stringOutput)
}
}

}
4 changes: 0 additions & 4 deletions integration/plain_glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestEnvsubstIntegration_NoSubst_GlobPatterns_Yaml(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -54,11 +53,9 @@ func TestEnvsubstIntegration_NoSubst_GlobPatterns_Yaml(t *testing.T) {
t.Errorf("Expected substituted output does not contain '%s'", er)
}
}

}

func TestEnvsubstIntegration_NoSubst_GlobPatterns_Json(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -104,5 +101,4 @@ func TestEnvsubstIntegration_NoSubst_GlobPatterns_Json(t *testing.T) {
t.Errorf("Expected substituted output does not contain '%s'", er)
}
}

}
2 changes: 0 additions & 2 deletions integration/plain_recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestEnvsubstIntegration_NoSubst_Recursive(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -54,5 +53,4 @@ func TestEnvsubstIntegration_NoSubst_Recursive(t *testing.T) {
t.Errorf("Expected substituted output to contain '%s', got %s", er, stringOutput)
}
}

}
2 changes: 0 additions & 2 deletions integration/plain_yaml_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestEnvsubstIntegration_NoSubst_MixedManifests_MixedFileFormats(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -49,5 +48,4 @@ func TestEnvsubstIntegration_NoSubst_MixedManifests_MixedFileFormats(t *testing.
t.Errorf("Expected substituted output to contain '%s', got %s", er, stringOutput)
}
}

}
2 changes: 0 additions & 2 deletions integration/subst_combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestEnvsubstIntegration_SubstMixedManifestsCombined(t *testing.T) {

if os.Getenv(integrationTestEnv) != integrationTestFlag {
t.Log("integration test was skipped due to configuration")
return
Expand Down Expand Up @@ -63,5 +62,4 @@ func TestEnvsubstIntegration_SubstMixedManifestsCombined(t *testing.T) {
t.Errorf("Expected substituted output to contain '%s', got %s", er, stringOutput)
}
}

}
Loading

0 comments on commit 5676aeb

Please sign in to comment.