Skip to content

Commit

Permalink
new output targets. add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 19, 2020
1 parent de6ede1 commit 95c338a
Show file tree
Hide file tree
Showing 34 changed files with 354 additions and 199 deletions.
15 changes: 7 additions & 8 deletions pkg/cmd/add_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ 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 *file.File, p *types.Profile) error
type addRemoveFn func(h *file.File, p *host.Profile) error

func newAddRemoveCmd() (*cobra.Command, *cobra.Command) {
addCmd := newAddCmd()
Expand All @@ -32,7 +31,7 @@ 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 *file.File, p *types.Profile) error {
RunE: makeAddReplace(func(h *file.File, p *host.Profile) error {
return h.AddProfile(p)
}),
}
Expand All @@ -47,7 +46,7 @@ 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 *file.File, p *types.Profile) error {
RunE: makeAddReplace(func(h *file.File, p *host.Profile) error {
return h.ReplaceProfile(p)
}),
PostRunE: postRunListOnly,
Expand All @@ -72,7 +71,7 @@ func makeAddReplace(actionFn addRemoveFn) func(cmd *cobra.Command, profiles []st
}

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

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

func getProfileFromInput(in io.Reader, from string, uniq bool) (*types.Profile, error) {
func getProfileFromInput(in io.Reader, from string, uniq bool) (*host.Profile, error) {
var (
r io.Reader
err error
Expand All @@ -104,5 +103,5 @@ func getProfileFromInput(in io.Reader, from string, uniq bool) (*types.Profile,
return nil, err
}

return profile.NewProfileFromReader(r, uniq)
return host.NewProfileFromReader(r, uniq)
}
2 changes: 1 addition & 1 deletion pkg/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func isPiped() bool {
func containsDefault(args []string) error {
for _, p := range args {
if p == "default" {
return errors.ErrDefaultProfileError
return errors.ErrDefaultProfile
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/spf13/cobra"

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

func newListCmd() *cobra.Command {
Expand Down Expand Up @@ -36,20 +36,20 @@ The "default" profile is all the content that is not handled by hostctl tool.
},
}

listCmd.AddCommand(makeListStatusCmd(types.Enabled))
listCmd.AddCommand(makeListStatusCmd(types.Disabled))
listCmd.AddCommand(makeListStatusCmd(host.Enabled))
listCmd.AddCommand(makeListStatusCmd(host.Disabled))

return listCmd
}

var makeListStatusCmd = func(status types.Status) *cobra.Command {
var makeListStatusCmd = func(status host.Status) *cobra.Command {
cmd := ""
alias := ""
switch status {
case types.Enabled:
case host.Enabled:
cmd = "enabled"
alias = "on"
case types.Disabled:
case host.Disabled:
cmd = "disabled"
alias = "off"
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/sync_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/spf13/cobra"

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

func newSyncDockerCmd(removeCmd *cobra.Command) *cobra.Command {
Expand Down Expand Up @@ -40,7 +40,7 @@ Reads from Docker the list of containers and add names and IPs to a profile in y
}

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

err = h.AddProfile(p)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/sync_docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

"github.com/spf13/cobra"

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

type composeInfo struct {
Expand All @@ -32,7 +32,7 @@ Reads from a docker-compose.yml file the list of containers and add names and I
name, _ := cmd.Flags().GetString("profile")

if name == "default" {
return errors.ErrDefaultProfileError
return errors.ErrDefaultProfile
}
return nil
},
Expand Down Expand Up @@ -84,7 +84,7 @@ Reads from a docker-compose.yml file the list of containers and add names and I
}

p.Name = name
p.Status = types.Enabled
p.Status = host.Enabled

err = h.AddProfile(p)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/host/content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package host

// Content contains complete data of all profiles
type Content struct {
DefaultProfile DefaultProfile
ProfileNames []string
Profiles map[string]*Profile
}
12 changes: 6 additions & 6 deletions pkg/host/docker/profile_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"

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

// DockerOptions contains parameters to sync with docker and docker-compose
Expand Down Expand Up @@ -53,7 +53,7 @@ func containerList(ctx context.Context, opts *Options) ([]types.Container, error
}

// NewProfileFromDocker creates a new profile from docker info
func NewProfileFromDocker(ctx context.Context, opts *Options) (*types2.Profile, error) {
func NewProfileFromDocker(ctx context.Context, opts *Options) (*host.Profile, error) {
containers, err := containerList(ctx, opts)
if err != nil {
return nil, err
Expand All @@ -67,19 +67,19 @@ func NewProfileFromDocker(ctx context.Context, opts *Options) (*types2.Profile,
}
}

p := &types2.Profile{
Routes: map[string]*types2.Route{},
p := &host.Profile{
Routes: map[string]*host.Route{},
}

return addToProfile(ctx, p, containers, composeServices, opts)
}

func addToProfile(
ctx context.Context,
profile *types2.Profile,
profile *host.Profile,
containers []types.Container,
composeServices []string,
opts *Options) (*types2.Profile, error) {
opts *Options) (*host.Profile, error) {
networkID, err := getNetworkID(ctx, opts)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/host/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var (
// ErrUnknownProfile when the profile is not present
ErrUnknownProfile = errors.New("unknown profile name")

// ErrDefaultProfileError when trying to edit default content
ErrDefaultProfileError = errors.New("'default' profile should not be handled by hostctl")
// 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 = errors.New("no content to write")
Expand Down
10 changes: 5 additions & 5 deletions pkg/host/file/add.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package file

import (
"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/errors"
"github.com/guumaster/hostctl/pkg/host/types"
)

// AddProfile adds a profile to the list
func (f *File) AddProfile(p *types.Profile) error {
if p.Name == types.Default {
return errors.ErrDefaultProfileError
func (f *File) AddProfile(p *host.Profile) error {
if p.Name == host.Default {
return errors.ErrDefaultProfile
}

f.MergeProfiles([]*types.Profile{p})
f.MergeProfiles([]*host.Profile{p})

return nil
}
23 changes: 18 additions & 5 deletions pkg/host/file/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/stretchr/testify/assert"

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

func TestFile_AddProfile(t *testing.T) {
Expand All @@ -20,10 +20,10 @@ func TestFile_AddProfile(t *testing.T) {
assert.NoError(t, err)
r := strings.NewReader(`127.0.0.1 added.loc`)

p, err := profile.NewProfileFromReader(r, true)
p, err := host.NewProfileFromReader(r, true)
assert.NoError(t, err)
p.Name = "awesome"
p.Status = types.Enabled
p.Status = host.Enabled

err = m.AddProfile(p)
assert.NoError(t, err)
Expand All @@ -41,7 +41,7 @@ func TestFile_AddProfile(t *testing.T) {
assert.NoError(t, err)
r := strings.NewReader(`127.0.0.1 added.loc`)

p, err := profile.NewProfileFromReader(r, true)
p, err := host.NewProfileFromReader(r, true)
assert.NoError(t, err)
p.Name = "profile1"

Expand All @@ -57,4 +57,17 @@ func TestFile_AddProfile(t *testing.T) {

assert.Equal(t, hosts, []string{"first.loc", "second.loc", "added.loc"})
})

t.Run("Add default error", func(t *testing.T) {
m, err := NewWithFs(f.Name(), mem)
assert.NoError(t, err)
r := strings.NewReader(`127.0.0.1 added.loc`)

p, err := host.NewProfileFromReader(r, true)
assert.NoError(t, err)
p.Name = "default"

err = m.AddProfile(p)
assert.Error(t, err, errors.ErrDefaultProfile)
})
}
37 changes: 15 additions & 22 deletions pkg/host/file/enable_disable.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
package file

import (
"github.com/guumaster/hostctl/pkg/host"
"github.com/guumaster/hostctl/pkg/host/errors"
"github.com/guumaster/hostctl/pkg/host/types"
)

// Enable marks profiles as enable by uncommenting all hosts lines
// making the routing work again.
func (f *File) Enable(profiles []string) error {
return f.changeTo(profiles, types.Enabled)
return f.changeTo(profiles, host.Enabled)
}

// Disable marks profiles as disable by commenting all hosts lines.
// The content remains on the file and can be enabled later.
func (f *File) Disable(profiles []string) error {
return f.changeTo(profiles, types.Disabled)
return f.changeTo(profiles, host.Disabled)
}

// EnableAll marks all profiles as enable by uncommenting all hosts lines
// making the routing work again.
func (f *File) EnableAll() error {
return f.changeTo(f.GetProfileNames(), types.Enabled)
return f.changeTo(f.GetProfileNames(), host.Enabled)
}

// DisableAll marks all profiles as disable by commenting all hosts lines.
// The content remains on the file and can be enabled later.
func (f *File) DisableAll() error {
return f.changeTo(f.GetProfileNames(), types.Disabled)
return f.changeTo(f.GetProfileNames(), host.Disabled)
}

// DisableOnly marks profiles as disable and enable all other profiles
func (f *File) DisableOnly(profiles []string) error {
return f.changeToSplitted(profiles, types.Disabled)
return f.changeToSplitted(profiles, host.Disabled)
}

// EnableOnly marks profiles as enable and disable all other profiles
func (f *File) EnableOnly(profiles []string) error {
return f.changeToSplitted(profiles, types.Enabled)
return f.changeToSplitted(profiles, host.Enabled)
}

func (f *File) changeToSplitted(profiles []string, status types.Status) error {
func (f *File) changeToSplitted(profiles []string, status host.Status) error {
for _, name := range f.data.ProfileNames {
if name == types.Default {
continue
}

p, ok := f.data.Profiles[name]
if !ok {
return errors.ErrUnknownProfile
}
p := f.data.Profiles[name]

if contains(profiles, name) {
p.Status = status
Expand All @@ -62,16 +55,16 @@ func (f *File) changeToSplitted(profiles []string, status types.Status) error {
return nil
}

func invertStatus(s types.Status) types.Status {
if s == types.Enabled {
return types.Disabled
func invertStatus(s host.Status) host.Status {
if s == host.Enabled {
return host.Disabled
}

return types.Enabled
return host.Enabled
}
func (f *File) changeTo(profiles []string, status types.Status) error {
func (f *File) changeTo(profiles []string, status host.Status) error {
for _, name := range profiles {
if name == types.Default {
if name == host.Default {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/host/file/enable_disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestFile_Enable(t *testing.T) {

t.Run("Enable Only", func(t *testing.T) {
err = m.Enable([]string{"profile1", "profile2"})
err = m.EnableOnly([]string{"profile2"})
err = m.EnableOnly([]string{"default", "profile2"})
assert.NoError(t, err)
assert.Contains(t, m.GetEnabled(), "profile2")
assert.Contains(t, m.GetDisabled(), "profile1")
Expand Down
Loading

0 comments on commit 95c338a

Please sign in to comment.