diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3a503b1..fb0f1b1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -22,7 +22,7 @@ jobs: uses: actions/checkout@v2 - 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 + run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.33.0 - name: Install golint run: go get -u golang.org/x/lint/golint diff --git a/.golangci.yml b/.golangci.yml index 89d5f52..8b4b51a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,10 +1,115 @@ -linters: - enable-all: true - -# all available settings of specific linters linters-settings: - unparam: - check-exported: true + dupl: + threshold: 100 funlen: lines: 120 - statements: 80 + statements: 60 + gci: + local-prefixes: github.com/golangci/golangci-lint + goconst: + min-len: 2 + min-occurrences: 2 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - dupImport # https://github.com/go-critic/go-critic/issues/845 + - ifElseChain + - octalLiteral + - whyNoLint + - wrapperFunc + gocyclo: + min-complexity: 15 + goimports: + local-prefixes: github.com/golangci/golangci-lint + golint: + min-confidence: 0 + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: argument,case,condition,return + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US + ignore-words: + - konstellation + nolintlint: + allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space) + allow-unused: false # report any unused nolint directives + require-explanation: false # don't require an explanation for nolint directives + require-specific: false # don't require nolint directives to be specific about which linter is being skipped + unparam: + check-exported: true +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - exhaustive + - funlen + - gochecknoinits + - goconst + # - gocritic + - gocyclo + - gofmt + - goimports + - golint + - gomnd + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - interfacer + - lll + - misspell + - nakedret + - noctx + - nolintlint + - rowserrcheck + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + - asciicheck + - gochecknoglobals + - gocognit + - godot + - godox + - maligned + - nestif + - prealloc + - wsl + - goerr113 +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd + - dupl + - goconst + # https://github.com/go-critic/go-critic/issues/926 + - linters: + - gocritic + text: "unnecessaryDefer:" diff --git a/cmd/hostctl/actions/add_domains.go b/cmd/hostctl/actions/add_domains.go index dcd3f06..7b2bb80 100644 --- a/cmd/hostctl/actions/add_domains.go +++ b/cmd/hostctl/actions/add_domains.go @@ -3,12 +3,10 @@ package actions import ( "strings" - "github.com/spf13/cobra" - "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) func newAddRemoveDomainsCmd() (*cobra.Command, *cobra.Command) { diff --git a/cmd/hostctl/actions/add_replace.go b/cmd/hostctl/actions/add_replace.go index f37ddf1..98d86ce 100644 --- a/cmd/hostctl/actions/add_replace.go +++ b/cmd/hostctl/actions/add_replace.go @@ -1,14 +1,14 @@ package actions import ( + "context" "io" "os" - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) type addRemoveFn func(h *file.File, p *types.Profile) error @@ -93,7 +93,8 @@ func getProfileFromInput(in io.Reader, from string) (*types.Profile, error) { r = in case isValidURL(from): - r, err = readerFromURL(from) + ctx := context.Background() + r, err = readerFromURL(ctx, from) default: r, err = os.Open(from) diff --git a/cmd/hostctl/actions/backup.go b/cmd/hostctl/actions/backup.go index ba82cff..6288181 100644 --- a/cmd/hostctl/actions/backup.go +++ b/cmd/hostctl/actions/backup.go @@ -1,11 +1,9 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) func newBackupCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/completion.go b/cmd/hostctl/actions/completion.go index 4686cbd..da7804c 100644 --- a/cmd/hostctl/actions/completion.go +++ b/cmd/hostctl/actions/completion.go @@ -7,7 +7,7 @@ import ( ) func newCompletionCmd(rootCmd *cobra.Command) *cobra.Command { - var completionCmd = &cobra.Command{ + completionCmd := &cobra.Command{ Use: "completion ", Short: "Generate bash or zsh completion script", Hidden: true, diff --git a/cmd/hostctl/actions/enable_disable.go b/cmd/hostctl/actions/enable_disable.go index be62107..5571aa0 100644 --- a/cmd/hostctl/actions/enable_disable.go +++ b/cmd/hostctl/actions/enable_disable.go @@ -1,9 +1,8 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) type enableDisableFn func(h *file.File, profiles []string, only, all bool) error diff --git a/cmd/hostctl/actions/errors.go b/cmd/hostctl/actions/errors.go index 00c0475..e529450 100644 --- a/cmd/hostctl/actions/errors.go +++ b/cmd/hostctl/actions/errors.go @@ -5,15 +5,15 @@ import ( ) var ( - // ErrIncompatibleAllFlag when you can't use --all flag + // ErrIncompatibleAllFlag when you can't use --all flag. ErrIncompatibleAllFlag = errors.New("args must be empty with --all flag") - // ErrMultipleProfiles when you can use only a single profile + // ErrMultipleProfiles when you can use only a single profile. ErrMultipleProfiles = errors.New("specify only one profile") - // ErrEmptyProfiles when trying to update empty profile list + // ErrEmptyProfiles when trying to update empty profile list. ErrEmptyProfiles = errors.New("there are no profiles") - // ErrReadingFile when a file can't be read + // ErrReadingFile when a file can't be read. ErrReadingFile = errors.New("error reading data from file") ) diff --git a/cmd/hostctl/actions/gen_md_docs.go b/cmd/hostctl/actions/gen_md_docs.go index 5b1cde4..c210440 100644 --- a/cmd/hostctl/actions/gen_md_docs.go +++ b/cmd/hostctl/actions/gen_md_docs.go @@ -14,6 +14,8 @@ import ( "github.com/spf13/cobra/doc" ) +var errPathNotDirectory = errors.New("path should be a directory") + func newGenMdDocsCmd(rootCmd *cobra.Command) *cobra.Command { return &cobra.Command{ Use: "gen-md-docs", @@ -31,7 +33,7 @@ Creates full markdown documentation and store it on the given path. } if !s.IsDir() { - return errors.New("path should be a directory") + return errPathNotDirectory } return nil @@ -55,7 +57,7 @@ Creates full markdown documentation and store it on the given path. fname := path.Base(src) dst := strings.Replace(src, "hostctl_", "", 1) - if fname == "hostctl.md" { + if fname == "hostctl.md" { // nolint: goconst dst = path.Join(output, "_index.md") err = fixContent(rootCmd.Long, src) } else { @@ -92,7 +94,7 @@ func fixContent(desc, dst string) error { wrapped := fmt.Sprintf("```\n%s\n```\n", desc) final := strings.Replace(string(data), desc, wrapped, 1) - return ioutil.WriteFile(dst, []byte(final), 0666) + return ioutil.WriteFile(dst, []byte(final), 0666) // nolint: gosec } func filePrepender(filename string) string { diff --git a/cmd/hostctl/actions/helpers.go b/cmd/hostctl/actions/helpers.go index 88bdb18..2227852 100644 --- a/cmd/hostctl/actions/helpers.go +++ b/cmd/hostctl/actions/helpers.go @@ -2,6 +2,7 @@ package actions import ( "bytes" + "context" "io" "io/ioutil" "net/http" @@ -9,10 +10,9 @@ import ( "os" "runtime" - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/render" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) func commonCheckProfileOnly(_ *cobra.Command, args []string) error { @@ -58,7 +58,7 @@ func commonCheckArgs(_ *cobra.Command, args []string) error { return nil } -// isPiped detect if there is any input through STDIN +// isPiped detect if there is any input through STDIN. func isPiped() bool { info, err := os.Stdin.Stat() if err != nil { @@ -82,7 +82,7 @@ func containsDefault(args []string) error { func getDefaultHostFile() string { if runtime.GOOS == "linux" { - return "/etc/hosts" + return "/etc/hosts" // nolint: goconst } envHostFile := os.Getenv("HOSTCTL_FILE") @@ -112,11 +112,17 @@ func isValidURL(s string) bool { return true } -func readerFromURL(url string) (io.Reader, error) { - resp, err := http.Get(url) // nolint:gosec +func readerFromURL(ctx context.Context, url string) (io.Reader, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } + defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) @@ -141,6 +147,7 @@ func getRenderer(cmd *cobra.Command, opts *render.TableRendererOptions) types.Re opts.Writer = cmd.OutOrStdout() } + // nolint: goconst switch { case raw || out == "raw": return render.NewRawRenderer(opts) diff --git a/cmd/hostctl/actions/helpers_test.go b/cmd/hostctl/actions/helpers_test.go index da8e4c1..53f7921 100644 --- a/cmd/hostctl/actions/helpers_test.go +++ b/cmd/hostctl/actions/helpers_test.go @@ -2,16 +2,16 @@ package actions import ( "bytes" + "context" "net/http" "net/http/httptest" "sync" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/render" "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestContainsDefault(t *testing.T) { @@ -113,7 +113,8 @@ func TestReadFromURL(t *testing.T) { }) defer server.Close() - r, err := readerFromURL(server.URL) + ctx := context.Background() + r, err := readerFromURL(ctx, server.URL) assert.NoError(t, err) p, err := parser.ParseProfile(r) diff --git a/cmd/hostctl/actions/list.go b/cmd/hostctl/actions/list.go index e7d9df7..25801de 100644 --- a/cmd/hostctl/actions/list.go +++ b/cmd/hostctl/actions/list.go @@ -3,10 +3,9 @@ package actions import ( "fmt" - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) func newListCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/post_action.go b/cmd/hostctl/actions/post_action.go index 06e4793..f026a35 100644 --- a/cmd/hostctl/actions/post_action.go +++ b/cmd/hostctl/actions/post_action.go @@ -8,9 +8,8 @@ import ( "syscall" "time" - "github.com/spf13/cobra" - "github.com/guumaster/cligger" + "github.com/spf13/cobra" ) const longWaitTime = 999999 diff --git a/cmd/hostctl/actions/remove.go b/cmd/hostctl/actions/remove.go index 578153f..24b91da 100644 --- a/cmd/hostctl/actions/remove.go +++ b/cmd/hostctl/actions/remove.go @@ -3,11 +3,9 @@ package actions import ( "strings" - "github.com/spf13/cobra" - "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) func newRemoveCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/remove_domains.go b/cmd/hostctl/actions/remove_domains.go index 3694624..ca0157b 100644 --- a/cmd/hostctl/actions/remove_domains.go +++ b/cmd/hostctl/actions/remove_domains.go @@ -3,11 +3,9 @@ package actions import ( "strings" - "github.com/spf13/cobra" - "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) func newRemoveDomainsCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/restore.go b/cmd/hostctl/actions/restore.go index ed99085..e9e2daf 100644 --- a/cmd/hostctl/actions/restore.go +++ b/cmd/hostctl/actions/restore.go @@ -1,11 +1,9 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) func newRestoreCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/root.go b/cmd/hostctl/actions/root.go index b612a4e..33b0e56 100644 --- a/cmd/hostctl/actions/root.go +++ b/cmd/hostctl/actions/root.go @@ -3,17 +3,15 @@ package actions import ( "os" - "github.com/spf13/cobra" - "github.com/guumaster/cligger" + "github.com/spf13/cobra" ) -// nolint:gochecknoglobals var ( version = "dev" ) -// NewRootCmd creates the base command for hostctl +// NewRootCmd creates the base command for hostctl. func NewRootCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "hostctl", diff --git a/cmd/hostctl/actions/status.go b/cmd/hostctl/actions/status.go index 99c7e4e..54898b9 100644 --- a/cmd/hostctl/actions/status.go +++ b/cmd/hostctl/actions/status.go @@ -1,11 +1,10 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/render" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) func newStatusCmd() *cobra.Command { diff --git a/cmd/hostctl/actions/sync_docker.go b/cmd/hostctl/actions/sync_docker.go index de348dd..ed581f6 100644 --- a/cmd/hostctl/actions/sync_docker.go +++ b/cmd/hostctl/actions/sync_docker.go @@ -1,12 +1,11 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/docker" "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) func newSyncDockerCmd(removeCmd *cobra.Command, optsFn getOptionsFn) *cobra.Command { diff --git a/cmd/hostctl/actions/sync_docker_compose.go b/cmd/hostctl/actions/sync_docker_compose.go index 03a3bdd..e058bf9 100644 --- a/cmd/hostctl/actions/sync_docker_compose.go +++ b/cmd/hostctl/actions/sync_docker_compose.go @@ -7,12 +7,11 @@ import ( "regexp" "strings" - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/docker" "github.com/guumaster/hostctl/pkg/file" "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/cobra" ) type composeInfo struct { diff --git a/cmd/hostctl/actions/sync_docker_test.go b/cmd/hostctl/actions/sync_docker_test.go index 4dc5fa8..c981987 100644 --- a/cmd/hostctl/actions/sync_docker_test.go +++ b/cmd/hostctl/actions/sync_docker_test.go @@ -7,10 +7,9 @@ import ( "testing" "github.com/docker/docker/client" + "github.com/guumaster/hostctl/pkg/docker" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" - - "github.com/guumaster/hostctl/pkg/docker" ) func testGetOptions(t *testing.T, cli *client.Client) getOptionsFn { diff --git a/cmd/hostctl/actions/toggle.go b/cmd/hostctl/actions/toggle.go index 9a50377..544fcd7 100644 --- a/cmd/hostctl/actions/toggle.go +++ b/cmd/hostctl/actions/toggle.go @@ -1,9 +1,8 @@ package actions import ( - "github.com/spf13/cobra" - "github.com/guumaster/hostctl/pkg/file" + "github.com/spf13/cobra" ) func newToggleCmd() *cobra.Command { diff --git a/cmd/hostctl/main.go b/cmd/hostctl/main.go index 0a24363..a7bd6a8 100644 --- a/cmd/hostctl/main.go +++ b/cmd/hostctl/main.go @@ -5,13 +5,11 @@ import ( "os" "github.com/guumaster/cligger" - "github.com/guumaster/hostctl/cmd/hostctl/actions" ) func main() { _, err := os.Getwd() - if err != nil { cligger.Fatal("error: %w\n", err) } diff --git a/pkg/docker/compose.go b/pkg/docker/compose.go index 1a71097..651ffc6 100644 --- a/pkg/docker/compose.go +++ b/pkg/docker/compose.go @@ -8,17 +8,17 @@ import ( "gopkg.in/yaml.v2" ) -// ComposeData represents data in a docker-compose.yml file +// ComposeData represents data in a docker-compose.yml file. type composeData struct { Services map[string]composeService `yaml:"services"` } -// ComposeService represents one service from a yml file +// ComposeService represents one service from a yml file. type composeService struct { ContainerName string `yaml:"container_name"` } -// ParseComposeFile returns a list of containers from a docker-compose.yml file +// ParseComposeFile returns a list of containers from a docker-compose.yml file. func ParseComposeFile(r io.Reader, projectName string) ([]string, error) { bytes, err := ioutil.ReadAll(r) if err != nil { diff --git a/pkg/docker/compose_test.go b/pkg/docker/compose_test.go index 29306fc..51c0243 100644 --- a/pkg/docker/compose_test.go +++ b/pkg/docker/compose_test.go @@ -9,7 +9,7 @@ import ( ) func TestParseComposeFile(t *testing.T) { - var composeFile = ` + composeFile := ` version: "3" services: diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go index 523a46e..ddc88b7 100644 --- a/pkg/docker/docker.go +++ b/pkg/docker/docker.go @@ -8,11 +8,10 @@ import ( dtypes "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" - "github.com/guumaster/hostctl/pkg/types" ) -// Options contains parameters to sync with docker and docker-compose +// Options contains parameters to sync with docker and docker-compose. type Options struct { Domain string Network string @@ -23,7 +22,7 @@ type Options struct { Cli *client.Client } -// GetContainerList returns a list of running docker containers, filter by network if networkID passed +// GetContainerList returns a list of running docker containers, filter by network if networkID passed. func GetContainerList(opts *Options) ([]dtypes.Container, error) { var ( networkID string @@ -56,7 +55,7 @@ func GetContainerList(opts *Options) ([]dtypes.Container, error) { return opts.Cli.ContainerList(ctx, dtypes.ContainerListOptions{Filters: f}) } -// GetNetworkID returns the an ID that match a network name +// GetNetworkID returns the an ID that match a network name. func GetNetworkID(ctx context.Context, cli *client.Client, network string) (string, error) { var networkID string diff --git a/pkg/docker/docker_test.go b/pkg/docker/docker_test.go index 25d08dd..3d3a41e 100644 --- a/pkg/docker/docker_test.go +++ b/pkg/docker/docker_test.go @@ -10,9 +10,8 @@ import ( dtypes "github.com/docker/docker/api/types" "github.com/docker/docker/client" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestNew(t *testing.T) { diff --git a/pkg/file/add.go b/pkg/file/add.go index 698eb72..c587640 100644 --- a/pkg/file/add.go +++ b/pkg/file/add.go @@ -4,7 +4,7 @@ import ( "github.com/guumaster/hostctl/pkg/types" ) -// AddProfile adds a profile to the list +// AddProfile adds a profile to the list. func (f *File) AddProfile(p *types.Profile) error { if p.Name == types.Default { return types.ErrDefaultProfile diff --git a/pkg/file/add_test.go b/pkg/file/add_test.go index 94b58aa..b63107e 100644 --- a/pkg/file/add_test.go +++ b/pkg/file/add_test.go @@ -4,10 +4,9 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_AddProfile(t *testing.T) { diff --git a/pkg/file/enable_disable.go b/pkg/file/enable_disable.go index a03e067..a6d5831 100644 --- a/pkg/file/enable_disable.go +++ b/pkg/file/enable_disable.go @@ -28,12 +28,12 @@ func (f *File) DisableAll() error { return f.changeTo(f.GetProfileNames(), types.Disabled) } -// DisableOnly marks profiles as disable and enable all other profiles +// DisableOnly marks profiles as disable and enable all other profiles. func (f *File) DisableOnly(profiles []string) error { return f.changeToSplitted(profiles, types.Disabled) } -// EnableOnly marks profiles as enable and disable all other profiles +// EnableOnly marks profiles as enable and disable all other profiles. func (f *File) EnableOnly(profiles []string) error { return f.changeToSplitted(profiles, types.Enabled) } @@ -61,6 +61,7 @@ func invertStatus(s types.Status) types.Status { return types.Enabled } + func (f *File) changeTo(profiles []string, status types.Status) error { for _, name := range profiles { if name == types.Default { diff --git a/pkg/file/enable_disable_test.go b/pkg/file/enable_disable_test.go index 91a79f5..15a4ae9 100644 --- a/pkg/file/enable_disable_test.go +++ b/pkg/file/enable_disable_test.go @@ -3,9 +3,8 @@ package file import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_EnableDisable(t *testing.T) { diff --git a/pkg/file/file.go b/pkg/file/file.go index 93fac5b..85b22a5 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -7,13 +7,12 @@ import ( "os" "sync" - "github.com/spf13/afero" - "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/afero" ) -// File container to handle a hosts file +// File container to handle a hosts file. type File struct { fs afero.Fs src afero.File @@ -22,12 +21,12 @@ type File struct { mutex sync.Mutex } -// NewFile creates a new File from the given src on default OS filesystem +// NewFile creates a new File from the given src on default OS filesystem. func NewFile(src string) (*File, error) { return NewWithFs(src, afero.NewOsFs()) } -// NewWithFs creates a new File with src and an existing filesystem +// NewWithFs creates a new File with src and an existing filesystem. func NewWithFs(src string, fs afero.Fs) (*File, error) { if fs == nil { fs = afero.NewOsFs() @@ -51,7 +50,7 @@ func NewWithFs(src string, fs afero.Fs) (*File, error) { return f, nil } -// GetStatus returns a map with the status of the given profiles +// GetStatus returns a map with the status of the given profiles. func (f *File) GetStatus(profiles []string) map[string]types.Status { st := map[string]types.Status{} @@ -67,7 +66,7 @@ func (f *File) GetStatus(profiles []string) map[string]types.Status { return st } -// GetEnabled returns a list of profiles that are Enabled +// GetEnabled returns a list of profiles that are Enabled. func (f *File) GetEnabled() []string { enabled := []string{} @@ -80,7 +79,7 @@ func (f *File) GetEnabled() []string { return enabled } -// GetDisabled returns a list of profiles that are Enabled +// GetDisabled returns a list of profiles that are Enabled. func (f *File) GetDisabled() []string { disabled := []string{} @@ -93,7 +92,7 @@ func (f *File) GetDisabled() []string { return disabled } -// GetProfile return a Profile from the list +// GetProfile return a Profile from the list. func (f *File) GetProfile(name string) (*types.Profile, error) { p, ok := f.data.Profiles[name] if !ok { @@ -103,17 +102,17 @@ func (f *File) GetProfile(name string) (*types.Profile, error) { return p, nil } -// GetProfileNames return a list of all profile names +// GetProfileNames return a list of all profile names. func (f *File) GetProfileNames() []string { return f.data.ProfileNames } -// AddRoute adds a single route information to a given profile +// AddRoute adds a single route information to a given profile. func (f *File) AddRoute(name string, route *types.Route) error { return f.AddRoutes(name, []*types.Route{route}) } -// AddRoutes adds routes information to a given profile +// AddRoutes adds routes information to a given profile. func (f *File) AddRoutes(name string, routes []*types.Route) error { p, err := f.GetProfile(name) if err != nil && !errors.Is(err, types.ErrUnknownProfile) { @@ -159,7 +158,7 @@ func (f *File) RemoveHostnames(name string, routes []string) (bool, error) { return false, nil } -// WriteTo overwrite file with hosts info +// WriteTo overwrite file with hosts info. func (f *File) WriteTo(src string) error { h, err := f.fs.OpenFile(src, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644) if err != nil { @@ -169,7 +168,7 @@ func (f *File) WriteTo(src string) error { return f.writeToFile(h) } -// Flush overwrite file with hosts info +// Flush overwrite file with hosts info. func (f *File) Flush() error { h, err := f.fs.OpenFile(f.src.Name(), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644) if err != nil { @@ -180,7 +179,7 @@ func (f *File) Flush() error { return f.writeToFile(h) } -// writeToFile overwrite file with hosts info +// writeToFile overwrite file with hosts info. func (f *File) writeToFile(dst afero.File) error { f.mutex.Lock() defer f.mutex.Unlock() @@ -226,7 +225,7 @@ func (f *File) writeBanner(w io.StringWriter) { f.hasBanner = true } -// Close closes the underlying file +// Close closes the underlying file. func (f *File) Close() { f.src.Close() } diff --git a/pkg/file/file_backup.go b/pkg/file/file_backup.go index 2f5f127..2e31729 100644 --- a/pkg/file/file_backup.go +++ b/pkg/file/file_backup.go @@ -7,7 +7,7 @@ import ( "time" ) -// Backup creates a copy of your hosts file to a new location with the date as extension +// Backup creates a copy of your hosts file to a new location with the date as extension. func (f *File) Backup(dst string) (string, error) { _, _ = f.src.Seek(0, io.SeekStart) bkpFilename := fmt.Sprintf("%s.%s", f.src.Name(), time.Now().UTC().Format("20060102")) diff --git a/pkg/file/file_test.go b/pkg/file/file_test.go index 7041f7e..8f15204 100644 --- a/pkg/file/file_test.go +++ b/pkg/file/file_test.go @@ -6,11 +6,10 @@ import ( "strings" "testing" - "github.com/spf13/afero" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" ) // nolint:gochecknoglobals @@ -133,7 +132,7 @@ func TestManagerRoutes(t *testing.T) { assert.NoError(t, err) assert.Contains(t, string(c), onlyEnabled) - var added = ` + added := ` # profile.off profile2 # 127.0.0.1 first.loc # 127.0.0.1 second.loc @@ -162,7 +161,7 @@ func TestManagerRoutes(t *testing.T) { assert.NoError(t, err) assert.Contains(t, string(c), onlyEnabled) - var added = ` + added := ` # profile.on awesome 3.3.3.4 host1.loc 3.3.3.4 host2.loc @@ -191,7 +190,7 @@ func TestManagerRoutes(t *testing.T) { assert.NoError(t, err) assert.Contains(t, string(c), onlyEnabled) - var added = ` + added := ` # profile.off profile2 # 127.0.0.1 first.loc # end diff --git a/pkg/file/list.go b/pkg/file/list.go index 5453aaf..96c5d15 100644 --- a/pkg/file/list.go +++ b/pkg/file/list.go @@ -26,7 +26,7 @@ func includeProfile(needle string, stack []string) bool { return false } -// ProfileStatus shows a table only with profile names status +// ProfileStatus shows a table only with profile names status. func (f *File) ProfileStatus(r types.Renderer, profiles []string) { for _, name := range f.data.ProfileNames { currProfile := f.data.Profiles[name] @@ -44,7 +44,7 @@ func (f *File) ProfileStatus(r types.Renderer, profiles []string) { _ = r.Render() } -// List shows a table with profile names status and routing information +// List shows a table with profile names status and routing information. func (f *File) List(r types.Renderer, opts *ListOptions) { addDefault(f, r, opts) diff --git a/pkg/file/list_test.go b/pkg/file/list_test.go index 85c924b..e0d1c75 100644 --- a/pkg/file/list_test.go +++ b/pkg/file/list_test.go @@ -5,10 +5,9 @@ import ( "regexp" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/render" "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_List(t *testing.T) { diff --git a/pkg/file/merge.go b/pkg/file/merge.go index e82f769..77fe53f 100644 --- a/pkg/file/merge.go +++ b/pkg/file/merge.go @@ -4,7 +4,7 @@ import ( "github.com/guumaster/hostctl/pkg/types" ) -// MergeFile joins new content with existing content +// MergeFile joins new content with existing content. func (f *File) MergeFile(from *File) { ps := make([]*types.Profile, len(from.data.Profiles)) for i, name := range from.data.ProfileNames { @@ -14,7 +14,7 @@ func (f *File) MergeFile(from *File) { f.MergeProfiles(ps) } -// MergeProfiles joins new profiles with existing content +// MergeProfiles joins new profiles with existing content. func (f *File) MergeProfiles(profiles []*types.Profile) { for _, newP := range profiles { newName := newP.Name diff --git a/pkg/file/merge_test.go b/pkg/file/merge_test.go index 30a8c6b..d5a9543 100644 --- a/pkg/file/merge_test.go +++ b/pkg/file/merge_test.go @@ -4,9 +4,8 @@ import ( "net" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_MergeFile(t *testing.T) { diff --git a/pkg/file/remove.go b/pkg/file/remove.go index c8c62d1..9095833 100644 --- a/pkg/file/remove.go +++ b/pkg/file/remove.go @@ -4,7 +4,7 @@ import ( "github.com/guumaster/hostctl/pkg/types" ) -// RemoveProfiles removes given profiles from the list +// RemoveProfiles removes given profiles from the list. func (f *File) RemoveProfiles(profiles []string) error { for _, p := range profiles { err := f.RemoveProfile(p) @@ -16,7 +16,7 @@ func (f *File) RemoveProfiles(profiles []string) error { return nil } -// RemoveProfile removes given profile from the list +// RemoveProfile removes given profile from the list. func (f *File) RemoveProfile(name string) error { var names []string diff --git a/pkg/file/remove_test.go b/pkg/file/remove_test.go index ac9e9d8..a7eec96 100644 --- a/pkg/file/remove_test.go +++ b/pkg/file/remove_test.go @@ -3,9 +3,8 @@ package file import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_RemoveProfile(t *testing.T) { diff --git a/pkg/file/replace.go b/pkg/file/replace.go index 27e7d51..d4005b5 100644 --- a/pkg/file/replace.go +++ b/pkg/file/replace.go @@ -6,7 +6,7 @@ import ( "github.com/guumaster/hostctl/pkg/types" ) -// ReplaceProfile removes previous profile with same name and add new profile to the list +// ReplaceProfile removes previous profile with same name and add new profile to the list. func (f *File) ReplaceProfile(p *types.Profile) error { err := f.RemoveProfile(p.Name) if err != nil && !errors.Is(err, types.ErrUnknownProfile) { diff --git a/pkg/file/replace_test.go b/pkg/file/replace_test.go index 96827dd..998ea15 100644 --- a/pkg/file/replace_test.go +++ b/pkg/file/replace_test.go @@ -4,10 +4,9 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/parser" "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_ReplaceProfile(t *testing.T) { diff --git a/pkg/file/toggle_test.go b/pkg/file/toggle_test.go index a63c0f5..9bbb109 100644 --- a/pkg/file/toggle_test.go +++ b/pkg/file/toggle_test.go @@ -3,9 +3,8 @@ package file import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestFile_Toggle(t *testing.T) { diff --git a/pkg/file/types.go b/pkg/file/types.go index edd0ede..8e3e734 100644 --- a/pkg/file/types.go +++ b/pkg/file/types.go @@ -1,6 +1,6 @@ package file -// Banner is the mark added to hosts file +// Banner is the mark added to hosts file. const Banner = ` ################################################################## # Content under this line is handled by hostctl. DO NOT EDIT. diff --git a/pkg/parser/docker.go b/pkg/parser/docker.go index a8245b2..ccc4d1c 100644 --- a/pkg/parser/docker.go +++ b/pkg/parser/docker.go @@ -5,12 +5,11 @@ import ( "strings" dtypes "github.com/docker/docker/api/types" - "github.com/guumaster/hostctl/pkg/docker" "github.com/guumaster/hostctl/pkg/types" ) -// NewProfileFromDocker creates a new profile from docker info +// NewProfileFromDocker creates a new profile from docker info. func NewProfileFromDocker(opts *docker.Options) (*types.Profile, error) { p := &types.Profile{} @@ -24,7 +23,7 @@ func NewProfileFromDocker(opts *docker.Options) (*types.Profile, error) { return p, err } -// NewProfileFromDockerCompose creates a new profile from docker info +// NewProfileFromDockerCompose creates a new profile from docker info. func NewProfileFromDockerCompose(opts *docker.Options) (*types.Profile, error) { p := &types.Profile{} diff --git a/pkg/parser/docker_test.go b/pkg/parser/docker_test.go index 4c280b8..1114204 100644 --- a/pkg/parser/docker_test.go +++ b/pkg/parser/docker_test.go @@ -8,9 +8,8 @@ import ( "testing" "github.com/docker/docker/client" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/docker" + "github.com/stretchr/testify/assert" ) func TestNewProfileFromDocker(t *testing.T) { diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 10b0a53..20b1857 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -10,7 +10,6 @@ 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*`) @@ -19,7 +18,7 @@ var ( endingComment = regexp.MustCompile(`(.[^#]*).*`) ) -// Parser is the interface for content parsers +// Parser is the interface for content parsers. type Parser interface { Parse(reader io.Reader) types.Content } @@ -161,7 +160,7 @@ func parseRouteLine(str string) (*types.Route, bool) { return &types.Route{IP: ip, HostNames: p[i+1:]}, true } -// ParseProfile creates a new profile reading lines from a reader +// ParseProfile creates a new profile reading lines from a reader. func ParseProfile(r io.Reader) (*types.Profile, error) { p := &types.Profile{} s := bufio.NewScanner(r) diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index 078ccfb..274f238 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -5,9 +5,8 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestHostFile(t *testing.T) { diff --git a/pkg/render/json.go b/pkg/render/json.go index 21d46b7..c660134 100644 --- a/pkg/render/json.go +++ b/pkg/render/json.go @@ -7,14 +7,14 @@ import ( "github.com/guumaster/hostctl/pkg/types" ) -// JSONRendererOptions contains options to render JSON content +// 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 +// JSONRenderer is the Renderer used to output JSON. type JSONRenderer struct { Type RendererType Columns []string @@ -26,7 +26,7 @@ type data struct { lines []line } -// NewJSONRenderer creates an instance of JSONRenderer +// NewJSONRenderer creates an instance of JSONRenderer. func NewJSONRenderer(opts *JSONRendererOptions) JSONRenderer { if len(opts.Columns) == 0 { opts.Columns = types.DefaultColumns @@ -40,7 +40,7 @@ func NewJSONRenderer(opts *JSONRendererOptions) JSONRenderer { } } -// AddSeparator not used on JSONRenderer +// AddSeparator not used on JSONRenderer. func (j JSONRenderer) AddSeparator() { // not used } @@ -52,7 +52,7 @@ type line struct { Host string } -// AppendRow adds a new row to the list +// AppendRow adds a new row to the list. func (j JSONRenderer) AppendRow(row *types.Row) { if row.Comment != "" { return @@ -67,7 +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 +// Render returns a JSON representation of the list content. func (j JSONRenderer) Render() error { enc := json.NewEncoder(j.w) diff --git a/pkg/render/json_test.go b/pkg/render/json_test.go index 54a81fc..d90758a 100644 --- a/pkg/render/json_test.go +++ b/pkg/render/json_test.go @@ -5,9 +5,8 @@ import ( "io/ioutil" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestNewJSONRenderer(t *testing.T) { diff --git a/pkg/render/markdown.go b/pkg/render/markdown.go index 47d28c0..e2c9a28 100644 --- a/pkg/render/markdown.go +++ b/pkg/render/markdown.go @@ -4,7 +4,7 @@ import ( "github.com/guumaster/tablewriter" ) -// NewMarkdownRenderer creates an instance of TableRenderer +// NewMarkdownRenderer creates an instance of TableRenderer. func NewMarkdownRenderer(opts *TableRendererOptions) TableRenderer { table := createTableWriter(opts) diff --git a/pkg/render/markdown_test.go b/pkg/render/markdown_test.go index 096db42..334245e 100644 --- a/pkg/render/markdown_test.go +++ b/pkg/render/markdown_test.go @@ -5,9 +5,8 @@ import ( "io/ioutil" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestNewMarkdownRenderer(t *testing.T) { diff --git a/pkg/render/raw.go b/pkg/render/raw.go index b2eb038..a82fbd4 100644 --- a/pkg/render/raw.go +++ b/pkg/render/raw.go @@ -4,7 +4,7 @@ import ( "github.com/guumaster/tablewriter" ) -// NewRawRenderer creates an instance of TableRenderer without borders +// NewRawRenderer creates an instance of TableRenderer without borders. func NewRawRenderer(opts *TableRendererOptions) TableRenderer { table := createTableWriter(opts) diff --git a/pkg/render/raw_test.go b/pkg/render/raw_test.go index ca1c798..ea5b557 100644 --- a/pkg/render/raw_test.go +++ b/pkg/render/raw_test.go @@ -6,9 +6,8 @@ import ( "regexp" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestNewRawRenderer(t *testing.T) { diff --git a/pkg/render/table.go b/pkg/render/table.go index b7c6727..b85d23a 100644 --- a/pkg/render/table.go +++ b/pkg/render/table.go @@ -4,18 +4,17 @@ import ( "io" "os" - "github.com/guumaster/tablewriter" - "github.com/guumaster/hostctl/pkg/types" + "github.com/guumaster/tablewriter" ) -// TableRendererOptions contains options to render a table +// TableRendererOptions contains options to render a table. type TableRendererOptions struct { Writer io.Writer Columns []string } -// RendererType represents all the existing renderers +// RendererType represents all the existing renderers. type RendererType string // nolint:gochecknoglobals @@ -31,7 +30,7 @@ type meta struct { Raw bool } -// TableRenderer is the Renderer used to output tables +// TableRenderer is the Renderer used to output tables. type TableRenderer struct { Type RendererType Columns []string @@ -56,7 +55,7 @@ func createTableWriter(opts *TableRendererOptions) *tablewriter.Table { return table } -// NewTableRenderer creates an instance of TableRenderer +// NewTableRenderer creates an instance of TableRenderer. func NewTableRenderer(opts *TableRendererOptions) TableRenderer { table := createTableWriter(opts) @@ -71,7 +70,7 @@ func NewTableRenderer(opts *TableRendererOptions) TableRenderer { } } -// AppendRow adds a new row to the list +// AppendRow adds a new row to the list. func (t TableRenderer) AppendRow(row *types.Row) { r := []string{} @@ -98,15 +97,15 @@ func (t TableRenderer) AppendRow(row *types.Row) { } } -// AddSeparator adds a separator line to the list +// AddSeparator adds a separator line to the list. func (t TableRenderer) AddSeparator() { if !t.meta.Raw && t.meta.Rows > 0 { t.table.AddSeparator() } } -// Render prints a table representation of row content -func (t TableRenderer) Render() error { // nolint:unparam +// Render prints a table representation of row content. +func (t TableRenderer) Render() error { // nolint: unparam if t.meta.Rows > 0 { t.table.Render() } diff --git a/pkg/render/table_test.go b/pkg/render/table_test.go index 1fd4b42..dac8f2e 100644 --- a/pkg/render/table_test.go +++ b/pkg/render/table_test.go @@ -5,9 +5,8 @@ import ( "io/ioutil" "testing" - "github.com/stretchr/testify/assert" - "github.com/guumaster/hostctl/pkg/types" + "github.com/stretchr/testify/assert" ) func TestNewTableRenderer(t *testing.T) { diff --git a/pkg/types/Route.go b/pkg/types/Route.go index b3ee402..5f86f0a 100644 --- a/pkg/types/Route.go +++ b/pkg/types/Route.go @@ -4,13 +4,13 @@ import ( "net" ) -// Route contains hostnames of all routes with the same IP +// Route contains hostnames of all routes with the same IP. type Route struct { IP net.IP HostNames []string } -// NewRoute creates an new Route +// NewRoute creates an new Route. func NewRoute(ip string, hostnames ...string) *Route { return &Route{ IP: net.ParseIP(ip), diff --git a/pkg/types/errors.go b/pkg/types/errors.go index 3988b76..107966f 100644 --- a/pkg/types/errors.go +++ b/pkg/types/errors.go @@ -5,27 +5,27 @@ import ( ) var ( - // ErrMissingProfile when the profile is mandatory + // ErrMissingProfile when the profile is mandatory. ErrMissingProfile = errors.New("missing profile name") - // ErrUnknownProfile when the profile is not present + // ErrUnknownProfile when the profile is not present. ErrUnknownProfile = errors.New("unknown profile name") - // ErrDefaultProfile when trying to edit default content + // ErrDefaultProfile when trying to edit default content. ErrDefaultProfile = errors.New("'default' profile should not be handled by hostctl") - // ErrNoContent when data to write is empty + // ErrNoContent when data to write is empty. ErrNoContent = errors.New("no content to write") - // ErrNotPresentIP when looking for an IP not contained in profile + // ErrNotPresentIP when looking for an IP not contained in profile. ErrNotPresentIP = errors.New("ip not present") - // ErrUnknownNetworkID when you pass an invalid network ID to sync docker + // ErrUnknownNetworkID when you pass an invalid network ID to sync docker. ErrUnknownNetworkID = errors.New("unknown network ID") - // ErrInvalidIP when the IP is malformed + // ErrInvalidIP when the IP is malformed. ErrInvalidIP = errors.New("invalid ip") - // ErrInvalidProfileHeader when the profile header is invalid + // ErrInvalidProfileHeader when the profile header is invalid. ErrInvalidProfileHeader = errors.New("invalid format for profile header") ) diff --git a/pkg/types/profile.go b/pkg/types/profile.go index b663e0e..443a63d 100644 --- a/pkg/types/profile.go +++ b/pkg/types/profile.go @@ -7,7 +7,9 @@ import ( "net" ) -// Profile contains all data of a single profile +const disabledPrefix = "# " + +// Profile contains all data of a single profile. type Profile struct { Name string Status Status @@ -15,12 +17,12 @@ type Profile struct { Routes map[string]*Route } -// String returns a string representation of the profile +// String returns a string representation of the profile. func (p *Profile) String() string { return fmt.Sprintf("[%s]%s", p.Status, p.Name) } -// GetStatus returns a string value of ProfileStatus +// GetStatus returns a string value of ProfileStatus. func (p *Profile) GetStatus() string { return string(p.Status) } @@ -35,12 +37,12 @@ func (p *Profile) appendIP(n string) { p.IPList = append(p.IPList, n) } -// AddRoute adds a single route to the profile +// AddRoute adds a single route to the profile. func (p *Profile) AddRoute(route *Route) { p.AddRoutes([]*Route{route}) } -// AddRoutes adds non duplicated routes to a profile +// AddRoutes adds non duplicated routes to a profile. func (p *Profile) AddRoutes(routes []*Route) { if p.Routes == nil { p.Routes = map[string]*Route{} @@ -60,7 +62,7 @@ func (p *Profile) AddRoutes(routes []*Route) { } } -// RemoveHostnames removes multiple hostnames of a profile +// RemoveHostnames removes multiple hostnames of a profile. func (p *Profile) RemoveHostnames(hostnames []string) { for _, h := range hostnames { for _, ip := range p.IPList { @@ -89,7 +91,7 @@ func (p *Profile) GetHostNames(ip string) ([]string, error) { // GetAllHostNames returns all hostnames of the profile. func (p *Profile) GetAllHostNames() []string { - var list []string + list := []string{} if p.IPList == nil { return list @@ -102,7 +104,7 @@ func (p *Profile) GetAllHostNames() []string { return list } -// Render writes the profile content to the given StringWriter +// Render writes the profile content to the given StringWriter. func (p *Profile) Render(w io.StringWriter) error { tmp := bytes.NewBufferString("") @@ -116,7 +118,7 @@ func (p *Profile) Render(w io.StringWriter) error { for _, host := range route.HostNames { prefix := "" if p.Status == Disabled { - prefix = "# " + prefix = disabledPrefix } _, err = tmp.WriteString(fmt.Sprintf("%s%s %s\n", prefix, ip, host)) @@ -138,7 +140,7 @@ func (p *Profile) Render(w io.StringWriter) error { } func uniqueStrings(xs []string) []string { - var list []string + list := []string{} keys := make(map[string]bool) @@ -154,7 +156,7 @@ func uniqueStrings(xs []string) []string { } func remove(s []string, n string) []string { - var list []string + list := []string{} for _, x := range s { if x != n { diff --git a/pkg/types/renderer.go b/pkg/types/renderer.go index daf1319..3487eda 100644 --- a/pkg/types/renderer.go +++ b/pkg/types/renderer.go @@ -2,21 +2,21 @@ package types // nolint:gochecknoglobals var ( - // DefaultColumns is the list of default columns to use when showing table list + // DefaultColumns is the list of default columns to use when showing table list. DefaultColumns = []string{"profile", "status", "ip", "domain"} - // ProfilesOnlyColumns are the columns used for profile status list + // ProfilesOnlyColumns are the columns used for profile status list. ProfilesOnlyColumns = []string{"profile", "status"} ) -// Renderer is the interface to render hosts file content +// Renderer is the interface to render hosts file content. type Renderer interface { AppendRow(row *Row) AddSeparator() Render() error } -// Row represents a line for all output types +// Row represents a line for all output types. type Row struct { Comment string Profile string diff --git a/pkg/types/types.go b/pkg/types/types.go index 5e27418..693a54f 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -7,14 +7,14 @@ import ( "strings" ) -// Content contains complete data of all profiles +// Content contains complete data of all profiles. type Content struct { DefaultProfile DefaultProfile ProfileNames []string Profiles map[string]*Profile } -// Status represents the status of a Profile +// Status represents the status of a Profile. type Status string const ( @@ -23,14 +23,14 @@ const ( // Disabled marks a profile not active on your hosts file. Disabled Status = "off" - // Default is the name of the default profile + // Default is the name of the default profile. Default = "default" ) -// DefaultProfile contains data for the default profile +// DefaultProfile contains data for the default profile. type DefaultProfile []*Row -// Render writes the default profile content to the given StringWriter +// Render writes the default profile content to the given StringWriter. func (d DefaultProfile) Render(w io.StringWriter) error { tmp := bytes.NewBufferString("")