From: Jeff Halter Date: Wed, 31 Jul 2024 21:56:46 +0000 (-0700) Subject: Migrate user account yaml files to new Access flag format if needed X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/9360423f0aae6d7ce63cc9a4b20c031a58e06820 Migrate user account yaml files to new Access flag format if needed --- diff --git a/internal/mobius/account_manager.go b/internal/mobius/account_manager.go index 3bb5cbc..6bbc951 100644 --- a/internal/mobius/account_manager.go +++ b/internal/mobius/account_manager.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strings" "sync" ) @@ -45,11 +46,25 @@ func NewYAMLAccountManager(accountDir string) (*YAMLAccountManager, error) { return nil, fmt.Errorf("no accounts found in directory: %s", accountDir) } - for _, file := range matches { + for _, filePath := range matches { var account hotline.Account - if err = loadFromYAMLFile(file, &account); err != nil { - return nil, fmt.Errorf("error loading account %s: %w", file, err) + fileContents, err := os.ReadFile(filePath) + if err != nil { + return nil, fmt.Errorf("read file: %v", err) + } + + if err := yaml.Unmarshal(fileContents, &account); err != nil { + return nil, fmt.Errorf("unmarshal: %v", err) } + + // Check the account file contents for a field name that only appears in the new AccessBitmap flag format. + // If not present, re-save the file to migrate it from the old array of ints format to new bool flag format. + if !strings.Contains(string(fileContents), " DownloadFile:") { + if err := accountMgr.Update(account, account.Login); err != nil { + return nil, fmt.Errorf("migrate account to new access flag format: %v", err) + } + } + accountMgr.accounts[account.Login] = account }