Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya committed Sep 25, 2023
1 parent 3fef738 commit 8d93506
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 72 deletions.
26 changes: 19 additions & 7 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ linters-settings:
linters:
disable-all: true
enable:
- deadcode
- depguard
- dogsled
- dupl
- errcheck
Expand All @@ -67,21 +65,16 @@ linters:
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
- varcheck
- whitespace
- bodyclose
- noctx
- rowserrcheck
- structcheck
- unparam

# don't enable:
# - asciicheck
Expand All @@ -99,7 +92,19 @@ linters:
# - testpackage
# - scopelint
# - wsl
# - depguard # This is not being used in the project hence do not enable
# - unparam # TODO: Remove all unused params and re enable the linter
# WARN [runner] The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
# - deadcode
# WARN [runner] The linter 'structcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
# - structcheck
# WARN [runner] The linter 'varcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
# - varcheck

# TODO: Fix unused-parameters and enable the linters
# - revive
# TODO: Fix naked returns and enable the linters
# - nakedret
issues:
exclude:
- 'declaration of "(err|ctx)" shadows declaration at'
Expand All @@ -113,6 +118,13 @@ issues:
- gocritic
- funlen

- linters:
- gosec
text: "G602:" # TODO: Fix disable slice out of bounds issue and re enable the linter

- linters:
- gosec
text: "G601:" # TODO: Fix Implicit memory aliasing in for loop issue and re enable the linter
# https://github.com/go-critic/go-critic/issues/926
- linters:
- gocritic
Expand Down
2 changes: 1 addition & 1 deletion cmd/plugin/builder/command/cli_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Compile(compileArgs *PluginCompileArgs) error {
guard := make(chan struct{}, maxConcurrent)

// Mix up IDs so we don't always get the same set.
randSkew := rand.Intn(len(helpers.Identifiers)) // nolint:gosec
randSkew := rand.Intn(len(helpers.Identifiers)) //nolint:gosec
var wg sync.WaitGroup
plugins := make(chan cli.Plugin, len(files))
fatalErrors := make(chan helpers.ErrInfo, len(files))
Expand Down
8 changes: 4 additions & 4 deletions cmd/plugin/builder/inventory_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ type inventoryPluginActivateDeactivateFlags struct {
InventoryDBFile string
}

func newInventoryPluginActivateCmd() *cobra.Command { // nolint:dupl
func newInventoryPluginActivateCmd() *cobra.Command { //nolint:dupl
pluginActivateCmd, flags := getActivateDeactivateBaseCmd()
pluginActivateCmd.Use = "activate" // nolint:goconst
pluginActivateCmd.Use = "activate" //nolint:goconst
pluginActivateCmd.Short = "Activate the existing plugin in the inventory database available on the remote repository"
pluginActivateCmd.Example = ""
pluginActivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
Expand All @@ -111,9 +111,9 @@ func newInventoryPluginActivateCmd() *cobra.Command { // nolint:dupl
return pluginActivateCmd
}

func newInventoryPluginDeactivateCmd() *cobra.Command { // nolint:dupl
func newInventoryPluginDeactivateCmd() *cobra.Command { //nolint:dupl
pluginDeactivateCmd, flags := getActivateDeactivateBaseCmd()
pluginDeactivateCmd.Use = "deactivate" // nolint:goconst
pluginDeactivateCmd.Use = "deactivate" //nolint:goconst
pluginDeactivateCmd.Short = "Deactivate the existing plugin in the inventory database available on the remote repository"
pluginDeactivateCmd.Example = ""
pluginDeactivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/airgapped/plugin_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ imagesToCopy:

// downloadInventoryImageAndSaveFilesToDirStub fakes the image downloads and puts a database
// with the table schemas created to provided path
downloadInventoryImageAndSaveFilesToDirStub := func(image, path string) error {
downloadInventoryImageAndSaveFilesToDirStub := func(_, path string) error {
dbFile := filepath.Join(path, plugininventory.SQliteDBFileName)
err := utils.SaveFile(dbFile, []byte{})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -233,7 +233,7 @@ imagesToCopy:

// downloadInventoryMetadataImageWithNoExistingPlugins fakes the image downloads and puts a database
// with the table schemas created to provided path
downloadInventoryMetadataImageWithNoExistingPlugins := func(image, path string) error {
downloadInventoryMetadataImageWithNoExistingPlugins := func(_, path string) error {
dbFile := filepath.Join(path, plugininventory.SQliteInventoryMetadataDBFileName)
err := utils.SaveFile(dbFile, []byte{})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -247,7 +247,7 @@ imagesToCopy:

// downloadInventoryMetadataImageWithExistingPlugins fakes the image downloads and puts a database
// with the table schemas created to provided path
downloadInventoryMetadataImageWithExistingPlugins := func(image, path string) error {
downloadInventoryMetadataImageWithExistingPlugins := func(_, path string) error {
dbFile := filepath.Join(path, plugininventory.SQliteInventoryMetadataDBFileName)
err := utils.SaveFile(dbFile, []byte{})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -272,7 +272,7 @@ imagesToCopy:
}

// copyImageToTarStub fakes the image downloads and creates a fake tar.gz file for images
copyImageToTarStub := func(image, tarfile string) error {
copyImageToTarStub := func(_, tarfile string) error {
_, err := os.Create(tarfile)
Expect(err).ToNot(HaveOccurred())
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/csp/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ type configSource struct {

// Token fetches the token.
func (c *configSource) Token() (*oauth2.Token, error) {
g, err := c.GetCurrentServer() // nolint:staticcheck // Deprecated
g, err := c.GetCurrentServer() //nolint:staticcheck // Deprecated
if err != nil {
return nil, err
}
if g == nil {
return nil, fmt.Errorf("current server is nil")
}
if !g.IsGlobal() { // nolint:staticcheck // Deprecated
if !g.IsGlobal() { //nolint:staticcheck // Deprecated
return nil, fmt.Errorf("trying to fetch token for non global server")
}
var expiration time.Time
Expand Down
1 change: 1 addition & 0 deletions pkg/auth/csp/selfmanaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func runLocalListener() error {
mux := http.NewServeMux()
mux.HandleFunc("/callback", callbackHandler)
tokenExchange, tokenExchangeComplete = context.WithCancel(context.TODO())
//nolint:gosec //G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
l := http.Server{
Addr: "",
Handler: mux,
Expand Down
6 changes: 3 additions & 3 deletions pkg/auth/tkg/cluster_pinniped_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetClusterInfoFromCluster(clusterAPIServerURL, configmapName, endpointCACer

clusterAPIServerURL = strings.TrimRight(clusterAPIServerURL, " /")
clusterInfoURL := clusterAPIServerURL + fmt.Sprintf("/api/v1/namespaces/%s/configmaps/%s", KubePublicNamespace, configmapName)
req, _ := http.NewRequest("GET", clusterInfoURL, http.NoBody)
req, _ := http.NewRequest("GET", clusterInfoURL, http.NoBody) //nolint:noctx //should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)

tlsConfig, err := GetTLSConfig(endpointCACertPath, skipTLSVerify)
if err != nil {
Expand Down Expand Up @@ -126,7 +126,7 @@ func GetPinnipedInfoFromCluster(clusterInfo *clientcmdapi.Cluster, discoveryPort
}
}
pinnipedInfoURL := endpoint + fmt.Sprintf("/api/v1/namespaces/%s/configmaps/pinniped-info", KubePublicNamespace)
req, _ := http.NewRequest("GET", pinnipedInfoURL, http.NoBody)
req, _ := http.NewRequest("GET", pinnipedInfoURL, http.NoBody) //nolint:noctx //should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(clusterInfo.CertificateAuthorityData)
clusterClient := &http.Client{
Expand Down Expand Up @@ -187,7 +187,7 @@ func GetTLSConfig(caCertPath string, skipTLSVerify bool) (*tls.Config, error) {
return &tls.Config{
RootCAs: pool,
MinVersion: tls.VersionTLS12,
// nolint:gosec
//nolint:gosec
// skipTLSVerify: true is only possible if the user has explicitly enabled insecure-skip-tls-verify.
InsecureSkipVerify: skipTLSVerify,
}, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/auth/tkg/kube_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func TanzuLocalKubeConfigPath() (path string, err error) {
}

func MergeAndSaveKubeconfigBytes(kubeconfigBytes []byte, options *KubeConfigOptions) (mergeFilePath, currentContext string, err error) {
mergeFilePath = ""
if options != nil && options.MergeFilePath != "" {
mergeFilePath = options.MergeFilePath
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/wcp/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
func IsVSphereSupervisor(endpoint string, httpClient *http.Client) (bool, error) {
loginBannerURL := fmt.Sprintf("%s/wcp/loginbanner", endpoint)

req, _ := http.NewRequest("GET", loginBannerURL, http.NoBody)
req, _ := http.NewRequest("GET", loginBannerURL, http.NoBody) //nolint:noctx //should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)

resp, err := httpClient.Do(req)
if err != nil {
Expand Down
58 changes: 29 additions & 29 deletions pkg/command/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func init() {
loginCmd.Flags().BoolVar(&staging, "staging", false, "use CSP staging issuer")
loginCmd.Flags().StringVar(&endpointCACertPath, "endpoint-ca-certificate", "", "path to the endpoint public certificate")
loginCmd.Flags().BoolVar(&skipTLSVerify, "insecure-skip-tls-verify", false, "skip endpoint's TLS certificate verification")
loginCmd.Flags().MarkHidden("stderr-only") // nolint
loginCmd.Flags().MarkHidden("force-csp") // nolint
loginCmd.Flags().MarkHidden("staging") // nolint
loginCmd.Flags().MarkHidden("stderr-only") //nolint
loginCmd.Flags().MarkHidden("force-csp") //nolint
loginCmd.Flags().MarkHidden("staging") //nolint
loginCmd.SetUsageFunc(cli.SubCmdUsageFunc)
loginCmd.MarkFlagsMutuallyExclusive("endpoint-ca-certificate", "insecure-skip-tls-verify")

Expand Down Expand Up @@ -94,7 +94,7 @@ func login(cmd *cobra.Command, args []string) (err error) {
}

newServerSelector := "+ new server"
var serverTarget *configtypes.Server // nolint:staticcheck // Deprecated
var serverTarget *configtypes.Server //nolint:staticcheck // Deprecated
if name != "" {
serverTarget, err = createNewServer()
if err != nil {
Expand All @@ -106,7 +106,7 @@ func login(cmd *cobra.Command, args []string) (err error) {
return err
}
} else {
serverTarget, err = config.GetServer(server) // nolint:staticcheck // Deprecated
serverTarget, err = config.GetServer(server) //nolint:staticcheck // Deprecated
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func login(cmd *cobra.Command, args []string) (err error) {
}
}

if serverTarget.Type == configtypes.GlobalServerType { // nolint:staticcheck // Deprecated
if serverTarget.Type == configtypes.GlobalServerType { //nolint:staticcheck // Deprecated
err = globalLoginUsingServer(serverTarget)
} else {
err = managementClusterLogin(serverTarget)
Expand All @@ -137,11 +137,11 @@ func login(cmd *cobra.Command, args []string) (err error) {
return nil
}

func getServerTarget(cfg *configtypes.ClientConfig, newServerSelector string) (*configtypes.Server, error) { // nolint:staticcheck // Deprecated
func getServerTarget(cfg *configtypes.ClientConfig, newServerSelector string) (*configtypes.Server, error) { //nolint:staticcheck // Deprecated
promptOpts := getPromptOpts()
servers := map[string]*configtypes.Server{} // nolint:staticcheck // Deprecated
for _, server := range cfg.KnownServers { // nolint:staticcheck // Deprecated
ep, err := config.EndpointFromServer(server) // nolint:staticcheck // Deprecated
servers := map[string]*configtypes.Server{} //nolint:staticcheck // Deprecated
for _, server := range cfg.KnownServers { //nolint:staticcheck // Deprecated
ep, err := config.EndpointFromServer(server) //nolint:staticcheck // Deprecated
if err != nil {
return nil, err
}
Expand All @@ -159,7 +159,7 @@ func getServerTarget(cfg *configtypes.ClientConfig, newServerSelector string) (*
}
serverKeys := getKeysFromServerMap(servers)
serverKeys = append(serverKeys, newServerSelector)
servers[newServerSelector] = &configtypes.Server{} // nolint:staticcheck // Deprecated
servers[newServerSelector] = &configtypes.Server{} //nolint:staticcheck // Deprecated
err := component.Prompt(
&component.PromptConfig{
Message: "Select a server",
Expand All @@ -175,7 +175,7 @@ func getServerTarget(cfg *configtypes.ClientConfig, newServerSelector string) (*
return servers[server], nil
}

func getKeysFromServerMap(m map[string]*configtypes.Server) []string { // nolint:staticcheck // Deprecated
func getKeysFromServerMap(m map[string]*configtypes.Server) []string { //nolint:staticcheck // Deprecated
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
Expand All @@ -194,7 +194,7 @@ func isGlobalServer(endpoint string) bool {
return false
}

func createNewServer() (server *configtypes.Server, err error) { // nolint:staticcheck // Deprecated
func createNewServer() (server *configtypes.Server, err error) { //nolint:staticcheck // Deprecated
// user provided command line options to create a server using kubeconfig[optional] and context
if kubeContext != "" {
return createServerWithKubeconfig()
Expand Down Expand Up @@ -227,7 +227,7 @@ func createNewServer() (server *configtypes.Server, err error) { // nolint:stati
return createServerWithKubeconfig()
}

func createServerWithKubeconfig() (server *configtypes.Server, err error) { // nolint:staticcheck // Deprecated
func createServerWithKubeconfig() (server *configtypes.Server, err error) { //nolint:staticcheck // Deprecated
promptOpts := getPromptOpts()
if kubeConfig == "" && kubeContext == "" {
err = component.Prompt(
Expand Down Expand Up @@ -272,7 +272,7 @@ func createServerWithKubeconfig() (server *configtypes.Server, err error) { // n
}
}
name = strings.TrimSpace(name)
nameExists, err := config.ServerExists(name) // nolint:staticcheck // Deprecated
nameExists, err := config.ServerExists(name) //nolint:staticcheck // Deprecated
if err != nil {
return server, err
}
Expand All @@ -281,20 +281,20 @@ func createServerWithKubeconfig() (server *configtypes.Server, err error) { // n
return
}

endpointType := configtypes.ManagementClusterServerType // nolint:staticcheck // Deprecated
endpointType := configtypes.ManagementClusterServerType //nolint:staticcheck // Deprecated

server = &configtypes.Server{ // nolint:staticcheck // Deprecated
server = &configtypes.Server{ //nolint:staticcheck // Deprecated
Name: name,
Type: endpointType,
ManagementClusterOpts: &configtypes.ManagementClusterServer{ // nolint:staticcheck // Deprecated
ManagementClusterOpts: &configtypes.ManagementClusterServer{ //nolint:staticcheck // Deprecated
Path: kubeConfig,
Context: kubeContext,
Endpoint: endpoint},
}
return server, err
}

func createServerWithEndpoint() (server *configtypes.Server, err error) { // nolint:staticcheck // Deprecated
func createServerWithEndpoint() (server *configtypes.Server, err error) { //nolint:staticcheck // Deprecated
promptOpts := getPromptOpts()
if endpoint == "" {
err = component.Prompt(
Expand Down Expand Up @@ -322,7 +322,7 @@ func createServerWithEndpoint() (server *configtypes.Server, err error) { // nol
}
}
name = strings.TrimSpace(name)
nameExists, err := config.ServerExists(name) // nolint:staticcheck // Deprecated
nameExists, err := config.ServerExists(name) //nolint:staticcheck // Deprecated
if err != nil {
return server, err
}
Expand All @@ -331,9 +331,9 @@ func createServerWithEndpoint() (server *configtypes.Server, err error) { // nol
return
}
if isGlobalServer(endpoint) {
server = &configtypes.Server{ // nolint:staticcheck // Deprecated
server = &configtypes.Server{ //nolint:staticcheck // Deprecated
Name: name,
Type: configtypes.GlobalServerType, // nolint:staticcheck // Deprecated
Type: configtypes.GlobalServerType, //nolint:staticcheck // Deprecated
GlobalOpts: &configtypes.GlobalServer{Endpoint: sanitizeEndpoint(endpoint)},
}
} else {
Expand All @@ -343,10 +343,10 @@ func createServerWithEndpoint() (server *configtypes.Server, err error) { // nol
return
}

server = &configtypes.Server{ // nolint:staticcheck // Deprecated
server = &configtypes.Server{ //nolint:staticcheck // Deprecated
Name: name,
Type: configtypes.ManagementClusterServerType, // nolint:staticcheck // Deprecated
ManagementClusterOpts: &configtypes.ManagementClusterServer{ // nolint:staticcheck // Deprecated
Type: configtypes.ManagementClusterServerType, //nolint:staticcheck // Deprecated
ManagementClusterOpts: &configtypes.ManagementClusterServer{ //nolint:staticcheck // Deprecated
Path: kubeConfig,
Context: kubeContext,
Endpoint: endpoint},
Expand All @@ -355,7 +355,7 @@ func createServerWithEndpoint() (server *configtypes.Server, err error) { // nol
return server, err
}

func globalLoginUsingServer(s *configtypes.Server) (err error) { // nolint:staticcheck // Deprecated
func globalLoginUsingServer(s *configtypes.Server) (err error) { //nolint:staticcheck // Deprecated
a := configtypes.GlobalServerAuth{}
apiTokenValue, apiTokenExists := os.LookupEnv(config.EnvAPITokenKey)

Expand Down Expand Up @@ -396,7 +396,7 @@ func globalLoginUsingServer(s *configtypes.Server) (err error) { // nolint:stati
s.GlobalOpts.Auth = a
}

err = config.PutServer(s, true) // nolint:staticcheck // Deprecated
err = config.PutServer(s, true) //nolint:staticcheck // Deprecated
if err != nil {
return err
}
Expand All @@ -406,15 +406,15 @@ func globalLoginUsingServer(s *configtypes.Server) (err error) { // nolint:stati
return nil
}

func managementClusterLogin(s *configtypes.Server) error { // nolint:staticcheck // Deprecated
func managementClusterLogin(s *configtypes.Server) error { //nolint:staticcheck // Deprecated
if s != nil && s.ManagementClusterOpts != nil && s.ManagementClusterOpts.Path != "" && s.ManagementClusterOpts.Context != "" {
_, err := tkgauth.GetServerKubernetesVersion(s.ManagementClusterOpts.Path, s.ManagementClusterOpts.Context)
if err != nil {
err := fmt.Errorf("failed to login to the management cluster %s, %v", s.Name, err)
log.Error(err, "")
return err
}
err = config.PutServer(s, true) // nolint:staticcheck // Deprecated
err = config.PutServer(s, true) //nolint:staticcheck // Deprecated
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8d93506

Please sign in to comment.