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