github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/uniqid/legacy.go (about)

     1  package uniqid
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/ActiveState/cli/internal/osutils/user"
     9  )
    10  
    11  const legacyPersistDir = "activestate/persist"
    12  
    13  func moveUniqidFile(destination string) error {
    14  	legacyDir, err := legacyStorageDir()
    15  	if err != nil {
    16  		return fmt.Errorf("Could not get legacy storage directory: %w", err)
    17  	}
    18  
    19  	legacyUniqIDFile := filepath.Join(legacyDir, fileName)
    20  
    21  	// If the uniqID file does not not exist there is nothing to move
    22  	if !fileExists(legacyUniqIDFile) {
    23  		return nil
    24  	}
    25  
    26  	err = mkdirUnlessExists(filepath.Dir(destination))
    27  	if err != nil {
    28  		return fmt.Errorf("Could not create new persist directory: %w", err)
    29  	}
    30  
    31  	err = copyFile(legacyUniqIDFile, destination)
    32  	if err != nil {
    33  		return fmt.Errorf("Could not move legacy uniqid file: %w", err)
    34  	}
    35  
    36  	// Ignore removal errors that could occur due to permissions issues
    37  	// Remove the legacy uniqid file and its parent directories
    38  	// The legacy directory is a sub directory, we want to remove the parent
    39  	_ = os.Remove(legacyUniqIDFile)
    40  	_ = os.Remove(legacyDir)
    41  	_ = os.Remove(filepath.Dir(legacyDir))
    42  
    43  	return nil
    44  }
    45  
    46  func legacyStorageDir() (string, error) {
    47  	home, err := user.HomeDir()
    48  	if err != nil {
    49  		return "", fmt.Errorf("cannot get home dir for uniqid file: %w", err)
    50  	}
    51  
    52  	return filepath.Join(home, "AppData", legacyPersistDir), nil
    53  }