-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
167 lines (152 loc) · 3.35 KB
/
profile.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"cmp"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"slices"
"github.com/thansetan/git-sw/pkg/gitconfig"
)
type Profile struct {
Config *gitconfig.GitConfig
Name, DirName string
IsActive bool
}
func getProfilePath(profileName string) (string, error) {
dirName, err := hash(profileName)
if err != nil {
return "", err
}
return filepath.Join(saveDirPath, dirName), nil
}
func saveProfile(dirPath string, profileName string, config *gitconfig.GitConfig) (err error) {
err = os.MkdirAll(dirPath, 0o744)
if err != nil {
return err
}
defer func() {
if err != nil {
errDel := os.RemoveAll(dirPath)
if errDel != nil {
fmt.Printf("error removing directory: %s", dirPath)
}
}
}()
err = config.Save(filepath.Join(dirPath, ".gitconfig"))
if err != nil {
return err
}
f, err := os.Create(filepath.Join(dirPath, "profile"))
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(profileName)
if err != nil {
return err
}
err = f.Chmod(0o444)
if err != nil {
return err
}
return nil
}
func copyDefault() error {
var defaultConf *gitconfig.GitConfig
dirName, err := getProfilePath(defaultConfigName)
if err != nil {
return err
}
err = os.RemoveAll(dirName)
if err != nil {
return err
}
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
configPath := filepath.Join(homeDir, ".gitconfig")
configContent, err := os.ReadFile(configPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
defaultConf = gitconfig.New()
err = defaultConf.Save(configPath)
if err != nil {
return err
}
goto saveProfile
}
return err
}
defaultConf, err = gitconfig.Parse(configContent)
if err != nil {
return err
}
saveProfile:
err = saveProfile(dirName, defaultConfigName, defaultConf)
if err != nil {
return err
}
return nil
}
func getCurrentProfile() (string, error) {
currentConfig, err := getCurrentConfig()
if err != nil {
return "", err
}
profileDir := filepath.Dir(currentConfig)
if profileDir == "." {
return "default", nil
}
profileFile, err := os.Open(filepath.Join(profileDir, "profile"))
if err != nil {
return "default", err
}
defer profileFile.Close()
profileName, err := io.ReadAll(profileFile)
if err != nil {
return "default", err
}
return string(profileName), nil
}
func getProfiles(configPath string) ([]Profile, error) {
var profiles []Profile
currProfile, err := getCurrentProfile()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
err = filepath.WalkDir(configPath, func(path string, d fs.DirEntry, err error) error {
if d.Name() == "profile" {
profileFile, err := os.Open(path)
if err != nil {
return err
}
defer profileFile.Close()
profileName, err := io.ReadAll(profileFile)
if err != nil {
return err
}
dirName := filepath.Base(filepath.Dir(path))
h, err := hash(string(profileName))
if err != nil || h != dirName { // an entity has changed the profile name, should I remove ??? or just skip it ???
return fs.SkipDir
}
profiles = append(profiles, Profile{
Name: string(profileName),
IsActive: string(profileName) == currProfile,
DirName: dirName,
})
}
return err
})
if err != nil {
return nil, err
}
slices.SortFunc(profiles, func(a, b Profile) int {
return cmp.Compare(a.Name, b.Name)
})
return profiles, nil
}