Commit ea659324 authored by Addshore's avatar Addshore 🏄
Browse files

mwdd: Only save hosts file on change

https://phabricator.wikimedia.org/T292909
parent 8bdf986a
...@@ -36,7 +36,7 @@ var mwddHostsAddCmd = &cobra.Command{ ...@@ -36,7 +36,7 @@ var mwddHostsAddCmd = &cobra.Command{
Use: "add", Use: "add",
Short: "Adds development environment hosts into your system hosts file (might need sudo)", Short: "Adds development environment hosts into your system hosts file (might need sudo)",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
save := hosts.AddHosts( changeResult := hosts.AddHosts(
append( append(
[]string{ []string{
// TODO generate these by reading the yml files? // TODO generate these by reading the yml files?
...@@ -49,20 +49,7 @@ var mwddHostsAddCmd = &cobra.Command{ ...@@ -49,20 +49,7 @@ var mwddHostsAddCmd = &cobra.Command{
mwdd.DefaultForUser().UsedHosts()..., mwdd.DefaultForUser().UsedHosts()...,
), ),
) )
if save.Success { handleChangeResult(changeResult)
fmt.Println("Hosts file " + save.WriteFile + " updated!")
} else {
fmt.Println("Could not save your hosts file.")
fmt.Println("You can return with sudo.")
fmt.Println("Or edit the hosts file yourself.")
fmt.Println("Temporary file: " + save.WriteFile)
fmt.Println("")
fmt.Println(save.Content)
// Hack around https://phabricator.wikimedia.org/T292909
if os.Getenv("MWCLI_CONTEXT_TEST") == "" {
os.Exit(1)
}
}
}, },
} }
...@@ -70,24 +57,25 @@ var mwddHostsRemoveCmd = &cobra.Command{ ...@@ -70,24 +57,25 @@ var mwddHostsRemoveCmd = &cobra.Command{
Use: "remove", Use: "remove",
Short: "Removes development environment hosts from your system hosts file (might need sudo)", Short: "Removes development environment hosts from your system hosts file (might need sudo)",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
save := hosts.RemoveHostsWithSuffix("mwdd.localhost") handleChangeResult(hosts.RemoveHostsWithSuffix("mwdd.localhost"))
if save.Success {
fmt.Println("Hosts file " + save.WriteFile + " updated!")
} else {
fmt.Println("Could not save your hosts file.")
fmt.Println("You can return with sudo.")
fmt.Println("Or edit the hosts file yourself.")
fmt.Println("Temporary file: " + save.WriteFile)
fmt.Println("")
fmt.Println(save.Content)
// Hack around https://phabricator.wikimedia.org/T292909
if os.Getenv("MWCLI_CONTEXT_TEST") == "" {
os.Exit(1)
}
}
}, },
} }
func handleChangeResult(result hosts.ChangeResult) {
if result.Success && result.Altered {
fmt.Println("Hosts file altered and updated: " + result.WriteFile)
} else if result.Altered {
fmt.Println("Wanted to alter your hosts file bu could not.")
fmt.Println("You can re-run this command with sudo.")
fmt.Println("Or edit the hosts file yourself.")
fmt.Println("Temporary file: " + result.WriteFile)
fmt.Println("")
fmt.Println(result.Content)
} else {
fmt.Println("No changes needed.")
}
}
var mwddHostsWritableCmd = &cobra.Command{ var mwddHostsWritableCmd = &cobra.Command{
Use: "writable", Use: "writable",
Short: "Checks if you can write to the needed hosts file", Short: "Checks if you can write to the needed hosts file",
......
...@@ -30,15 +30,16 @@ var ( ...@@ -30,15 +30,16 @@ var (
hostsTmpPrefix = "mwcli-hosts-" hostsTmpPrefix = "mwcli-hosts-"
) )
/*Save result of saving the hosts file.*/ /*ChangeResult result of changing the hosts file.*/
type Save struct { type ChangeResult struct {
Success bool Success bool
Altered bool
Content string Content string
WriteFile string WriteFile string
} }
/*AddHosts attempts to add requested hosts to the system hosts file, and gives you the new content, a tmp file and success.*/ /*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 { func AddHosts(toAdd []string) ChangeResult {
hosts := hosts() hosts := hosts()
serviceIP := "127.0.0.1" serviceIP := "127.0.0.1"
...@@ -55,14 +56,14 @@ func AddHosts(toAdd []string) Save { ...@@ -55,14 +56,14 @@ func AddHosts(toAdd []string) Save {
// TODO when the library supports it do ipv6 too https://github.com/txn2/txeh/issues/15 // TODO when the library supports it do ipv6 too https://github.com/txn2/txeh/issues/15
// hosts.AddHosts("::1", toAdd) // hosts.AddHosts("::1", toAdd)
return save(hosts) return finishChanges(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.*/ /*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 { func RemoveHostsWithSuffix(hostSuffix string) ChangeResult {
hosts := hosts() hosts := hosts()
removeHostsWithSuffixFromLines(hostSuffix, hosts) removeHostsWithSuffixFromLines(hostSuffix, hosts)
return save(hosts) return finishChanges(hosts)
} }
/*Writable is the hosts file writable.*/ /*Writable is the hosts file writable.*/
...@@ -113,7 +114,20 @@ func removeHostsWithSuffixFromLines(hostSuffix string, hosts *txeh.Hosts) *txeh. ...@@ -113,7 +114,20 @@ func removeHostsWithSuffixFromLines(hostSuffix string, hosts *txeh.Hosts) *txeh.
return hosts return hosts
} }
func save(hosts *txeh.Hosts) Save { func finishChanges(touchedHosts *txeh.Hosts) ChangeResult {
diskHosts := hosts()
if diskHosts.RenderHostsFile() != touchedHosts.RenderHostsFile() {
return save(touchedHosts)
}
return ChangeResult{
Success: true,
Altered: false,
Content: diskHosts.RenderHostsFile(),
WriteFile: diskHosts.WriteFilePath,
}
}
func save(hosts *txeh.Hosts) ChangeResult {
err := hosts.Save() err := hosts.Save()
if err != nil { if err != nil {
tmpFile := tmpFile() tmpFile := tmpFile()
...@@ -121,15 +135,17 @@ func save(hosts *txeh.Hosts) Save { ...@@ -121,15 +135,17 @@ func save(hosts *txeh.Hosts) Save {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return Save{ return ChangeResult{
Success: false, Success: false,
Altered: true,
Content: hosts.RenderHostsFile(), Content: hosts.RenderHostsFile(),
WriteFile: tmpFile, WriteFile: tmpFile,
} }
} }
return Save{ return ChangeResult{
Success: true, Success: true,
Altered: true,
Content: hosts.RenderHostsFile(), Content: hosts.RenderHostsFile(),
WriteFile: hosts.WriteFilePath, WriteFile: hosts.WriteFilePath,
} }
......
...@@ -50,7 +50,7 @@ func TestAddHosts(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestAddHosts(t *testing.T) {
startingContent string startingContent string
name string name string
args args args args
want Save want ChangeResult
}{ }{
{ {
name: "Empty: Add single: 1.mwcli.test", name: "Empty: Add single: 1.mwcli.test",
...@@ -58,8 +58,9 @@ func TestAddHosts(t *testing.T) { ...@@ -58,8 +58,9 @@ func TestAddHosts(t *testing.T) {
args: args{ args: args{
toAdd: []string{"1.mwcli.test"}, toAdd: []string{"1.mwcli.test"},
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: "127.0.0.1 1.mwcli.test\n", Content: "127.0.0.1 1.mwcli.test\n",
WriteFile: "", WriteFile: "",
}, },
...@@ -70,8 +71,9 @@ func TestAddHosts(t *testing.T) { ...@@ -70,8 +71,9 @@ func TestAddHosts(t *testing.T) {
args: args{ args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"}, toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: "127.0.0.1 1.mwcli.test 2.mwcli.test\n", Content: "127.0.0.1 1.mwcli.test 2.mwcli.test\n",
WriteFile: "", WriteFile: "",
}, },
...@@ -82,8 +84,9 @@ func TestAddHosts(t *testing.T) { ...@@ -82,8 +84,9 @@ func TestAddHosts(t *testing.T) {
args: args{ args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"}, toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: "127.0.0.1 iam.localhost 1.mwcli.test 2.mwcli.test\n", Content: "127.0.0.1 iam.localhost 1.mwcli.test 2.mwcli.test\n",
WriteFile: "", WriteFile: "",
}, },
...@@ -94,8 +97,9 @@ func TestAddHosts(t *testing.T) { ...@@ -94,8 +97,9 @@ func TestAddHosts(t *testing.T) {
args: args{ args: args{
toAdd: []string{"1.mwcli.test", "2.mwcli.test"}, toAdd: []string{"1.mwcli.test", "2.mwcli.test"},
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: "123.123.111.111 iam.not.localhost\n127.0.0.1 1.mwcli.test 2.mwcli.test\n", Content: "123.123.111.111 iam.not.localhost\n127.0.0.1 1.mwcli.test 2.mwcli.test\n",
WriteFile: "", WriteFile: "",
}, },
...@@ -125,7 +129,7 @@ func TestRemoveHostsWithSuffix(t *testing.T) { ...@@ -125,7 +129,7 @@ func TestRemoveHostsWithSuffix(t *testing.T) {
startingContent string startingContent string
name string name string
args args args args
want Save want ChangeResult
}{ }{
{ {
name: "Remove mwcli.test suffix, resulting in same content", name: "Remove mwcli.test suffix, resulting in same content",
...@@ -133,8 +137,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) { ...@@ -133,8 +137,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) {
args: args{ args: args{
hostSuffix: "mwcli.test", hostSuffix: "mwcli.test",
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: false,
Content: singleLocalHost, Content: singleLocalHost,
WriteFile: "", WriteFile: "",
}, },
...@@ -145,8 +150,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) { ...@@ -145,8 +150,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) {
args: args{ args: args{
hostSuffix: "mwcli.test", hostSuffix: "mwcli.test",
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: "", Content: "",
WriteFile: "", WriteFile: "",
}, },
...@@ -157,8 +163,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) { ...@@ -157,8 +163,9 @@ func TestRemoveHostsWithSuffix(t *testing.T) {
args: args{ args: args{
hostSuffix: "mwcli.test", hostSuffix: "mwcli.test",
}, },
want: Save{ want: ChangeResult{
Success: true, Success: true,
Altered: true,
Content: singleLocalHost, Content: singleLocalHost,
WriteFile: "", WriteFile: "",
}, },
......
Supports Markdown
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