• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

UiPath / uipathcli / 12392579821

18 Dec 2024 12:05PM UTC coverage: 88.981% (+2.9%) from 86.121%
12392579821

push

github

thschmitt
Add support to analyze studio projects

211 of 270 new or added lines in 8 files covered. (78.15%)

40 existing lines in 4 files now uncovered.

4813 of 5409 relevant lines covered (88.98%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.72
/cache/file_cache.go
1
package cache
2

3
import (
4
        "crypto/sha256"
5
        "errors"
6
        "fmt"
7
        "os"
8
        "path/filepath"
9
        "strconv"
10
        "strings"
11
        "time"
12

13
        "github.com/UiPath/uipathcli/utils"
14
)
15

16
const cacheFilePermissions = 0600
17
const separator = "|"
18

19
// The FileCache stores data on disk in order to preserve them across
20
// multiple CLI invocations.
21
type FileCache struct{}
22

23
func (c FileCache) Get(key string) (string, float32) {
1✔
24
        expiry, value, err := c.readValue(key)
1✔
25
        if err != nil {
2✔
26
                return "", 0
1✔
27
        }
1✔
28
        if expiry < time.Now().Unix()+30 {
2✔
29
                return "", 0
1✔
30
        }
1✔
31
        return value, float32(expiry)
1✔
32
}
33

34
func (c FileCache) Set(key string, value string, expiresIn float32) {
1✔
35
        path, err := c.cacheFilePath(key)
1✔
36
        if err != nil {
1✔
37
                return
×
UNCOV
38
        }
×
39
        expires := time.Now().Unix() + int64(expiresIn)
1✔
40
        data := []byte(fmt.Sprintf("%d%s%s", expires, separator, value))
1✔
41
        _ = os.WriteFile(path, data, cacheFilePermissions)
1✔
42
}
43

44
func (c FileCache) readValue(key string) (int64, string, error) {
1✔
45
        path, err := c.cacheFilePath(key)
1✔
46
        if err != nil {
1✔
47
                return 0, "", err
×
UNCOV
48
        }
×
49
        data, err := os.ReadFile(path)
1✔
50
        if err != nil {
2✔
51
                return 0, "", err
1✔
52
        }
1✔
53
        split := strings.Split(string(data), separator)
1✔
54
        if len(split) != 2 {
1✔
55
                return 0, "", errors.New("Could not split cache data")
×
UNCOV
56
        }
×
57
        expiry, err := strconv.ParseInt(split[0], 10, 64)
1✔
58
        if err != nil {
1✔
59
                return 0, "", err
×
UNCOV
60
        }
×
61
        value := split[1]
1✔
62
        return expiry, value, nil
1✔
63
}
64

65
func (c FileCache) cacheFilePath(key string) (string, error) {
1✔
66
        cacheDirectory, err := utils.Directories{}.Cache()
1✔
67
        if err != nil {
1✔
68
                return "", err
×
UNCOV
69
        }
×
70
        hash := sha256.Sum256([]byte(key))
1✔
71
        fileName := fmt.Sprintf("%x", hash)
1✔
72
        return filepath.Join(cacheDirectory, fileName), nil
1✔
73
}
74

75
func NewFileCache() *FileCache {
1✔
76
        return &FileCache{}
1✔
77
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc