-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_ext.go
100 lines (90 loc) · 2.55 KB
/
windows_ext.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
package config
import (
"fmt"
"path/filepath"
"strings"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/config/registry"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/runtime"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/util"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
func (cpkg *Package) RenameFile(logE *logrus.Entry, fs afero.Fs, pkgPath string, file *registry.File, rt *runtime.Runtime) (string, error) {
s, err := cpkg.getFileSrcWithoutWindowsExt(file, rt)
if err != nil {
return "", err
}
if !isWindows(rt.GOOS) {
return s, nil
}
if util.Ext(s, cpkg.Package.Version) != "" {
return s, nil
}
return cpkg.renameFile(logE, fs, pkgPath, s)
}
func (cpkg *Package) renameFile(logE *logrus.Entry, fs afero.Fs, pkgPath, oldName string) (string, error) {
newName := oldName + cpkg.windowsExt()
newPath := filepath.Join(pkgPath, newName)
if _, err := fs.Stat(newPath); err == nil {
return newName, nil
}
old := filepath.Join(pkgPath, oldName)
if _, err := fs.Stat(old); err != nil {
return "", &FileNotFoundError{
Err: err,
}
}
logE.WithFields(logrus.Fields{
"new": newPath,
"old": old,
}).Info("rename a file")
if err := fs.Rename(old, newPath); err != nil {
return "", fmt.Errorf("rename a file: %w", err)
}
return newName, nil
}
func (cpkg *Package) windowsExt() string {
if cpkg.PackageInfo.WindowsExt == "" {
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return ".sh"
}
return ".exe"
}
return cpkg.PackageInfo.WindowsExt
}
func (cpkg *Package) completeWindowsExt(s string) string {
if cpkg.PackageInfo.CompleteWindowsExt != nil {
if *cpkg.PackageInfo.CompleteWindowsExt {
return s + cpkg.windowsExt()
}
return s
}
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return s
}
return s + cpkg.windowsExt()
}
func (cpkg *Package) completeWindowsExtToAsset(asset string) string {
if strings.HasSuffix(asset, ".exe") {
return asset
}
if cpkg.PackageInfo.Format == "raw" {
return cpkg.completeWindowsExt(asset)
}
if cpkg.PackageInfo.Format != "" {
return asset
}
if util.Ext(asset, cpkg.Package.Version) == "" {
return cpkg.completeWindowsExt(asset)
}
return asset
}
func (cpkg *Package) completeWindowsExtToURL(url string) string {
return cpkg.completeWindowsExtToAsset(url)
}
func (cpkg *Package) completeWindowsExtToFileSrc(src string) string {
if util.Ext(src, cpkg.Package.Version) == "" {
return src + cpkg.windowsExt()
}
return src
}