Skip to content

Commit

Permalink
better method naming, change from (ip, hosts) signature to ([]route).…
Browse files Browse the repository at this point in the history
… more tests.
  • Loading branch information
guumaster committed Apr 25, 2020
1 parent 9fb05bb commit 64812c3
Show file tree
Hide file tree
Showing 21 changed files with 498 additions and 230 deletions.
5 changes: 3 additions & 2 deletions cmd/hostctl/actions/add_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/guumaster/cligger"

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

func newAddRemoveDomainsCmd() (*cobra.Command, *cobra.Command) {
Expand All @@ -24,14 +25,14 @@ If the profile already exists it will be added to it.`,
ip, _ := cmd.Flags().GetString("ip")
quiet, _ := cmd.Flags().GetBool("quiet")
name := args[0]
routes := args[1:]
hostnames := args[1:]

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

err = h.AddRoutes(name, ip, routes)
err = h.AddRoute(name, types.NewRoute(ip, hostnames...))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hostctl/actions/remove_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ It cannot be undone unless you have a backup and restore it.
return err
}

removed, err := h.RemoveRoutes(name, domains)
removed, err := h.RemoveHostnames(name, domains)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/hostctl/actions/sync_docker.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package actions

import (
"context"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/file"
Expand All @@ -23,9 +21,7 @@ Reads from Docker the list of containers and add names and IPs to a profile in y
domain, _ := cmd.Flags().GetString("domain")
network, _ := cmd.Flags().GetString("network")

ctx := context.Background()

p, err := profile.NewProfileFromDocker(ctx, &profile.DockerOptions{
p, err := profile.NewProfileFromDocker(&profile.DockerOptions{
Domain: domain,
Network: network,
Cli: nil,
Expand Down
12 changes: 7 additions & 5 deletions cmd/hostctl/actions/sync_docker_compose.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package actions

import (
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -63,12 +62,15 @@ Reads from a docker-compose.yml file the list of containers and add names and I
domain = fmt.Sprintf("%s.loc", name)
}

ctx := context.Background()
f, err := os.Open(compose.File)
if err != nil {
return err
}

p, err := profile.NewProfileFromDocker(ctx, &profile.DockerOptions{
p, err := profile.NewProfileFromDockerCompose(&profile.DockerOptions{
Domain: domain,
Network: network,
ComposeFile: compose.File,
ComposeFile: f,
ProjectName: compose.ProjectName,
KeepPrefix: prefix,
Cli: nil,
Expand All @@ -85,7 +87,7 @@ Reads from a docker-compose.yml file the list of containers and add names and I
p.Name = name
p.Status = types.Enabled

err = h.AddProfile(p)
err = h.ReplaceProfile(p)
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ go 1.14
require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
github.com/docker/docker v17.12.1-ce+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/ghodss/yaml v1.0.0
github.com/guumaster/cligger v0.1.0
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
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.5.1
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v2 v2.2.2
)
26 changes: 9 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v17.12.1-ce+incompatible h1:JF3ixBk1BbHBmKGimGdei9/2mFcc2rKOReZ+nketjOI=
github.com/docker/docker v17.12.1-ce+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand All @@ -44,7 +43,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -74,21 +72,21 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -133,17 +131,15 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -154,13 +150,10 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 h1:opSr2sbRXk5X5/givKrrKj9HXxFpW2sdCiP8MJSKLQY=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -178,7 +171,6 @@ gopkg.in/gookit/color.v1 v1.1.6/go.mod h1:IcEkFGaveVShJ+j8ew+jwe9epHyGpJ9IrptHmW
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
2 changes: 1 addition & 1 deletion pkg/docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"io/ioutil"

"github.com/ghodss/yaml"
"gopkg.in/yaml.v2"
)

// ComposeData represents data in a docker-compose.yml file
Expand Down
19 changes: 12 additions & 7 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ func (f *File) GetProfileNames() []string {
return f.data.ProfileNames
}

// AddRoutes add route information to a given profile
func (f *File) AddRoutes(name, ip string, hostnames []string) error {
// 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
func (f *File) AddRoutes(name string, routes []*types.Route) error {
p, err := f.GetProfile(name)
if err != nil && !errors.Is(err, types.ErrUnknownProfile) {
return err
Expand All @@ -122,25 +127,25 @@ func (f *File) AddRoutes(name, ip string, hostnames []string) error {
Routes: map[string]*types.Route{},
}

p.AddRoutes(ip, hostnames)
p.AddRoutes(routes)

return f.AddProfile(p)
}

p.AddRoutes(ip, hostnames)
p.AddRoutes(routes)

return nil
}

// RemoveRoutes removes route information from a given types.
// RemoveHostnames removes route information from a given types.
// also removes the profile if gets empty.
func (f *File) RemoveRoutes(name string, routes []string) (bool, error) {
func (f *File) RemoveHostnames(name string, routes []string) (bool, error) {
p, err := f.GetProfile(name)
if err != nil {
return false, err
}

p.RemoveRoutes(routes)
p.RemoveHostnames(routes)

if len(p.Routes) == 0 {
err := f.RemoveProfile(p.Name)
Expand Down
32 changes: 28 additions & 4 deletions pkg/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ func TestManagerStatus(t *testing.T) {
}
assert.Equal(t, expected, actual)
})

t.Run("GetStatus unknown", func(t *testing.T) {
actual := m.GetStatus([]string{"unknown"})
expected := map[string]types.Status{}
assert.Equal(t, expected, actual)
})
})
}

func TestManagerRoutes(t *testing.T) {
t.Run("AddRoutes", func(t *testing.T) {
mem := createBasicFS(t)
Expand All @@ -55,7 +62,7 @@ func TestManagerRoutes(t *testing.T) {
hosts, err := p.GetHostNames("3.3.3.4")
assert.NoError(t, err)

err = f.AddRoutes("profile2", "3.3.3.4", hosts)
err = f.AddRoute("profile2", types.NewRoute("3.3.3.4", hosts...))
assert.NoError(t, err)

err = f.Flush()
Expand Down Expand Up @@ -86,7 +93,7 @@ func TestManagerRoutes(t *testing.T) {

h, _ := mem.OpenFile("/tmp/etc/hosts", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)

err = f.AddRoutes("awesome", "3.3.3.4", []string{"host1.loc", "host2.loc"})
err = f.AddRoute("awesome", types.NewRoute("3.3.3.4", "host1.loc", "host2.loc"))
assert.NoError(t, err)

err = f.Flush()
Expand Down Expand Up @@ -116,7 +123,7 @@ func TestManagerRoutes(t *testing.T) {

h, _ := mem.OpenFile("/tmp/etc/hosts", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)

removed, err := f.RemoveRoutes("profile2", []string{"second.loc"})
removed, err := f.RemoveHostnames("profile2", []string{"second.loc"})
assert.NoError(t, err)
assert.Equal(t, false, removed)

Expand Down Expand Up @@ -145,7 +152,7 @@ func TestManagerRoutes(t *testing.T) {

h, _ := mem.OpenFile("/tmp/etc/hosts", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)

removed, err := f.RemoveRoutes("profile2", []string{"first.loc", "second.loc"})
removed, err := f.RemoveHostnames("profile2", []string{"first.loc", "second.loc"})
assert.NoError(t, err)
assert.Equal(t, true, removed)

Expand Down Expand Up @@ -212,4 +219,21 @@ func TestManagerWrite(t *testing.T) {

assert.Contains(t, string(content), Banner)
})

t.Run("writeBanner once", func(t *testing.T) {
mem := createBasicFS(t)
h, _ := mem.OpenFile("/tmp/etc/hosts", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)

f, err := NewWithFs("/tmp/etc/hosts", mem)
assert.NoError(t, err)

f.writeBanner(h)
f.writeBanner(h)
h.Close()

content, err := afero.ReadFile(mem, h.Name())
assert.NoError(t, err)

assert.Contains(t, string(content), Banner)
})
}
18 changes: 15 additions & 3 deletions pkg/file/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import (
"github.com/guumaster/hostctl/pkg/types"
)

// MergeProfiles joins new content with existing content
// 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)
}

f.MergeProfiles(ps)
}

// MergeProfiles joins new profiles with existing content
func (f *File) MergeProfiles(profiles []*types.Profile) {
for _, newP := range profiles {
newName := newP.Name
Expand All @@ -22,11 +32,13 @@ func (f *File) MergeProfiles(profiles []*types.Profile) {
baseP.Routes = map[string]*types.Route{}
}

routes := []*types.Route{}
for _, r := range newP.Routes {
ip := r.IP.String()
baseP.AddRoutes(ip, r.HostNames)
routes = append(routes, r)
}

baseP.AddRoutes(routes)

f.data.Profiles[newName] = baseP
}
}
Loading

0 comments on commit 64812c3

Please sign in to comment.