| 208 | } |
| 209 | |
| 210 | func (c *cfg) Migrate(m gh.Migration) error { |
| 211 | // If there is no version entry we must never have applied a migration, and the following conditional logic |
| 212 | // handles the version as an empty string correctly. |
| 213 | version := c.Version().UnwrapOrZero() |
| 214 | |
| 215 | // If migration has already occurred then do not attempt to migrate again. |
| 216 | if m.PostVersion() == version { |
| 217 | return nil |
| 218 | } |
| 219 | |
| 220 | // If migration is incompatible with current version then return an error. |
| 221 | if m.PreVersion() != version { |
| 222 | return fmt.Errorf("failed to migrate as %q pre migration version did not match config version %q", m.PreVersion(), version) |
| 223 | } |
| 224 | |
| 225 | if err := m.Do(c.cfg); err != nil { |
| 226 | return fmt.Errorf("failed to migrate config: %s", err) |
| 227 | } |
| 228 | |
| 229 | c.Set("", versionKey, m.PostVersion()) |
| 230 | |
| 231 | // Then write out our migrated config. |
| 232 | if err := c.Write(); err != nil { |
| 233 | return fmt.Errorf("failed to write config after migration: %s", err) |
| 234 | } |
| 235 | |
| 236 | return nil |
| 237 | } |
| 238 | |
| 239 | func (c *cfg) CacheDir() string { |
| 240 | return ghConfig.CacheDir() |