Skip to content

Commit

Permalink
ci: better goreportcard score and adds golint
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 26, 2020
1 parent 87ad9fc commit ee416fb
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ jobs:
- name: Install golangci-lint
run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0

- name: Install golint
run: go get -u golang.org/x/lint/golint

- name: Run golangci-lint
run: $(go env GOPATH)/bin/golangci-lint run ./... --timeout 5m

- name: Run golint
run: $(go env GOPATH)/bin/golint run ./...
7 changes: 2 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
linters:
disable:
- goimports
- gochecknoglobals
- gosec
- prealloc
enable-all: true

# all available settings of specific linters
linters-settings:
unparam:
check-exported: true
funlen:
lines: 120
statements: 80
2 changes: 1 addition & 1 deletion cmd/hostctl/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func isValidURL(s string) bool {
}

func readerFromURL(url string) (io.Reader, error) {
resp, err := http.Get(url)
resp, err := http.Get(url) // nolint:gosec
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/hostctl/actions/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func TestReadFromURL(t *testing.T) {
p, err := profile.NewProfileFromReader(r, true)
assert.NoError(t, err)

hosts, err := p.GetAllHostNames()
assert.NoError(t, err)
hosts := p.GetAllHostNames()

assert.Equal(t, []string{"some.profile.loc"}, hosts)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/hostctl/actions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ The "default" profile is all the content that is not handled by hostctl tool.
return listCmd
}

var makeListStatusCmd = func(status types.Status) *cobra.Command {
func makeListStatusCmd(status types.Status) *cobra.Command {
cmd := ""
alias := ""

switch status {
case types.Enabled:
cmd = "enabled"
Expand All @@ -53,6 +54,7 @@ var makeListStatusCmd = func(status types.Status) *cobra.Command {
cmd = "disabled"
alias = "off"
}

return &cobra.Command{
Use: cmd,
Aliases: []string{alias},
Expand Down
6 changes: 5 additions & 1 deletion cmd/hostctl/actions/post_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func postRunListOnly(cmd *cobra.Command, args []string) error {
return postActionCmd(cmd, args, nil, true)
}

var postActionCmd = func(cmd *cobra.Command, args []string, postCmd *cobra.Command, list bool) error {
func postActionCmd(cmd *cobra.Command, args []string, postCmd *cobra.Command, list bool) error {
listCmd := newListCmd()
quiet, _ := cmd.Flags().GetBool("quiet")
duration, _ := cmd.Flags().GetDuration("wait")
Expand All @@ -43,6 +43,7 @@ var postActionCmd = func(cmd *cobra.Command, args []string, postCmd *cobra.Comma
if !quiet {
p := strings.Join(args, ", ")
_, _ = fmt.Fprintln(cmd.OutOrStdout())

if duration == 0 {
cligger.Info("Waiting until ctrl+c to %s from profile '%s'\n\n", action, p)
} else if duration > 0 {
Expand All @@ -61,11 +62,14 @@ var postActionCmd = func(cmd *cobra.Command, args []string, postCmd *cobra.Comma
if err != nil {
return err
}

if quiet {
return nil
}

return listCmd.RunE(cmd, args)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/hostctl/actions/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"github.com/guumaster/cligger"
)

// nolint:gochecknoglobals
var (
version = "dev"
snapBuild string
)

// NewRootCmd creates the base command for hostctl
func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "hostctl",
Expand Down
6 changes: 4 additions & 2 deletions pkg/docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ func ParseComposeFile(r io.Reader, projectName string) ([]string, error) {
return nil, fmt.Errorf("error parsing docker-compose content: %w", err)
}

var containers []string
containers := make([]string, len(data.Services))
i := 0

for serv, data := range data.Services {
name := data.ContainerName
if data.ContainerName == "" {
name = fmt.Sprintf("%s_%s", projectName, serv)
}

containers = append(containers, name)
containers[i] = name
i++
}

return containers, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/file/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestFile_AddProfile(t *testing.T) {

added, err := m.GetProfile("profile1")
assert.NoError(t, err)
hosts, err := added.GetHostNames(Localhost.String())
hosts, err := added.GetHostNames("127.0.0.1")
assert.NoError(t, err)

assert.Equal(t, hosts, []string{"first.loc", "second.loc", "added.loc"})
Expand Down
18 changes: 18 additions & 0 deletions pkg/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import (
"github.com/guumaster/hostctl/pkg/types"
)

// nolint:gochecknoglobals
var (
defaultProfile = "127.0.0.1 localhost\n"

testEnabledProfile = `
# profile.on profile1
127.0.0.1 first.loc
127.0.0.1 second.loc
# end
`
testDisabledProfile = `
# profile.off profile2
# 127.0.0.1 first.loc
# 127.0.0.1 second.loc
# end
`
)

func TestManagerStatus(t *testing.T) {
t.Run("Get Status", func(t *testing.T) {
mem := createBasicFS(t)
Expand Down
16 changes: 8 additions & 8 deletions pkg/file/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ package file

import (
"io/ioutil"
"net"
"os"
"testing"

"github.com/spf13/afero"
)

var Localhost = net.ParseIP("127.0.0.1")
func makeTempHostsFile(t *testing.T, pattern string) *os.File {
t.Helper()

var (
defaultProfile = "127.0.0.1 localhost\n"

var defaultProfile = "127.0.0.1 localhost\n"
var testEnabledProfile = `
testEnabledProfile = `
# profile.on profile1
127.0.0.1 first.loc
127.0.0.1 second.loc
# end
`
var testDisabledProfile = `
testDisabledProfile = `
# profile.off profile2
# 127.0.0.1 first.loc
# 127.0.0.1 second.loc
# end
`

func makeTempHostsFile(t *testing.T, pattern string) *os.File {
t.Helper()
)

file, err := ioutil.TempFile("/tmp", pattern+"_")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/file/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

// MergeFile joins new content with existing content
func (f *File) MergeFile(from *File) {
var ps []*types.Profile
for _, p := range from.data.Profiles {
ps = append(ps, p)
ps := make([]*types.Profile, len(from.data.Profiles))
for i, name := range from.data.ProfileNames {
ps[i] = from.data.Profiles[name]
}

f.MergeProfiles(ps)
Expand Down
3 changes: 2 additions & 1 deletion pkg/file/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func TestFile_MergeProfiles(t *testing.T) {
p2, err := m.GetProfile("profile2")
assert.NoError(t, err)

ip := net.ParseIP("127.0.0.1")
modP2 := profiles[0]
modP2.IPList = []string{"127.0.0.1", "2.2.2.2"}
modP2.Routes[Localhost.String()] = &types.Route{IP: Localhost, HostNames: []string{"first.loc", "second.loc"}}
modP2.Routes[ip.String()] = &types.Route{IP: ip, HostNames: []string{"first.loc", "second.loc"}}
modP2.Status = types.Disabled
assert.Equal(t, modP2, p2)
}
1 change: 1 addition & 0 deletions pkg/file/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package file

// Banner is the mark added to hosts file
const Banner = `
##################################################################
# Content under this line is handled by hostctl. DO NOT EDIT.
Expand Down
9 changes: 3 additions & 6 deletions pkg/profile/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func TestNewProfileFromDocker(t *testing.T) {

assert.NoError(t, err)

hosts, err := p.GetAllHostNames()
assert.NoError(t, err)
hosts := p.GetAllHostNames()

assert.Equal(t, []string{"172.0.0.2", "172.0.0.3"}, p.IPList)
assert.Equal(t, []string{"container1.test", "container2.test"}, hosts)
Expand All @@ -60,8 +59,7 @@ func TestNewProfileFromDocker(t *testing.T) {

assert.NoError(t, err)

hosts, err := p.GetAllHostNames()
assert.NoError(t, err)
hosts := p.GetAllHostNames()

assert.Equal(t, []string{"172.0.0.3"}, p.IPList)
assert.Equal(t, []string{"container2.test"}, hosts)
Expand Down Expand Up @@ -113,8 +111,7 @@ networks:

assert.NoError(t, err)

hosts, err := p.GetAllHostNames()
assert.NoError(t, err)
hosts := p.GetAllHostNames()

assert.Equal(t, []string{"172.0.0.2", "172.0.0.3"}, p.IPList)
assert.Equal(t, []string{"container1_1.loc", "container2_1.loc"}, hosts)
Expand Down
2 changes: 2 additions & 0 deletions pkg/profile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/guumaster/hostctl/pkg/types"
)

// nolint:gochecknoglobals
var (
profileNameRe = regexp.MustCompile(`# profile(?:.(on|off))?\s+([a-z0-9-_.\s]+)`)
profileEnd = regexp.MustCompile(`(?i)# end\s*`)
spaceRemover = regexp.MustCompile(`\s+`)
tabReplacer = regexp.MustCompile(`\t+`)
)

// Parser is the interface for content parsers
type Parser interface {
Parse(reader io.Reader) types.Content
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"github.com/guumaster/hostctl/pkg/types"
)

// JSONRendererOptions contains options to render JSON content
type JSONRendererOptions struct {
Writer io.Writer
Columns []string
OnlyEnabled bool
}

// JSONRenderer is the Renderer used to output JSON
type JSONRenderer struct {
Type RendererType
Columns []string
Expand All @@ -24,6 +26,7 @@ type data struct {
lines []line
}

// NewJSONRenderer creates an instance of JSONRenderer
func NewJSONRenderer(opts *JSONRendererOptions) JSONRenderer {
if len(opts.Columns) == 0 {
opts.Columns = types.DefaultColumns
Expand All @@ -37,6 +40,7 @@ func NewJSONRenderer(opts *JSONRendererOptions) JSONRenderer {
}
}

// AddSeparator not used on JSONRenderer
func (j JSONRenderer) AddSeparator() {
// not used
}
Expand All @@ -48,6 +52,7 @@ type line struct {
Host string
}

// AppendRow adds a new row to the list
func (j JSONRenderer) AppendRow(row *types.Row) {
if row.Comment != "" {
return
Expand All @@ -62,6 +67,7 @@ func (j JSONRenderer) AppendRow(row *types.Row) {
j.data.lines = append(j.data.lines, l)
}

// Render returns a JSON representation of the list content
func (j JSONRenderer) Render() error {
enc := json.NewEncoder(j.w)

Expand Down
5 changes: 2 additions & 3 deletions pkg/render/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package render

import (
"github.com/guumaster/tablewriter"

"github.com/guumaster/hostctl/pkg/types"
)

// NewMarkdownRenderer creates an instance of TableRenderer
func NewMarkdownRenderer(opts *TableRendererOptions) TableRenderer {
table := createTableWriter(opts)

Expand All @@ -21,7 +20,7 @@ func NewMarkdownRenderer(opts *TableRendererOptions) TableRenderer {
Columns: opts.Columns,
table: table,
opts: opts,
meta: &types.Meta{
meta: &meta{
Rows: 0,
},
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/render/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package render

import (
"github.com/guumaster/tablewriter"

"github.com/guumaster/hostctl/pkg/types"
)

// NewRawRenderer creates an instance of TableRenderer without borders
func NewRawRenderer(opts *TableRendererOptions) TableRenderer {
table := createTableWriter(opts)

Expand All @@ -25,7 +24,7 @@ func NewRawRenderer(opts *TableRendererOptions) TableRenderer {
Columns: opts.Columns,
table: table,
opts: opts,
meta: &types.Meta{
meta: &meta{
Rows: 0,
Raw: true,
},
Expand Down
Loading

0 comments on commit ee416fb

Please sign in to comment.