-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathutil.go
More file actions
73 lines (65 loc) · 1.91 KB
/
util.go
File metadata and controls
73 lines (65 loc) · 1.91 KB
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
package environments
import (
"fmt"
"os"
"strings"
"time"
)
const (
errorRandomString string = "SomethingRandomWentWrong"
)
// ReadExternalFile to read an external file and return contents
func ReadExternalFile(path string) string {
content, err := os.ReadFile(path)
if err != nil {
return ""
}
return string(content)
}
// IsItExpired to determine if a time has expired, which makes it in the past
func IsItExpired(t time.Time) bool {
if t.IsZero() {
return false
}
now := time.Now()
return (int(t.Sub(now).Seconds()) <= 0)
}
// IsPlatformQuery to know if a plaform is going to trigger a query
func IsPlatformQuery(pQuery, pCheck string) bool {
// Empty plaform means all platforms
if pQuery == "" || pQuery == "all" || pQuery == "any" {
return true
}
// Check if platform is posix (darwin, freebsd, linux)
if pQuery == "posix" && (pCheck == "darwin" || pCheck == "freebsd" || pCheck == "linux" || IsPlatformLinux(strings.ToLower(pCheck))) {
return true
}
// Last check is platform itself
return (pQuery == pCheck)
}
// IsPlatformLinux to know if a linux is going to trigger a query
func IsPlatformLinux(pCheck string) bool {
return (pCheck == "ubuntu" || pCheck == "centos" || pCheck == "rhel" || pCheck == "fedora" || pCheck == "debian" || pCheck == "opensuse" || pCheck == "arch" || pCheck == "amzn")
}
// PackageDownloadURL to get the download URL for a package
func PackageDownloadURL(env TLSEnvironment, pkg string) string {
if pkg == "" {
return ""
}
if strings.HasPrefix(pkg, "https://") {
return pkg
}
return fmt.Sprintf("https://%s/%s/%s/package/%s", env.Hostname, env.UUID, env.Secret, pkg)
}
// EnvironmentFinder to find the environment and return its name based on the environment ID
func EnvironmentFinder(envID uint, envs []TLSEnvironment, uuid bool) string {
for _, env := range envs {
if env.ID == envID {
if uuid {
return env.UUID
}
return env.Name
}
}
return "Unknown"
}