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
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
with:
go-version: 1.22
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v9
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.56.2
version: v2.14.0
111 changes: 74 additions & 37 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
# This file contains all available configuration options
# with their default values.
# For complete .golangci.yml configuration, reference: https://golangci-lint.run/usage/configuration/#config-file
# golangci-lint v2 configuration.
# For the full reference, see: https://golangci-lint.run/usage/configuration/
version: "2"

# options for analysis running
run:
timeout: 10m

linters:
disable-all: true
default: none
enable: # please keep this alphabetized
# Don't use soon to deprecated[1] linters that lead to false
# https://github.com/golangci/golangci-lint/issues/1841
# - deadcode
# - structcheck
# - varcheck
- ineffassign
- staticcheck
- unused
- gosimple
- govet
- gofumpt
# gosimple and stylecheck are part of staticcheck since golangci-lint v2.
# exportloopref was removed: Go 1.22 loop variable semantics make it obsolete.
# typecheck is always on and can no longer be listed.
- bodyclose
# - depguard
- dogsled
- dupl
- exportloopref
- gocritic
# - misspell
- govet
- ineffassign
# - misspell
- nolintlint
- prealloc
- predeclared
- stylecheck
- staticcheck
- tparallel
- typecheck
- unconvert
- unused
- whitespace
# - depguard
# - wsl
# - revive
# - unparam
Expand All @@ -44,20 +36,65 @@ linters:
# - thelper
# - goconst
# - errcheck
settings:
govet:
disable:
# New in recent Go releases; not enabled under golangci-lint v1.
- inline
staticcheck:
# Default checks, minus the QF* quick fixes that staticcheck gained when
# gosimple and stylecheck were merged into it in golangci-lint v2.
checks:
- all
- -ST1000
- -ST1003
- -ST1016
- -ST1020
- -ST1021
- -ST1022
- -QF*
exclusions:
generated: lax
# Keep the exclusions and skipped directories that golangci-lint v1
# applied by default.
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
# Newer prealloc versions also flag slices that start from a literal.
# Allocation hints are not worth the churn in table-driven tests.
- path: _test\.go$
linters:
- prealloc
- path: (.+)\.go$
text: "G306: Expect WriteFile permissions to be 0600 or less"
- path: (.+)\.go$
text: "ST1018: string literal contains Unicode control characters, consider using escape sequences instead"
- path: (.+)\.go$
text: "ifElseChain: rewrite if-else to switch statement"
- path: (.+)\.go$
text: "S1000: should use for range instead of for { select {} }"
- path: (.+)\.go$
text: "SA4004: the surrounding loop is unconditionally terminated"
- path: (.+)\.go$
text: "copylocks: call of c\\.Post copies lock value: kcl-lang\\.io/kcl-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex"
paths:
- (^|/)third_party($|/)
- (^|/)builtin($|/)
- (^|/)examples($|/)

linters-settings:
gofumpt:
# Select the Go version to target. The default is `1.15`.
lang-version: "1.22"
# Choose whether or not to use the extra rules that are disabled
# by default
extra-rules: false

issues:
exclude:
- "G306: Expect WriteFile permissions to be 0600 or less"
- "ST1018: string literal contains Unicode control characters, consider using escape sequences instead"
- "ifElseChain: rewrite if-else to switch statement"
- "S1000: should use for range instead of for { select {} }"
- "SA4004: the surrounding loop is unconditionally terminated"
- "copylocks: call of c\\.Post copies lock value: kcl-lang\\.io/kcl-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex"
formatters:
enable:
- gofumpt
settings:
gofumpt:
# The target Go version is read from go.mod.
extra-rules: false
exclusions:
generated: lax
paths:
- (^|/)third_party($|/)
- (^|/)builtin($|/)
- (^|/)examples($|/)
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN_IN_DOCKER+=-w /root/kusion ${KCLVM_BUILDER}
GOFORMATER ?= gofumpt
GOFORMATER_VERSION ?= v0.2.0
GOLINTER ?= golangci-lint
GOLINTER_VERSION ?= v1.56.2
GOLINTER_VERSION ?= v2.14.0
COVER_FILE ?= coverage.out
SOURCE_PATHS ?= ./pkg/...

Expand Down Expand Up @@ -62,12 +62,20 @@ format: ## Format source code
@which $(GOFORMATER) > /dev/null || (echo "Installing $(GOFORMATER)@$(GOFORMATER_VERSION) ..."; go install mvdan.cc/gofumpt@$(GOFORMATER_VERSION) && echo -e "Installation complete!\n")
@for path in $(SOURCE_PATHS); do ${GOFORMATER} -l -w -e `echo $${path} | cut -b 3- | rev | cut -b 5- | rev`; done;

lint: ## Lint, will not fix but sets exit code on error
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
# Install golangci-lint if it is missing or not at GOLINTER_VERSION, so an older
# local install (for example v1, which cannot read the v2 config) is not used.
lint-install:
@if ! $(GOLINTER) --version 2>/dev/null | grep -q "version $(GOLINTER_VERSION:v%=%) "; then \
echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; \
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n"; \
fi
@$(GOLINTER) --version 2>/dev/null | grep -q "version $(GOLINTER_VERSION:v%=%) " || \
(echo -e "$(CCRED)$$(which $(GOLINTER)) is not $(GOLINTER_VERSION). Remove it or put $$(go env GOPATH)/bin first in PATH.$(CCEND)"; exit 1)

lint: lint-install ## Lint, will not fix but sets exit code on error
$(GOLINTER) run --timeout=10m $(SOURCE_PATHS)

lint-fix: ## Lint, will try to fix errors and modify code
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
lint-fix: lint-install ## Lint, will try to fix errors and modify code
$(GOLINTER) run --timeout=10m $(SOURCE_PATHS) --fix

doc: ## Start the documentation server with godoc
Expand Down Expand Up @@ -211,4 +219,4 @@ gen-api-doc: ## Generate API Documentation by API Specification
@which swagger > /dev/null || (echo "Installing swagger@v0.30.5 ..."; go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5 && echo "Installation complete!\n")
-swagger generate markdown -f ./api/openapispec/swagger.json --output=docs/api.md && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)

.PHONY: test cover cover-html format lint lint-fix doc build-changelog upload clean build-all build-image build-local-linux build-local-windows build-local-linux-all build-local-windows-all e2e-test gen-api-spec gen-api-doc build-portal show-portal-status
.PHONY: test cover cover-html format lint-install lint lint-fix doc build-changelog upload clean build-all build-image build-local-linux build-local-windows build-local-linux-all build-local-windows-all e2e-test gen-api-spec gen-api-doc build-portal show-portal-status
2 changes: 1 addition & 1 deletion pkg/cmd/project/list/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (o *Options) Validate(cmd *cobra.Command, args []string) error {

// Run executes the `list` command.
func (o *Options) Run() error {
if o.Workspace == nil || len(o.Workspace) == 0 {
if len(o.Workspace) == 0 {
return fmt.Errorf("workspace is empty")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/resource/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (o *ShowOptions) Run() (err error) {
return fmt.Errorf("no release found for project: %s, workspace: %s", *o.Project, *o.Workspace)
}

if rel.Spec.Resources == nil || len(rel.Spec.Resources) == 0 {
if len(rel.Spec.Resources) == 0 {
return fmt.Errorf("no resources found for project: %s, workspace: %s", *o.Project, *o.Workspace)
}
resourceMap := make(map[string]apiv1.Resource)
Expand Down
8 changes: 5 additions & 3 deletions pkg/engine/operation/models/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func (p *Changes) Summary(writer io.Writer, noStyle bool) {
// Create a fork of the default table, fill it with data and print it.
// Data can also be generated and inserted later.
tableHeader := []string{fmt.Sprintf("Stack: %s\nID", p.stack.Name), "\nAction"}
tableData := pterm.TableData{tableHeader}
tableData := make(pterm.TableData, 0, 1+len(p.Values()))
tableData = append(tableData, tableHeader)

if noStyle {
pterm.DisableStyling()
Expand All @@ -246,7 +247,8 @@ func (p *Changes) Summary(writer io.Writer, noStyle bool) {

func (o *ChangeOrder) PromptDetails(ui *terminal.UI) (string, error) {
// Prepare the selects
options := []string{"all"}
options := make([]string, 0, len(o.StepKeys)+2)
options = append(options, "all")
optionMaps := map[string]string{"all": "all"}

for _, key := range o.StepKeys {
Expand All @@ -267,7 +269,7 @@ func (o *ChangeOrder) PromptDetails(ui *terminal.UI) (string, error) {
WithOnInterruptFunc(func() {
hint := `Interruption during 'apply' or 'destroy' may result in a locked release file.
Please use 'kusion release unlock' before executing the next operation.`
fmt.Printf("\n" + hint + "\n")
fmt.Print("\n" + hint + "\n")
os.Exit(1)
}).
Show()
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
continue
}
if v1.IsErr(resp.Status) {
return fmt.Errorf(resp.Status.String())
return errors.New(resp.Status.String())
}

// Save watchers
Expand Down
36 changes: 18 additions & 18 deletions pkg/engine/printers/printer/k8s_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ func formatEndpoints(endpoints *corev1.Endpoints, ports sets.Set[string]) string
return "<none>"
}
list := []string{}
max := 3
maxItems := 3
more := false
count := 0
for i := range endpoints.Subsets {
ss := &endpoints.Subsets[i]
if len(ss.Ports) == 0 {
// It's possible to have headless services with no ports.
for i := range ss.Addresses {
if len(list) == max {
if len(list) == maxItems {
more = true
}
if !more {
Expand All @@ -211,7 +211,7 @@ func formatEndpoints(endpoints *corev1.Endpoints, ports sets.Set[string]) string
port := &ss.Ports[i]
if ports == nil || ports.Has(port.Name) {
for i := range ss.Addresses {
if len(list) == max {
if len(list) == maxItems {
more = true
}
addr := &ss.Addresses[i]
Expand All @@ -227,7 +227,7 @@ func formatEndpoints(endpoints *corev1.Endpoints, ports sets.Set[string]) string
}
ret := strings.Join(list, ",")
if more {
return fmt.Sprintf("%s + %d more...", ret, count-max)
return fmt.Sprintf("%s + %d more...", ret, count-maxItems)
}
return ret
}
Expand Down Expand Up @@ -687,7 +687,7 @@ func printServiceAccount(obj *corev1.ServiceAccount) (string, bool) {
}

func printDeployment(obj *appsv1.Deployment) (string, bool) {
desiredReplicas := *(obj.Spec.Replicas)
desiredReplicas := *obj.Spec.Replicas
updatedReplicas := obj.Status.UpdatedReplicas
readyReplicas := obj.Status.ReadyReplicas
availableReplicas := obj.Status.AvailableReplicas
Expand All @@ -706,48 +706,48 @@ func printEndpointSlice(obj *discoveryv1.EndpointSlice) (string, bool) {

func formatDiscoveryPorts(ports []discoveryv1.EndpointPort) string {
list := []string{}
max := 3
maxItems := 3
more := false
count := 0
for _, port := range ports {
if len(list) < max {
if len(list) < maxItems {
portNum := "*"
if port.Port != nil {
portNum = strconv.Itoa(int(*port.Port))
} else if port.Name != nil {
portNum = *port.Name
}
list = append(list, portNum)
} else if len(list) == max {
} else if len(list) == maxItems {
more = true
}
count++
}
return listWithMoreString(list, more, count, max)
return listWithMoreString(list, more, count, maxItems)
}

func formatDiscoveryEndpoints(endpoints []discoveryv1.Endpoint) string {
list := []string{}
max := 3
maxItems := 3
more := false
count := 0
for _, endpoint := range endpoints {
for _, address := range endpoint.Addresses {
if len(list) < max {
if len(list) < maxItems {
list = append(list, address)
} else if len(list) == max {
} else if len(list) == maxItems {
more = true
}
count++
}
}
return listWithMoreString(list, more, count, max)
return listWithMoreString(list, more, count, maxItems)
}

func listWithMoreString(list []string, more bool, count, max int) string {
func listWithMoreString(list []string, more bool, count, maxItems int) string {
ret := strings.Join(list, ",")
if more {
return fmt.Sprintf("%s + %d more...", ret, count-max)
return fmt.Sprintf("%s + %d more...", ret, count-maxItems)
}
if ret == "" {
ret = "<unset>"
Expand All @@ -756,7 +756,7 @@ func listWithMoreString(list []string, more bool, count, max int) string {
}

func printReplicaSet(obj *appsv1.ReplicaSet) (string, bool) {
desiredReplicas := *(obj.Spec.Replicas)
desiredReplicas := *obj.Spec.Replicas
currentReplicas := obj.Status.Replicas
readyReplicas := obj.Status.ReadyReplicas
return fmt.Sprintf("Desired: %d, Current: %d, Ready: %d",
Expand All @@ -776,7 +776,7 @@ func printDaemonSet(obj *appsv1.DaemonSet) (string, bool) {
}

func printStatefulSet(obj *appsv1.StatefulSet) (string, bool) {
desiredReplicas := *(obj.Spec.Replicas)
desiredReplicas := *obj.Spec.Replicas
readyReplicas := obj.Status.ReadyReplicas
createTime := translateTimestampSince(obj.CreationTimestamp)
return fmt.Sprintf("Ready: %d/%d, Age: %s", desiredReplicas, readyReplicas, createTime),
Expand Down Expand Up @@ -880,7 +880,7 @@ func printIngress(obj *networkingv1.Ingress) (string, bool) {
}

func getIngressHosts(obj *networkingv1.Ingress) string {
results := []string{}
results := make([]string, 0, len(obj.Spec.Rules))
for _, rule := range obj.Spec.Rules {
host := "*"
if rule.Host != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/runtime/terraform/tfops/tferrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func parseTerraformInfo(infos []byte) ([]*TerraformInfo, error) {
err := json.Unmarshal([]byte(v), terraformInfo)
if err != nil {
log.Debug("unmarshall TF output to TerraformInfo failed. Return the output directly.")
return nil, fmt.Errorf(string(infos))
return nil, errors.New(string(infos))
}
tfInfos = append(tfInfos, terraformInfo)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/oci/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func NewClient(options ...ClientOption) *Client {

// optionsWithContext returns the crane options for the given context.
func (c *Client) optionsWithContext(ctx context.Context) []crane.Option {
options := []crane.Option{
crane.WithContext(ctx),
}
options := make([]crane.Option, 0, 1+len(c.opts.craneOptions))
options = append(options, crane.WithContext(ctx))
return append(options, c.opts.craneOptions...)
}
Loading
Loading