Skip to content

Commit

Permalink
Merge pull request #1914 from loft-sh/thomaskosiewski/nilaway
Browse files Browse the repository at this point in the history
lint(nilaway): Added nilaway to project
  • Loading branch information
FabianKramm authored Jul 10, 2024
2 parents 41d5d52 + 9eb6b62 commit f0825e9
Show file tree
Hide file tree
Showing 91 changed files with 982 additions and 379 deletions.
6 changes: 6 additions & 0 deletions .custom-gcl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This has to be >= v1.57.0 for module plugin system support.
version: v1.59.1
plugins:
- module: "go.uber.org/nilaway"
import: "go.uber.org/nilaway/cmd/gclplugin"
version: latest # Or a fixed version for reproducible builds.
8 changes: 7 additions & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ jobs:
exit 1
fi
- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/[email protected]

- name: Build custom golangci-lint
run: golangci-lint custom

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
run: ./custom-gcl run
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ profile.out
*.test
tests/__snapshot__
/licenses
custom-gcl
19 changes: 17 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ linters:
disable-all: true
enable:
- asasalint
- tagalign
- asciicheck
- bidichk
- decorder
Expand All @@ -28,10 +27,14 @@ linters:
- makezero
- misspell
- nakedret
- nilaway
- nilnil
- promlinter
- revive
- sloglint
- staticcheck
- stylecheck
- tagalign
- typecheck
- unconvert
- unused
Expand All @@ -40,7 +43,6 @@ linters:

# next linters to be enabled:
# - prealloc
# - nilnil
# - errchkjson
# - gocritic

Expand Down Expand Up @@ -81,11 +83,24 @@ linters-settings:

tagalign:
order:
- protobuf
- json
- yaml
- xml
- form

custom:
nilaway:
type: "module"
description: Static analysis tool to detect potential nil panics in Go code.
settings:
# Settings must be a "map from string to string" to mimic command line flags: the keys are
# flag names and the values are the values to the particular flags.
include-pkgs: "github.com/loft-sh/vcluster"
exclude-pkgs: "github.com/loft-sh/vcluster/test"
experimental-anonymous-function: "true"
# experimental-struct-init: "true"

issues:
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0
Expand Down
3 changes: 3 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
!/.github/
/vendor/
!.golangci.yml
!.goreleaser.yaml
!.custom-gcl.yml
4 changes: 3 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ release-snapshot: gen-license-report

# Run golangci-lint for all packages
lint *ARGS:
golangci-lint run {{ARGS}}
[ -f ./custom-gcl ] || golangci-lint custom
./custom-gcl cache clean
./custom-gcl run {{ARGS}}

# Check struct memory alignment and print potential improvements
[no-exit-message]
Expand Down
14 changes: 12 additions & 2 deletions cmd/vclusterctl/cmd/disconnect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"

"github.com/loft-sh/log"
Expand Down Expand Up @@ -52,9 +53,14 @@ vcluster disconnect

// Run executes the functionality
func (cmd *DisconnectCmd) Run() error {
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{
CurrentContext: cmd.Context,
}).RawConfig()
})
if clientConfig == nil {
return errors.New("nil clientConfig")
}

rawConfig, err := clientConfig.RawConfig()
if err != nil {
return err
}
Expand Down Expand Up @@ -102,6 +108,10 @@ func (cmd *DisconnectCmd) Run() error {
}

func (cmd *DisconnectCmd) selectContext(kubeConfig *clientcmdapi.Config, currentContext string) (string, error) {
if kubeConfig == nil {
return "", errors.New("nil kubeConfig")
}

availableContexts := []string{}
for context := range kubeConfig.Contexts {
if context != currentContext {
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/platform/add/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (cmd *ClusterCmd) Run(ctx context.Context, args []string) error {
return false, err
}

return clusterInstance.Status.Phase == storagev1.ClusterStatusPhaseInitialized, nil
return clusterInstance != nil && clusterInstance.Status.Phase == storagev1.ClusterStatusPhaseInitialized, nil
})
if waitErr != nil {
return fmt.Errorf("get cluster: %w", waitErr)
Expand Down
14 changes: 9 additions & 5 deletions cmd/vclusterctl/cmd/platform/get/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
)

var (
ErrNotLoftContext = errors.New("current context is not a loft context, but predefined var LOFT_CLUSTER is used")
)
var ErrNotLoftContext = errors.New("current context is not a loft context, but predefined var LOFT_CLUSTER is used")

type clusterCmd struct {
*flags.GlobalFlags
Expand Down Expand Up @@ -92,7 +90,10 @@ func (c *clusterCmd) Run(ctx context.Context, _ []string) error {
return err
}

_, err = os.Stdout.Write([]byte(spaceInstance.Spec.ClusterRef.Cluster))
if spaceInstance != nil {
_, err = os.Stdout.Write([]byte(spaceInstance.Spec.ClusterRef.Cluster))
}

return err
}

Expand All @@ -117,7 +118,10 @@ func (c *clusterCmd) Run(ctx context.Context, _ []string) error {
return err
}

_, err = os.Stdout.Write([]byte(virtualClusterInstance.Spec.ClusterRef.Cluster))
if virtualClusterInstance != nil {
_, err = os.Stdout.Write([]byte(virtualClusterInstance.Spec.ClusterRef.Cluster))
}

return err
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/vclusterctl/cmd/platform/get/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,14 @@ func (cmd *SecretCmd) Run(ctx context.Context, args []string) error {
keyNames = append(keyNames, k)
}

defaultValue := ""
if len(keyNames) > 0 {
defaultValue = keyNames[0]
}

keyName, err = cmd.log.Question(&survey.QuestionOptions{
Question: "Please select a secret key to read",
DefaultValue: keyNames[0],
DefaultValue: defaultValue,
Options: keyNames,
})
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions cmd/vclusterctl/cmd/platform/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import (

type PasswordCmd struct {
*flags.GlobalFlags
Log log.Logger

User string
Password string
Create bool
Force bool

Log log.Logger
}

func NewResetCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
Expand Down Expand Up @@ -127,6 +126,9 @@ func (cmd *PasswordCmd) Run() error {
return err
}
}
if user == nil {
return errors.New("could not obtain user")
}

// check if user had a password before
if user.Spec.PasswordRef == nil || user.Spec.PasswordRef.SecretName == "" || user.Spec.PasswordRef.SecretNamespace == "" || user.Spec.PasswordRef.Key == "" {
Expand Down Expand Up @@ -184,6 +186,9 @@ func (cmd *PasswordCmd) Run() error {
return errors.Wrap(err, "create password secret")
}
} else {
if passwordSecret == nil {
passwordSecret = &corev1.Secret{}
}
if passwordSecret.Data == nil {
passwordSecret.Data = map[string][]byte{}
}
Expand Down
44 changes: 28 additions & 16 deletions cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -33,7 +34,11 @@ func NewRootCmd(log log.Logger) *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Short: "Welcome to vcluster!",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
if globalFlags == nil {
return errors.New("nil globalFlags")
}

if globalFlags.Config == "" {
var err error
globalFlags.Config, err = config.DefaultFilePath()
Expand All @@ -52,13 +57,13 @@ func NewRootCmd(log log.Logger) *cobra.Command {
} else {
log.SetLevel(logrus.InfoLevel)
}

return nil
},
Long: `vcluster root command`,
}
}

var globalFlags *flags.GlobalFlags

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand All @@ -69,36 +74,39 @@ func Execute() {

// start command
log := log.GetInstance()
rootCmd, err := BuildRoot(log)
rootCmd, globalFlags, err := BuildRoot(log)
if err != nil {
log.Fatalf("error building root: %+v\n", err)
}

// Execute command
err = rootCmd.ExecuteContext(context.Background())
recordAndFlush(err, log)
recordAndFlush(err, log, globalFlags)
if err != nil {
if globalFlags.Debug {
if globalFlags != nil && globalFlags.Debug {
log.Fatalf("%+v", err)
}

log.Fatal(err)
}
}

var globalFlags *flags.GlobalFlags

// BuildRoot creates a new root command from the
func BuildRoot(log log.Logger) (*cobra.Command, error) {
func BuildRoot(log log.Logger) (*cobra.Command, *flags.GlobalFlags, error) {
rootCmd := NewRootCmd(log)
persistentFlags := rootCmd.PersistentFlags()
globalFlags = flags.SetGlobalFlags(persistentFlags, log)

home, err := homedir.Dir()
if err != nil {
return nil, err
return nil, nil, err
}
defaults, err := defaults.NewFromPath(filepath.Join(home, defaults.ConfigFolder), defaults.ConfigFile)
if err != nil {
log.Debugf("Error loading defaults: %v", err)
return nil, err
return nil, nil, err
}

// Set version for --version flag
Expand All @@ -123,39 +131,43 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
// add platform commands
platformCmd, err := cmdplatform.NewPlatformCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create platform command: %w", err)
return nil, nil, fmt.Errorf("failed to create platform command: %w", err)
}
rootCmd.AddCommand(platformCmd)

loginCmd, err := NewLoginCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create login command: %w", err)
return nil, nil, fmt.Errorf("failed to create login command: %w", err)
}
rootCmd.AddCommand(loginCmd)

logoutCmd, err := NewLogoutCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create logout command: %w", err)
return nil, nil, fmt.Errorf("failed to create logout command: %w", err)
}
rootCmd.AddCommand(logoutCmd)

uiCmd, err := NewUICmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create ui command: %w", err)
return nil, nil, fmt.Errorf("failed to create ui command: %w", err)
}
rootCmd.AddCommand(uiCmd)
rootCmd.AddCommand(credits.NewCreditsCmd())

// add completion command
err = rootCmd.RegisterFlagCompletionFunc("namespace", completion.NewNamespaceCompletionFunc(rootCmd.Context()))
if err != nil {
return rootCmd, fmt.Errorf("failed to register completion for namespace: %w", err)
return nil, nil, fmt.Errorf("failed to register completion for namespace: %w", err)
}

return rootCmd, nil
return rootCmd, globalFlags, nil
}

func recordAndFlush(err error, log log.Logger) {
func recordAndFlush(err error, log log.Logger, globalFlags *flags.GlobalFlags) {
if globalFlags == nil {
panic("empty global flags")
}

telemetry.CollectorCLI.RecordCLI(globalFlags.LoadedConfig(log), platform.Self, err)
telemetry.CollectorCLI.Flush()
}
Loading

0 comments on commit f0825e9

Please sign in to comment.