| 180 | } |
| 181 | |
| 182 | func (c *cfg) Migrate(m gh.Migration) error { |
| 183 | // If there is no version entry we must never have applied a migration, and the following conditional logic |
| 184 | // handles the version as an empty string correctly. |
| 185 | version := c.Version().UnwrapOrZero() |
| 186 | |
| 187 | // If migration has already occurred then do not attempt to migrate again. |
| 188 | if m.PostVersion() == version { |
| 189 | return nil |
| 190 | } |
| 191 | |
| 192 | // If migration is incompatible with current version then return an error. |
| 193 | if m.PreVersion() != version { |
| 194 | return fmt.Errorf("failed to migrate as %q pre migration version did not match config version %q", m.PreVersion(), version) |
| 195 | } |
| 196 | |
| 197 | if err := m.Do(c.cfg); err != nil { |
| 198 | return fmt.Errorf("failed to migrate config: %s", err) |
| 199 | } |
| 200 | |
| 201 | c.Set("", versionKey, m.PostVersion()) |
| 202 | |
| 203 | // Then write out our migrated config. |
| 204 | if err := c.Write(); err != nil { |
| 205 | return fmt.Errorf("failed to write config after migration: %s", err) |
| 206 | } |
| 207 | |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | func (c *cfg) CacheDir() string { |
| 212 | return ghConfig.CacheDir() |