@@ -9,13 +9,36 @@ import (
99 "regexp"
1010)
1111
12+ const startMessageTemplate = `##>>> %s ## START >>>##`
13+ const endMessageTemplate = `##<<< %s ## END <<<##`
14+
15+ func IsHostsChanged (comment , newHostsFilePath , systemHostsFilePath string ) (changed bool , err error ) {
16+ var oldHostsBuff bytes.Buffer
17+ var newHosts []byte
18+ startMessage := fmt .Sprintf (startMessageTemplate , comment )
19+ endMessage := fmt .Sprintf (endMessageTemplate , comment )
20+
21+ if _ , oldHostsBuff , err = loadHostsFile (systemHostsFilePath , startMessage , endMessage ); err != nil {
22+ return changed , err
23+ }
24+ if newHosts , err = os .ReadFile (newHostsFilePath ); nil != err {
25+ return changed , err
26+ }
27+
28+ oldHosts := bytes .TrimSpace (oldHostsBuff .Bytes ())
29+ newHosts = bytes .TrimSpace (newHosts )
30+ newHosts = bytes .ReplaceAll (newHosts , []byte ("\r \n " ), []byte ("\n " ))
31+
32+ return ! bytes .Equal (oldHosts , newHosts ), nil
33+ }
34+
1235func UpdateHostsFile (comment , newHostsFilePath , systemHostsFilePath string ) (err error ) {
1336 var newHosts []byte
1437 var buff bytes.Buffer
15- startMessage := fmt .Sprintf (`##>>> %s ## START >>>##` , comment )
16- endMessage := fmt .Sprintf (`##<<< %s ## END <<<##` , comment )
38+ startMessage := fmt .Sprintf (startMessageTemplate , comment )
39+ endMessage := fmt .Sprintf (endMessageTemplate , comment )
1740
18- if buff , err = loadHostsFile (systemHostsFilePath , startMessage , endMessage ); err != nil {
41+ if buff , _ , err = loadHostsFile (systemHostsFilePath , startMessage , endMessage ); err != nil {
1942 return err
2043 }
2144
@@ -37,14 +60,14 @@ func UpdateHostsFile(comment, newHostsFilePath, systemHostsFilePath string) (err
3760 return os .WriteFile (systemHostsFilePath , buff .Bytes (), 0644 )
3861}
3962
40- func loadHostsFile (filepath , startMessage , endMessage string ) (buff bytes.Buffer , err error ) {
63+ func loadHostsFile (filepath , startMessage , endMessage string ) (buff bytes.Buffer , oldBuff bytes. Buffer , err error ) {
4164 if _ , err = os .Stat (filepath ); errors .Is (err , os .ErrNotExist ) {
42- return buff , fmt .Errorf ("the hosts file %q is not found" , filepath )
65+ return buff , oldBuff , fmt .Errorf ("the hosts file %q is not found" , filepath )
4366 }
4467
4568 var file * os.File
4669 if file , err = os .Open (filepath ); nil != err {
47- return buff , err
70+ return buff , oldBuff , err
4871 }
4972
5073 hostsFileScanner := bufio .NewScanner (file )
@@ -70,12 +93,15 @@ func loadHostsFile(filepath, startMessage, endMessage string) (buff bytes.Buffer
7093 if (false == hasStart && false == hasEnd ) || (true == hasStart && true == hasEnd ) {
7194 buff .WriteString (line )
7295 buff .WriteRune ('\n' )
96+ } else {
97+ oldBuff .WriteString (line )
98+ oldBuff .WriteRune ('\n' )
7399 }
74100 }
75101
76102 if err = file .Close (); nil != err {
77- return buff , err
103+ return buff , oldBuff , err
78104 }
79105
80- return buff , nil
106+ return buff , oldBuff , nil
81107}
0 commit comments