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