From d2ff55737cf43800e6d6a6fef8ef4845d1333bbe Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Wed, 1 Nov 2023 12:41:07 +0100 Subject: [PATCH] Enable multi-account migration --- cmd/gh/main.go | 13 ++++++------- internal/config/migrate.go | 7 +++++++ internal/config/migration/multi_account.go | 8 ++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 871afbb5f..4fe0f46d7 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -56,13 +56,12 @@ func mainRun() exitCode { ctx := context.Background() - // cfg, err := cmdFactory.Config() - // if err != nil { - // fmt.Fprintf(stderr, "failed to load configuration to attempt migration: %s\n", err) - // } - // if err := cfg.MigrateMultiAccount(); err != nil { - // fmt.Fprintf(stderr, "failed to migrate configuration: %s\n", err) - // } + if cfg, err := cmdFactory.Config(); err == nil { + if err := cfg.MigrateMultiAccount(); err != nil { + fmt.Fprintf(stderr, "failed to migrate configuration: %s\n", err) + return exitError + } + } updateCtx, updateCancel := context.WithCancel(ctx) defer updateCancel() diff --git a/internal/config/migrate.go b/internal/config/migrate.go index 1556f0a88..06a38027f 100644 --- a/internal/config/migrate.go +++ b/internal/config/migrate.go @@ -30,6 +30,13 @@ func Migrate(c *ghConfig.Config, m Migration) error { // It is expected initially that there is no version key because we don't // have one to begin with, so an error is expected. version, _ := c.Get([]string{versionKey}) + + // If migration has already occured then do not attempt to migrate again. + if m.PostVersion() == version { + return nil + } + + // If migration is incompatible with current version then return an error. if m.PreVersion() != version { return fmt.Errorf("failed to migrate as %q pre migration version did not match config version %q", m.PreVersion(), version) } diff --git a/internal/config/migration/multi_account.go b/internal/config/migration/multi_account.go index f950e6fc5..d8a6ed142 100644 --- a/internal/config/migration/multi_account.go +++ b/internal/config/migration/multi_account.go @@ -103,13 +103,13 @@ func (m MultiAccount) Do(c *config.Config) error { return CowardlyRefusalError{fmt.Errorf("couldn't get user name for %q: %w", hostname, err)} } - if err := migrateToken(hostname, username, token, inKeyring); err != nil { - return CowardlyRefusalError{fmt.Errorf("couldn't not migrate oauth token for %q: %w", hostname, err)} - } - if err := migrateConfig(c, hostname, username); err != nil { return CowardlyRefusalError{fmt.Errorf("couldn't not migrate config for %q: %w", hostname, err)} } + + if err := migrateToken(hostname, username, token, inKeyring); err != nil { + return CowardlyRefusalError{fmt.Errorf("couldn't not migrate oauth token for %q: %w", hostname, err)} + } } return nil