Skip to content

Commit

Permalink
Add linters (#6)
Browse files Browse the repository at this point in the history
* Add linters

* update local to 1.43

* Lint command
  • Loading branch information
Ryan Wholey authored Dec 7, 2021
1 parent ff32941 commit 169b6f7
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ jobs:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
args: --timeout=10m0s
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
run:
deadline: 15m
linters:
enable-all: true
disable:
- interfacer
- testpackage
- goerr113
- scopelint
- gomnd
- exhaustivestruct
- lll
- funlen
- wrapcheck
- paralleltest
- cyclop
- maligned
- stylecheck
- varnamelen
issues:
exclude-use-default: false
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: lint

lint:
golangci-lint run ./...
7 changes: 2 additions & 5 deletions cmd/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ func newForwardCommand() *cobra.Command {
return err
}

if err := command.Run(ctx, client, args[0], config, cmdArgs, streams); err != nil {
return err
}

return nil
return command.Run(ctx, client, args[0], config, cmdArgs, streams)
},
}

Expand Down Expand Up @@ -103,6 +99,7 @@ func parseArgFlag(cmd *cobra.Command) (map[string]string, error) {
if err != nil {
return nil, err
}

return cmdArgs, nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/command/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
)

// Args store command specific arguments to be passed to the hook commands.
type Args map[string]string

// parseArgs parses key value pairs from the passed annotations map, adds any overrides passed and returns a new args map.
Expand Down
6 changes: 5 additions & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

// Command represents a runnable command.
type Command struct {
ID string `json:"id"`
Command []string `json:"command"`
Interactive bool `json:"interactive"`
}

// cmd returns a golang cmd object from the calling command
// toCmd returns a golang cmd object from the calling command.
func (c Command) toCmd(ctx context.Context) *exec.Cmd {
name := c.Command[0]
args := []string{}
Expand All @@ -24,11 +25,14 @@ func (c Command) toCmd(ctx context.Context) *exec.Cmd {
args = append(args, c.Command[1:]...)
}

// nolint:gosec
return exec.CommandContext(ctx, name, args...)
}

// execute runs the command with the given config and outputs.
// nolint:unparam
func (c Command) execute(ctx context.Context, config *Config, arguments *Args, outputs map[string]Output, streams *genericclioptions.IOStreams) (Output, error) {
// nolint:godox
// TODO: Add in go templating to pair the args and config with the passed commands
cmd := c.toCmd(ctx)

Expand Down
1 change: 1 addition & 0 deletions internal/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

// Commands stores a slice of commands and provides some helper execution methods.
type Commands []*Command

// execute runs each command in the calling slice sequentially using the passed config and the outputs accumulated to that point.
Expand Down
1 change: 1 addition & 0 deletions internal/command/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command

// Config stores configuration which is used to construct the tunnel as well as passed to the hook commands.
type Config struct {
LocalPort int
Verbose bool
Expand Down
3 changes: 2 additions & 1 deletion internal/command/hooks.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package command

// Hooks store information regarding command hooks.
type Hooks struct {
Pre Commands
Post Commands
}

// newHooks returns a new Hooks struct assembled from the passed annotations
// newHooks returns a new Hooks struct assembled from the passed annotations.
func newHooks(annotations map[string]string) (*Hooks, error) {
pre, err := parseCommands(annotations, preAnnotation)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/command/outputs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command

// Output stores command output.
type Output struct {
Stdout string
Stderr string
Expand Down
2 changes: 2 additions & 0 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Run(ctx context.Context, client *kubernetes.Client, resource string, config
select {
case err := <-hookErrChan:
client.Opts.StopChannel <- struct{}{}

cancel()

return err
Expand All @@ -71,6 +72,7 @@ func Run(ctx context.Context, client *kubernetes.Client, resource string, config
return err
case <-doneChan:
client.Opts.StopChannel <- struct{}{}

cancel()

return nil
Expand Down
7 changes: 2 additions & 5 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/kubectl/pkg/scheme"
)

// Client represents a Kubernetes Client sufficient to forward to a Kubernetes resource.
type Client struct {
Opts *portforward.PortForwardOptions
builder *resource.Builder
Expand Down Expand Up @@ -38,9 +39,5 @@ func (c *Client) Init(getter genericclioptions.RESTClientGetter, cmd *cobra.Comm
return err
}

if err := c.Opts.Validate(); err != nil {
return err
}

return nil
return c.Opts.Validate()
}
3 changes: 3 additions & 0 deletions internal/kubernetes/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/kubectl/pkg/cmd/portforward"
)

// PortForwarder is responsible for managing a forwarding connection.
type PortForwarder genericclioptions.IOStreams

// ForwardPorts is an interface requirement for PortForwarder which handles the port forwarding connection.
Expand Down Expand Up @@ -48,10 +49,12 @@ func runPortForward(ctx context.Context, o portforward.PortForwardOptions) error

signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)

defer signal.Stop(signals)

go func() {
<-signals

if o.StopChannel != nil {
close(o.StopChannel)
}
Expand Down
1 change: 1 addition & 0 deletions internal/ports/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/phayes/freeport"
)

// Ports store port mapping information.
type Ports struct {
Local int
Remote interface{}
Expand Down

0 comments on commit 169b6f7

Please sign in to comment.