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