SetRefreshInterval sets the time interval at which a plugin should be re-run.
(pluginDirectory, installedPluginPath string, refreshInterval RefreshInterval)
| 65 | |
| 66 | // SetRefreshInterval sets the time interval at which a plugin should be re-run. |
| 67 | func SetRefreshInterval(pluginDirectory, installedPluginPath string, refreshInterval RefreshInterval) (string, RefreshInterval, error) { |
| 68 | interval := findIntervalInFilename(installedPluginPath) |
| 69 | if err := validateRefreshInterval(refreshInterval); err != nil { |
| 70 | return "", RefreshInterval{}, errors.Wrap(err, "invalid refresh interval") |
| 71 | } |
| 72 | oldFullPath := filepath.Join(pluginDirectory, installedPluginPath) |
| 73 | newFilename := strings.Replace(installedPluginPath, "."+interval+".", "."+refreshInterval.String()+".", 1) |
| 74 | newFullPath := filepath.Join(pluginDirectory, newFilename) |
| 75 | if err := os.Rename(oldFullPath, newFullPath); err != nil { |
| 76 | return "", RefreshInterval{}, errors.Wrap(err, "rename plugin file to new refresh interval") |
| 77 | } |
| 78 | _, err := os.Stat(newFullPath) |
| 79 | if err != nil { |
| 80 | return "", RefreshInterval{}, errors.Wrap(err, "stat plugin file") |
| 81 | } |
| 82 | oldVarFullPath := oldFullPath + variableJSONFileExt |
| 83 | _, err = os.Stat(oldVarFullPath) |
| 84 | if err != nil && !os.IsNotExist(err) { |
| 85 | return "", RefreshInterval{}, errors.Wrap(err, "stat plugin vars file") |
| 86 | } |
| 87 | if err != nil && os.IsNotExist(err) { |
| 88 | // no variable file, no probs |
| 89 | return newFilename, refreshInterval, nil |
| 90 | } |
| 91 | newVarFilename := newFilename + variableJSONFileExt |
| 92 | newVarFullPath := filepath.Join(pluginDirectory, newVarFilename) |
| 93 | if err := os.Rename(oldVarFullPath, newVarFullPath); err != nil { |
| 94 | return "", RefreshInterval{}, errors.Wrap(err, "rename plugin vars file to new refresh interval") |
| 95 | } |
| 96 | return newFilename, refreshInterval, nil |
| 97 | } |
| 98 | |
| 99 | func validateRefreshInterval(refreshInterval RefreshInterval) error { |
| 100 | if n := refreshInterval.N; n < 1 { |