diff --git a/src/cfg/cfg.go b/src/cfg/cfg.go index ea1e9ac..d2f623c 100644 --- a/src/cfg/cfg.go +++ b/src/cfg/cfg.go @@ -4,6 +4,7 @@ import ( "bufio" "log" "os" + "errors" ) const CFG = ` @@ -28,24 +29,37 @@ type Config struct { Sites []string Downloads []string Whitelist []string + System struct { + OS string + TmpDir string + HostsLoc string + CfgLoc string + } } +// Used to hold a list of functions to be run when building +var configFuncs []func(*Config) + // Create initialized a config to be used the entire session -func Create(cfgLoc string) (cfg Config) { - cfg.CfgLoc = cfgLoc +func Create() (err error,cfg Config) { + for _,fp := range(configFuncs){ + fp(&cfg) + } + if (cfg.System.OS == ""){return errors.New("Failed to detect the OS"), cfg} + err, cfg = cfg.Update() return } // cfgparse recieves the location of the config file and returns a list of sites to add and content to download func (cfg Config) Update() (error, Config) { - l := (cfg.CfgLoc + "rhosts.cfg") + l := (cfg.System.CfgLoc + "rhosts.cfg") var err error = nil log.Print("Opening: ", l) - if _, err = os.Stat(cfg.CfgLoc); os.IsNotExist(err) { - log.Print(cfg.CfgLoc + " Does not exist, attempting to create it") - err = os.MkdirAll(cfg.CfgLoc, 0755) + if _, err = os.Stat(cfg.System.CfgLoc); os.IsNotExist(err) { + log.Print(cfg.System.CfgLoc + " Does not exist, attempting to create it") + err = os.MkdirAll(cfg.System.CfgLoc, 0755) if err != nil { - log.Fatal("Could not create " + cfg.CfgLoc) + log.Fatal("Could not create " + cfg.System.CfgLoc) } } if _, err = os.Stat(l); os.IsNotExist(err) { diff --git a/src/cfg/linux.go b/src/cfg/linux.go new file mode 100644 index 0000000..1859124 --- /dev/null +++ b/src/cfg/linux.go @@ -0,0 +1,16 @@ +// +build linux + +package cfg + + +func init(){ + fp := func(c *Config){ + c.System.OS = "linux" + c.System.TmpDir = "/tmp/" + c.System.HostsLoc = "/etc/hosts" + c.System.CfgLoc = "/etc/rhosts/" + } + + configFuncs = append(configFuncs,fp) +} + diff --git a/src/cfg/windows.go b/src/cfg/windows.go new file mode 100644 index 0000000..1829598 --- /dev/null +++ b/src/cfg/windows.go @@ -0,0 +1,19 @@ +// +build windows + +package sys + + +func init(){ + +} + +func init(){ + fp := func(c *Config){ + c.System.os = "windows" + c.System.tmpdir = "/tmp/" + c.System.hostsloc = "/Windows/System32/drivers/etc/hosts" + c.System.cfgloc = "/ProgramData/rhosts/" + } + + configFuncs = append(configFuncs,fp) +} diff --git a/src/rhosts.go b/src/rhosts.go index 1dafa05..86d35bf 100644 --- a/src/rhosts.go +++ b/src/rhosts.go @@ -26,7 +26,6 @@ import ( "jbreich/rhosts/cfg" "jbreich/rhosts/hosts" "jbreich/rhosts/serve" - sysos "jbreich/rhosts/sys" "log" "time" ) @@ -53,9 +52,6 @@ const GPL = ` ` func main() { - tmpdir := "" - hostsloc := "" - cfgloc := "" var daemon bool = false var interval int = 1440 var versionflag bool = false @@ -87,28 +83,22 @@ func main() { log.Print("interval:", interval) } - sysos.Detect(&tmpdir, &hostsloc, &cfgloc) + _, config := cfg.Create() - // Read the config file - config := cfg.Create(cfgloc) - err, config := config.Update() - if err != nil { - log.Panic("Failed to parse config: " + cfgloc) - } // Starting web server serve.Start("blank") // Update the hosts file if daemon == false { - err := hosts.Update(config, tmpdir, hostsloc) + err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc) if err != nil { log.Print(err) } } else { for true { - err := hosts.Update(config, tmpdir, hostsloc) + err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc) if err != nil { log.Print(err) } diff --git a/src/sys/linux.go b/src/sys/linux.go deleted file mode 100644 index 9576a46..0000000 --- a/src/sys/linux.go +++ /dev/null @@ -1,13 +0,0 @@ -// +build linux - -package sys - - -func init(){ - - system.os = "linux" - system.tmpdir = "/tmp/" - system.hostsloc = "/etc/hosts" - system.cfgloc = "/etc/rhosts/" -} - diff --git a/src/sys/sys.go b/src/sys/sys.go deleted file mode 100644 index db13bfc..0000000 --- a/src/sys/sys.go +++ /dev/null @@ -1,22 +0,0 @@ -// This manages all the system specific settings -package sys - -import ( - "log" -) - -var system struct { - os string - tmpdir string - hostsloc string - cfgloc string -} - -func Detect (tmpdirp, hostslocp, cfglocp *string) { - if (system.os == "") { - log.Panic("This OS does not seem to be supported") - } - *tmpdirp = system.tmpdir - *hostslocp = system.hostsloc - *cfglocp = system.cfgloc -} diff --git a/src/sys/windows.go b/src/sys/windows.go deleted file mode 100644 index f02a866..0000000 --- a/src/sys/windows.go +++ /dev/null @@ -1,10 +0,0 @@ -// +build windows - -package sys - - -var os string = "windows" -var tmpdir string = "/tmp/" -var hostsloc string = "/Windows/System32/drivers/etc/hosts" -var cfgloc string = "/ProgramData/rhosts/" -