Skip to content

Commit 898ff5c

Browse files
committed
Merge branch 'master' of github.com:liamg/shox
2 parents 06d7733 + 9180b38 commit 898ff5c

File tree

4 files changed

+97
-22
lines changed

4 files changed

+97
-22
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ The default value is `1` which only shows the weather
7676

7777
> **_NOTE:_** You don't need to URL-encode the weather format, i.e. use `%l: %c %t` instead of `%l:+%c+%t`
7878

79+
## Uninstallation
80+
81+
### If installed with `sudo`
82+
Remove the binary from `/usr/local/bin`
83+
```bash
84+
rm /usr/local/bin/shox
85+
```
86+
87+
### If installed without `sudo`
88+
Remove the binary from the shox installation dir `$HOME/bin`
89+
```bash
90+
rm $HOME/bin/shox
91+
```
92+
93+
> **_NOTE:_** Don't forget to remove any configuration files you've created should you decide you don't need them
94+
7995
## Why?
8096

8197
I frequently needed a way to have a quick overview of several things without cramming them into my PS1, and to update those things dynamicly.

pkg/helpers/cache.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package helpers
2+
3+
import (
4+
"strings"
5+
"sync"
6+
"time"
7+
)
8+
9+
// HelperRun represents a single run of a helper
10+
type HelperRun struct {
11+
output string
12+
runTime time.Time
13+
}
14+
15+
// Output returns the output of the run
16+
func (hr HelperRun) Output() string {
17+
return hr.output
18+
}
19+
20+
// Time returns the time of the run
21+
func (hr HelperRun) Time() time.Time {
22+
return hr.runTime
23+
}
24+
25+
// HelperRunCache stores the last run of all unique helpers.
26+
// A helper is defined by its name and config
27+
type HelperRunCache struct {
28+
lock sync.Mutex
29+
store map[string]HelperRun
30+
}
31+
32+
// NewHelperRunCache creates a new helper run cache
33+
func NewHelperRunCache() HelperRunCache {
34+
return HelperRunCache{
35+
lock: sync.Mutex{},
36+
store: map[string]HelperRun{},
37+
}
38+
}
39+
40+
// GetOrAdd tries to find an item in the cache and creates one if not found
41+
func (c *HelperRunCache) GetOrAdd(key string) HelperRun {
42+
c.lock.Lock()
43+
defer c.lock.Unlock()
44+
45+
item, found := c.store[key]
46+
if !found {
47+
item = HelperRun{
48+
runTime: time.Now(),
49+
}
50+
c.store[key] = item
51+
}
52+
53+
return item
54+
}
55+
56+
// Put updates an existing item in the cache or creates one if not found
57+
func (c *HelperRunCache) Put(key, output string) {
58+
c.lock.Lock()
59+
defer c.lock.Unlock()
60+
61+
item, found := c.store[key]
62+
if !found {
63+
item = HelperRun{}
64+
}
65+
66+
item.output = output
67+
item.runTime = time.Now()
68+
c.store[key] = item
69+
}
70+
71+
// Key generates a cache key from a Helper's attributes
72+
func (c *HelperRunCache) Key(parts ...string) string {
73+
return strings.Join(parts, ":")
74+
}

pkg/helpers/helper.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ type Helper interface {
1212
UpdateInterval() time.Duration // UpdateInterval returns the minimum time period before the helper should run again
1313
}
1414

15-
var regLock sync.Mutex
15+
var regLock = sync.Mutex{}
1616
var registry = map[string]Helper{}
17+
var runCache = NewHelperRunCache()
1718

1819
// Register registers a new helper by name
1920
func Register(name string, helper Helper) {
@@ -28,14 +29,6 @@ func Register(name string, helper Helper) {
2829
// ErrHelperNotFound means no helper exists by the specified name
2930
var ErrHelperNotFound = fmt.Errorf("helper not found")
3031

31-
var cacheLock sync.Mutex
32-
var cache = map[string]helperRun{}
33-
34-
type helperRun struct {
35-
output string
36-
runTime time.Time
37-
}
38-
3932
// Run executes a helper with the provided config string
4033
func Run(name, config string) (string, error) {
4134
regLock.Lock()
@@ -45,19 +38,12 @@ func Run(name, config string) (string, error) {
4538
return "", ErrHelperNotFound
4639
}
4740

48-
cacheLock.Lock()
49-
defer cacheLock.Unlock()
50-
51-
if lastRun, ok := cache[name]; ok {
52-
if time.Since(lastRun.runTime) < helper.UpdateInterval() {
53-
return lastRun.output, nil
54-
}
41+
lastRun := runCache.GetOrAdd(runCache.Key(name, config))
42+
if time.Since(lastRun.Time()) < helper.UpdateInterval() {
43+
return lastRun.Output(), nil
5544
}
5645

5746
output := helper.Run(config)
58-
cache[name] = helperRun{
59-
output: output,
60-
runTime: time.Now(),
61-
}
47+
runCache.Put(runCache.Key(name, config), output)
6248
return output, nil
6349
}

scripts/install.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ set -e
55
echo "Determining platform..."
66
platform=$(uname | tr '[:upper:]' '[:lower:]')
77
echo "Finding latest release..."
8-
which jq >/dev/null 2>&1 || { echo "jq not found so you'll need to install it in a way suitable for your operating system. See https://stedolan.github.io/jq/ for details" >&2; exit 1; }
9-
asset=$(curl --silent https://api.github.com/repos/liamg/shox/releases/latest | jq -r ".assets[] | select(.name | contains(\"${platform}\")) | .url")
8+
asset=$(curl --silent https://api.github.com/repos/liamg/shox/releases/latest | grep -o "https://github.com/liamg/shox/releases/download/.*/shox-$platform-amd64" | head -n1)
109
echo "Downloading latest release for your platform..."
1110
curl -s -L -H "Accept: application/octet-stream" "${asset}" --output ./shox
1211
echo "Installing shox..."

0 commit comments

Comments
 (0)