Skip to content

Commit

Permalink
Merge branch 'master' of github.com:liamg/shox
Browse files Browse the repository at this point in the history
  • Loading branch information
liamg committed Mar 3, 2020
2 parents 06d7733 + 9180b38 commit 898ff5c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 22 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ The default value is `1` which only shows the weather

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

## Uninstallation

### If installed with `sudo`
Remove the binary from `/usr/local/bin`
```bash
rm /usr/local/bin/shox
```

### If installed without `sudo`
Remove the binary from the shox installation dir `$HOME/bin`
```bash
rm $HOME/bin/shox
```

> **_NOTE:_** Don't forget to remove any configuration files you've created should you decide you don't need them

## Why?

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.
Expand Down
74 changes: 74 additions & 0 deletions pkg/helpers/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package helpers

import (
"strings"
"sync"
"time"
)

// HelperRun represents a single run of a helper
type HelperRun struct {
output string
runTime time.Time
}

// Output returns the output of the run
func (hr HelperRun) Output() string {
return hr.output
}

// Time returns the time of the run
func (hr HelperRun) Time() time.Time {
return hr.runTime
}

// HelperRunCache stores the last run of all unique helpers.
// A helper is defined by its name and config
type HelperRunCache struct {
lock sync.Mutex
store map[string]HelperRun
}

// NewHelperRunCache creates a new helper run cache
func NewHelperRunCache() HelperRunCache {
return HelperRunCache{
lock: sync.Mutex{},
store: map[string]HelperRun{},
}
}

// GetOrAdd tries to find an item in the cache and creates one if not found
func (c *HelperRunCache) GetOrAdd(key string) HelperRun {
c.lock.Lock()
defer c.lock.Unlock()

item, found := c.store[key]
if !found {
item = HelperRun{
runTime: time.Now(),
}
c.store[key] = item
}

return item
}

// Put updates an existing item in the cache or creates one if not found
func (c *HelperRunCache) Put(key, output string) {
c.lock.Lock()
defer c.lock.Unlock()

item, found := c.store[key]
if !found {
item = HelperRun{}
}

item.output = output
item.runTime = time.Now()
c.store[key] = item
}

// Key generates a cache key from a Helper's attributes
func (c *HelperRunCache) Key(parts ...string) string {
return strings.Join(parts, ":")
}
26 changes: 6 additions & 20 deletions pkg/helpers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type Helper interface {
UpdateInterval() time.Duration // UpdateInterval returns the minimum time period before the helper should run again
}

var regLock sync.Mutex
var regLock = sync.Mutex{}
var registry = map[string]Helper{}
var runCache = NewHelperRunCache()

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

var cacheLock sync.Mutex
var cache = map[string]helperRun{}

type helperRun struct {
output string
runTime time.Time
}

// Run executes a helper with the provided config string
func Run(name, config string) (string, error) {
regLock.Lock()
Expand All @@ -45,19 +38,12 @@ func Run(name, config string) (string, error) {
return "", ErrHelperNotFound
}

cacheLock.Lock()
defer cacheLock.Unlock()

if lastRun, ok := cache[name]; ok {
if time.Since(lastRun.runTime) < helper.UpdateInterval() {
return lastRun.output, nil
}
lastRun := runCache.GetOrAdd(runCache.Key(name, config))
if time.Since(lastRun.Time()) < helper.UpdateInterval() {
return lastRun.Output(), nil
}

output := helper.Run(config)
cache[name] = helperRun{
output: output,
runTime: time.Now(),
}
runCache.Put(runCache.Key(name, config), output)
return output, nil
}
3 changes: 1 addition & 2 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ set -e
echo "Determining platform..."
platform=$(uname | tr '[:upper:]' '[:lower:]')
echo "Finding latest release..."
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; }
asset=$(curl --silent https://api.github.com/repos/liamg/shox/releases/latest | jq -r ".assets[] | select(.name | contains(\"${platform}\")) | .url")
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)
echo "Downloading latest release for your platform..."
curl -s -L -H "Accept: application/octet-stream" "${asset}" --output ./shox
echo "Installing shox..."
Expand Down

0 comments on commit 898ff5c

Please sign in to comment.