Skip to content

Commit

Permalink
add readFromUrl feature. add output for json and markdown.
Browse files Browse the repository at this point in the history
refactor code organization.
Closes #59
Closes #62
  • Loading branch information
guumaster committed Apr 18, 2020
1 parent 240a83a commit f048f62
Show file tree
Hide file tree
Showing 65 changed files with 1,311 additions and 899 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ linters:
disable:
- goimports
- gochecknoglobals
- gosec
- prealloc
enable-all: true

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/docker/docker v1.13.1
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/guumaster/tablewriter v0.0.6
github.com/guumaster/tablewriter v0.0.9
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/guumaster/tablewriter v0.0.6 h1:s2n2YeOJn8dmH9RvrHPwxVOrZjPdwzWJhT5tzJJqnUk=
github.com/guumaster/tablewriter v0.0.6/go.mod h1:9B1xy1BLPtcVAeYjC1EXPxcklqnzk7dU2c3ywGbUnKY=
github.com/guumaster/tablewriter v0.0.8 h1:bQGbe9Tx+i2CyX1+LwqBtrP/6gUowNbrnUUvyikuq1I=
github.com/guumaster/tablewriter v0.0.8/go.mod h1:9B1xy1BLPtcVAeYjC1EXPxcklqnzk7dU2c3ywGbUnKY=
github.com/guumaster/tablewriter v0.0.9 h1:qyswXhSCI1SWYH78MLApi8AfL8JsWZWAUkZLONNMiYI=
github.com/guumaster/tablewriter v0.0.9/go.mod h1:9B1xy1BLPtcVAeYjC1EXPxcklqnzk7dU2c3ywGbUnKY=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/add_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/file"
)

func newAddRemoveDomainsCmd() (*cobra.Command, *cobra.Command) {
Expand All @@ -25,7 +25,7 @@ If the profile already exists it will be added to it.`,
name := args[0]
routes := args[1:]

h, err := host.NewFile(src)
h, err := file.NewFile(src)
if err != nil {
return err
}
Expand Down
58 changes: 35 additions & 23 deletions pkg/cmd/add_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/file"
"github.com/guumaster/hostctl/pkg/host/profile"
"github.com/guumaster/hostctl/pkg/host/types"
)

type addRemoveFn func(h *host.File, p *host.Profile) error
type addRemoveFn func(h *file.File, p *types.Profile) error

func newAddRemoveCmd() (*cobra.Command, *cobra.Command) {
addCmd := newAddCmd()
Expand All @@ -30,8 +32,8 @@ func newAddCmd() *cobra.Command {
Reads from a file and set content to a profile in your hosts file.
If the profile already exists it will be added to it.`,
Args: commonCheckArgs,
RunE: makeAddReplace(func(h *host.File, p *host.Profile) error {
return h.AddProfile(*p)
RunE: makeAddReplace(func(h *file.File, p *types.Profile) error {
return h.AddProfile(p)
}),
}
}
Expand All @@ -45,46 +47,32 @@ Reads from a file and set content to a profile in your hosts file.
If the profile already exists it will be overwritten.
`,
Args: commonCheckArgs,
RunE: makeAddReplace(func(h *host.File, p *host.Profile) error {
return h.ReplaceProfile(*p)
RunE: makeAddReplace(func(h *file.File, p *types.Profile) error {
return h.ReplaceProfile(p)
}),
PostRunE: postRunListOnly,
}
}

func makeAddReplace(actionFn addRemoveFn) func(cmd *cobra.Command, profiles []string) error {
return func(cmd *cobra.Command, profiles []string) error {
var (
r io.Reader
err error
)

src, _ := cmd.Flags().GetString("host-file")
from, _ := cmd.Flags().GetString("from")
uniq, _ := cmd.Flags().GetBool("uniq")
in := cmd.InOrStdin()

if isPiped() || in != os.Stdin {
r = in
} else {
r, err = os.Open(from)
if err != nil {
return err
}
}

p, err := host.NewProfileFromReader(r, uniq)
p, err := getProfileFromInput(in, from, uniq)
if err != nil {
return err
}

h, err := host.NewFile(src)
h, err := file.NewFile(src)
if err != nil {
return err
}

p.Name = profiles[0]
p.Status = host.Enabled
p.Status = types.Enabled

err = actionFn(h, p)
if err != nil {
Expand All @@ -94,3 +82,27 @@ func makeAddReplace(actionFn addRemoveFn) func(cmd *cobra.Command, profiles []st
return h.Flush()
}
}

func getProfileFromInput(in io.Reader, from string, uniq bool) (*types.Profile, error) {
var (
r io.Reader
err error
)

switch {
case isPiped() || in != os.Stdin:
r = in

case isValidURL(from):
r, err = readerFromURL(from)

default:
r, err = os.Open(from)
}

if err != nil {
return nil, err
}

return profile.NewProfileFromReader(r, uniq)
}
4 changes: 2 additions & 2 deletions pkg/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/file"
)

func newBackupCmd() *cobra.Command {
Expand All @@ -22,7 +22,7 @@ as extension.
dst, _ := cmd.Flags().GetString("path")
quiet, _ := cmd.Flags().GetBool("quiet")

h, err := host.NewFile(src)
h, err := file.NewFile(src)
if err != nil {
return err
}
Expand Down
50 changes: 23 additions & 27 deletions pkg/cmd/enable_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,10 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/file"
)

type enableDisableFn func(h *host.File, profiles []string, only, all bool) error

var enableActionFn = func(h *host.File, profiles []string, only, all bool) error {
switch {
case only:
return h.EnableOnly(profiles)
case all:
return h.EnableAll()
default:
return h.Enable(profiles)
}
}

var disableActionFn = func(h *host.File, profiles []string, only, all bool) error {
switch {
case only:
return h.DisableOnly(profiles)
case all:
return h.DisableAll()
default:
return h.Disable(profiles)
}
}
type enableDisableFn func(h *file.File, profiles []string, only, all bool) error

func newEnableDisableCmd() (*cobra.Command, *cobra.Command) {
enableCmd := &cobra.Command{
Expand All @@ -39,7 +17,16 @@ Enables an existing profile.
It will be listed as "on" while it is enabled.
`,
Args: commonCheckArgsWithAll,
RunE: makeEnableDisable(enableActionFn),
RunE: makeEnableDisable(func(h *file.File, profiles []string, only, all bool) error {
switch {
case only:
return h.EnableOnly(profiles)
case all:
return h.EnableAll()
default:
return h.Enable(profiles)
}
}),
}

disableCmd := &cobra.Command{
Expand All @@ -50,7 +37,16 @@ Disable a profile from your hosts file without removing it.
It will be listed as "off" while it is disabled.
`,
Args: commonCheckArgsWithAll,
RunE: makeEnableDisable(disableActionFn),
RunE: makeEnableDisable(func(h *file.File, profiles []string, only, all bool) error {
switch {
case only:
return h.DisableOnly(profiles)
case all:
return h.DisableAll()
default:
return h.Disable(profiles)
}
}),
}

enableCmd.PostRunE = func(cmd *cobra.Command, args []string) error {
Expand All @@ -70,7 +66,7 @@ func makeEnableDisable(actionFn enableDisableFn) func(cmd *cobra.Command, profil
only, _ := cmd.Flags().GetBool("only")
all, _ := cmd.Flags().GetBool("all")

h, err := host.NewFile(src)
h, err := file.NewFile(src)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/enable_disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/errors"
)

func Test_Disable(t *testing.T) {
Expand Down Expand Up @@ -48,7 +48,7 @@ func Test_Disable(t *testing.T) {
cmd.SetArgs([]string{"disable", "unknown", "--host-file", tmp.Name()})

err := cmd.Execute()
assert.EqualError(t, err, host.ErrUnknownProfile.Error())
assert.EqualError(t, err, errors.ErrUnknownProfile.Error())
})

t.Run("Disable Only", func(t *testing.T) {
Expand Down Expand Up @@ -209,7 +209,7 @@ func Test_Enable(t *testing.T) {
cmd.SetArgs([]string{"enable", "unknown", "--host-file", tmp.Name()})

err := cmd.Execute()
assert.EqualError(t, err, host.ErrUnknownProfile.Error())
assert.EqualError(t, err, errors.ErrUnknownProfile.Error())
})

t.Run("Enable Only", func(t *testing.T) {
Expand Down
Loading

0 comments on commit f048f62

Please sign in to comment.