/
updateToken.go
67 lines (55 loc) · 1.54 KB
/
updateToken.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
package configuration
import (
"errors"
"fmt"
"strings"
"github.com/glad-dev/gin/log"
r "github.com/glad-dev/gin/remote"
)
// UpdateRemote loads the configuration file and updates the username and token name associated with each token.
func UpdateRemote() error {
config, err := Load()
if err != nil {
return err
}
invalid := make(map[string][]errorStruct)
var d r.Details
for i, remote := range config.Remotes {
for k, detail := range remote.Details {
// Check token's scope and update the username
d = detail
// We can ignore the gosec linter since go 1.22 creates a new variable for each for-loop iteration.
err = d.Init(&remote.URL, remote.Type) // nolint: gosec
if err != nil {
invalid[remote.URL.String()] = append(invalid[remote.URL.String()], errorStruct{
tokenName: d.TokenName,
err: err,
})
continue
}
remote.Details[k] = d
}
config.Remotes[i] = remote
}
err = write(config)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
if len(invalid) == 0 {
// write was successful and there were no issues updating the usernames
return nil
}
out := "The username for the following tokens could not be updated:\n"
for urlStr, errorStructs := range invalid {
for _, errStruct := range errorStructs {
out += fmt.Sprintf(
"- Remote '%s' with token '%s': %s\n",
urlStr,
errStruct.tokenName,
errStruct.err.Error(),
)
}
}
log.Error("Not all remotes could be updated.", "error", out)
return errors.New(strings.TrimSuffix(out, "\n"))
}