diff --git a/services/settings/syncsettings.go b/services/settings/syncsettings.go index d672dd64..7f2c3d63 100644 --- a/services/settings/syncsettings.go +++ b/services/settings/syncsettings.go @@ -14,26 +14,26 @@ import ( // SyncSettings is the struct holding sync-settings information type SyncSettings struct { - settingsFile string - defaultsFile string - currentFile string - osForSyncSettings string - tmpSettingsFile string - syncSettingsExecutable string - uidFile string + SettingsFile string + DefaultsFile string + CurrentFile string + OS string + TmpSettingsFile string + SyncSettingsExecutable string + UIDFile string } // NewSyncSettings creates a new settings object -func NewSyncSettings(settingsfile string, defaultsfile string, currentfile string, osforsyncsettings string, tmpsettingsfile string, syncsettingsexecutable string, uidfile string) *SyncSettings { +func NewSyncSettings(settingsfile string, defaultsfile string, currentfile string, os string, tmpsettingsfile string, syncsettingsexecutable string, uidfile string) *SyncSettings { s := new(SyncSettings) - s.settingsFile = settingsfile - s.defaultsFile = defaultsfile - s.currentFile = currentfile - s.osForSyncSettings = osforsyncsettings - s.tmpSettingsFile = tmpsettingsfile - s.syncSettingsExecutable = syncsettingsexecutable - s.uidFile = uidfile + s.SettingsFile = settingsfile + s.DefaultsFile = defaultsfile + s.CurrentFile = currentfile + s.OS = os + s.TmpSettingsFile = tmpsettingsfile + s.SyncSettingsExecutable = syncsettingsexecutable + s.UIDFile = uidfile return s @@ -42,7 +42,7 @@ func NewSyncSettings(settingsfile string, defaultsfile string, currentfile strin // CreateDefaults creates the settings defauls.json file func (s *SyncSettings) CreateDefaults() error { // sync the defaults - cmdArgs := []string{"-o", s.osForSyncSettings, "-c", "-s", "-f", s.tmpSettingsFile} + cmdArgs := []string{"-o", s.OS, "-c", "-s", "-f", s.TmpSettingsFile} err := s.runSyncSettings(cmdArgs) if err != nil { logger.Warn("Error creating defaults: %s\n", err.Error()) @@ -50,19 +50,19 @@ func (s *SyncSettings) CreateDefaults() error { } // move the defaults. Have to read/write file to avoid docker copy errors - defaultsBytes, readErr := ioutil.ReadFile(s.tmpSettingsFile) + defaultsBytes, readErr := ioutil.ReadFile(s.TmpSettingsFile) if readErr != nil { logger.Warn("Failure copying defaults over: %s\n", readErr.Error()) return readErr } - writeErr := ioutil.WriteFile(s.defaultsFile, defaultsBytes, 0660) + writeErr := ioutil.WriteFile(s.DefaultsFile, defaultsBytes, 0660) if writeErr != nil { logger.Warn("Failure copying defaults over: %s\n", writeErr.Error()) return writeErr } - removeErr := os.Remove(s.tmpSettingsFile) + removeErr := os.Remove(s.TmpSettingsFile) if removeErr != nil { logger.Warn("Could not remove default tmp file: %s. Continueing\n", removeErr.Error()) } @@ -72,7 +72,7 @@ func (s *SyncSettings) CreateDefaults() error { // NormalSync runs sync settings with OS and filename specified func (s *SyncSettings) NormalSync() error { - cmdArgs := []string{"-o", s.osForSyncSettings, "-f", s.settingsFile} + cmdArgs := []string{"-o", s.OS, "-f", s.SettingsFile} err := s.runSyncSettings(cmdArgs) if err != nil { logger.Warn("Error running sync-settings: %s\n", err.Error()) @@ -81,12 +81,26 @@ func (s *SyncSettings) NormalSync() error { return nil } +// SimulateSync will run sync-settings with simulation flag on the given filePath +// This will not write any files out or restart any services +// but will get the return result as if the file was run properly +func (s *SyncSettings) SimulateSync(filePath string) error { + cmdArgs := []string{"-o", s.OS, "-s", "-f", filePath} + err := s.runSyncSettings(cmdArgs) + if err != nil { + logger.Warn("Error running sync-settings with simulate flag : %s\n", err.Error()) + return err + } + + return nil +} + // FirstSyncSettingsRun will create the settings file if it doesn't exist, or rerun sync-settings for good measure func (s *SyncSettings) FirstSyncSettingsRun() error { - cmdArgs := []string{"-o", s.osForSyncSettings, "-n"} + cmdArgs := []string{"-o", s.OS, "-n"} // check if settings.json exists, if not create it - info, checkErr := os.Stat(s.settingsFile) + info, checkErr := os.Stat(s.SettingsFile) if os.IsNotExist(checkErr) { cmdArgs = append(cmdArgs, "-c") } else if info.IsDir() { @@ -107,7 +121,7 @@ func (s *SyncSettings) FirstSyncSettingsRun() error { // runSyncSettings runs sync settings with given cmd args func (s *SyncSettings) runSyncSettings(cmdArgs []string) error { - cmd := exec.Command(s.syncSettingsExecutable, cmdArgs...) + cmd := exec.Command(s.SyncSettingsExecutable, cmdArgs...) outbytes, err := cmd.CombinedOutput() output := string(outbytes) var runErr error