1
0
Fork 0
mirror of https://github.com/Luzifer/tmux-collector.git synced 2024-10-18 15:44:22 +00:00
tmux-collector/entry.go

95 lines
2.1 KiB
Go
Raw Normal View History

2018-08-18 16:34:53 +00:00
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path"
"strings"
"time"
homedir "github.com/mitchellh/go-homedir"
)
type config struct {
BaseBgColor string `yaml:"base_bg_color" json:"base_bg_color"`
Segments []*segment `yaml:"segments" json:"segments"`
}
type segment struct {
BackgroundError string `yaml:"background_error" json:"background_error"`
BackgroundSuccess string `yaml:"background_success" json:"background_success"`
ForegroundError string `yaml:"foreground_error" json:"foreground_error"`
ForegroundSuccess string `yaml:"foreground_success" json:"foreground_success"`
Command []string `yaml:"command" json:"command"`
Prefix string `yaml:"prefix" json:"prefix"`
Interval time.Duration `yaml:"interval" json:"interval"`
Output string
Err error
}
func (s segment) cacheKey() string {
return fmt.Sprintf("%x.json", sha256.Sum256([]byte(strings.Join(s.Command, " "))))
}
func (s segment) storeCache() error {
if s.Interval == 0 {
// No interval defined = execute all the time, no caching
return nil
}
p, err := homedir.Expand(path.Join("~", ".cache", "tmux-collector", s.cacheKey()))
if err != nil {
return err
}
if err = os.MkdirAll(path.Dir(p), 0700); err != nil {
return err
}
fh, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer fh.Close()
return json.NewEncoder(fh).Encode(s)
}
func (s *segment) loadCache() (bool, error) {
if s.Interval == 0 {
// No interval defined = execute all the time, no caching
return false, nil
}
p, err := homedir.Expand(path.Join("~", ".cache", "tmux-collector", s.cacheKey()))
if err != nil {
return false, err
}
stat, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if time.Since(stat.ModTime()) > s.Interval {
return false, nil
}
fh, err := os.Open(p)
if err != nil {
return false, err
}
defer fh.Close()
if err = json.NewDecoder(fh).Decode(s); err != nil {
return false, err
}
return true, nil
}