Commit 375cb4bb authored by Addshore's avatar Addshore 🏄
Browse files

dev: code to alter hosts file (not yet used)

Bug: T282337
parent 103dff07
......@@ -25,6 +25,7 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1 // indirect
github.com/txn2/txeh v1.3.0
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
gotest.tools/v3 v3.0.3 // indirect
......
......@@ -36,9 +36,11 @@ github.com/containerd/containerd v1.4.4 h1:rtRG4N6Ct7GNssATwgpvMGfnjnwfjnu/Zs9W3
github.com/containerd/containerd v1.4.4/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
......@@ -162,6 +164,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rhysd/go-github-selfupdate v1.2.3 h1:iaa+J202f+Nc+A8zi75uccC8Wg3omaM7HDeimXA22Ag=
github.com/rhysd/go-github-selfupdate v1.2.3/go.mod h1:mp/N8zj6jFfBQy/XMYoWsmfzxazpPAODuqarmPDe2Rg=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
......@@ -174,11 +177,13 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
......@@ -191,7 +196,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/txn2/txeh v1.3.0 h1:vnbv63htVMZCaQgLqVBxKvj2+HHHFUzNW7I183zjg3E=
github.com/txn2/txeh v1.3.0/go.mod h1:O7M6gUTPeMF+vsa4c4Ipx3JDkOYrruB1Wry8QRsMcw8=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
......@@ -201,6 +209,7 @@ 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-20181203042331-505ab145d0a9/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/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
......@@ -231,6 +240,7 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/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-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
......
/*Package hosts in internal utils is functionality for interacting with an etc hosts file
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hosts
import (
"io/ioutil"
"os"
"strings"
"github.com/txn2/txeh"
)
var hostsFile = ""
var hostsTmpPrefix = "mwcli-hosts-"
/*Save result of saving the hosts file*/
type Save struct {
Success bool
Content string
TmpFile string
}
/*AddHosts attempts to add requested hosts to the system hosts file, and gives you the new content, a tmp file and success*/
func AddHosts(toAdd []string) Save {
hosts := hosts()
hosts.AddHosts("127.0.0.1", toAdd)
return save(hosts)
}
/*RemoveHostsWithSuffix attempts to remove all hosts with a suffix from the system hosts file, and gives you the new content, a tmp file and success*/
func RemoveHostsWithSuffix(hostSuffix string) Save {
hosts := hosts()
removeHostsWithSuffixFromLines(hostSuffix, hosts)
return save(hosts)
}
func tmpFile() string {
tmpFile, err := ioutil.TempFile(os.TempDir(), hostsTmpPrefix)
if err != nil {
panic(err)
}
tmpFile.Close()
return tmpFile.Name()
}
func hosts() *txeh.Hosts {
hosts, err := txeh.NewHosts(&txeh.HostsConfig{
ReadFilePath: hostsFile,
WriteFilePath: hostsFile,
})
if err != nil {
panic(err)
}
return hosts
}
func removeHostsWithSuffixFromLines(hostSuffix string, hosts *txeh.Hosts) *txeh.Hosts {
lines := hosts.GetHostFileLines()
for _, line := range *lines {
for _, hostname := range line.Hostnames {
if strings.HasSuffix(hostname, hostSuffix) {
hosts.RemoveHost(hostname)
}
}
}
return hosts
}
func save(hosts *txeh.Hosts) Save {
err := hosts.Save()
if err != nil {
tmpFile := tmpFile()
err = hosts.SaveAs(tmpFile)
if err != nil {
panic(err)
}
return Save{
Success: false,
Content: hosts.RenderHostsFile(),
TmpFile: tmpFile,
}
}
return Save{
Success: true,
Content: hosts.RenderHostsFile(),
TmpFile: hosts.WriteFilePath,
}
}
/*Package hosts in internal utils is functionality for interacting with an etc hosts file
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hosts
import (
"io/ioutil"
"os"
"reflect"
"testing"
)
var singleLocalHost = "127.0.0.1 iam.localhost\n"
var singleOtherHost = "123.123.111.111 iam.not.localhost\n"
var twoHostsToRemoveOnly = "127.0.0.1 1.mwcli.test 2.mwcli.test\n"
var twoHostsToRemoveFromLocal = "127.0.0.1 iam.localhost 1.mwcli.test 2.mwcli.test\n"
func writeContentToTmpFile(content string) string {
tmpFile, err := ioutil.TempFile(os.TempDir(), hostsTmpPrefix+"test-")
if err != nil {
panic(err)
}
tmpFile.WriteString(content)
tmpFile.Close()
return tmpFile.Name()
}
func TestAddHosts(t *testing.T) {
type args struct {
toAdd []string
}
tests := []struct {
startingContent string
name string
args args
want Save
}{
{
name: "Empty: Add single: 1.mwcli.test",
startingContent: "",
args: args{
toAdd: []string{"1.mwcli.test"},
},
want: Save{
Success: true,
Content: "127.0.0.1 1.mwcli.test\n",
TmpFile: "",
},
},
{
name: "Empty: Add two: 1.mwcli.test, 2.mwcli.test",
startingContent: "",
args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
},
want: Save{
Success: true,
Content: "127.0.0.1 1.mwcli.test 2.mwcli.test\n",
TmpFile: "",
},
},
{
name: "singleLocalHost: Add two: 1.mwcli.test, 2.mwcli.test",
startingContent: singleLocalHost,
args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
},
want: Save{
Success: true,
Content: "127.0.0.1 iam.localhost 1.mwcli.test 2.mwcli.test\n",
TmpFile: "",
},
},
{
name: "singleOtherHost: Add two: 1.mwcli.test, 2.mwcli.test",
startingContent: singleOtherHost,
args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
},
want: Save{
Success: true,
Content: "123.123.111.111 iam.not.localhost\n127.0.0.1 1.mwcli.test 2.mwcli.test\n",
TmpFile: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup a test file
testFile := writeContentToTmpFile(tt.startingContent)
hostsFile = testFile
tt.want.TmpFile = testFile
// Perform the test!
if got := AddHosts(tt.args.toAdd); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AddHosts() = %v, want %v", got, tt.want)
}
})
}
}
func TestRemoveHostsWithSuffix(t *testing.T) {
type args struct {
hostSuffix string
}
tests := []struct {
startingContent string
name string
args args
want Save
}{
{
name: "Remove mwcli.test suffix, resulting in same content",
startingContent: singleLocalHost,
args: args{
hostSuffix: "mwcli.test",
},
want: Save{
Success: true,
Content: singleLocalHost,
TmpFile: "",
},
},
{
name: "Remove mwcli.test suffix, removing 2, resulting in nothing",
startingContent: twoHostsToRemoveOnly,
args: args{
hostSuffix: "mwcli.test",
},
want: Save{
Success: true,
Content: "",
TmpFile: "",
},
},
{
name: "Remove mwcli.test suffix, removing 2, resulting in 1 left",
startingContent: twoHostsToRemoveFromLocal,
args: args{
hostSuffix: "mwcli.test",
},
want: Save{
Success: true,
Content: singleLocalHost,
TmpFile: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup a test file
testFile := writeContentToTmpFile(tt.startingContent)
hostsFile = testFile
tt.want.TmpFile = testFile
// Perform the test!
if got := RemoveHostsWithSuffix(tt.args.hostSuffix); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveHostsWithSuffix() = %v, want %v", got, tt.want)
}
})
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment