Skip to content

Commit

Permalink
Better magic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Feb 12, 2018
1 parent 9a0cf81 commit 28cc53e
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 104 deletions.
38 changes: 19 additions & 19 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewCache(dir string) *Cache {
}

// Store saves data under the given name. If data is nil, the file is deleted.
func (c *Cache) Store(name string, data []byte) error {
func (c Cache) Store(name string, data []byte) error {
p := c.path(name)
if data == nil {
if util.PathExists(p) {
Expand All @@ -67,7 +67,7 @@ func (c *Cache) Store(name string, data []byte) error {

// StoreJSON serialises v to JSON and saves it to the cache. If v is nil,
// the cache is deleted.
func (c *Cache) StoreJSON(name string, v interface{}) error {
func (c Cache) StoreJSON(name string, v interface{}) error {
p := c.path(name)
if v == nil {
if util.PathExists(p) {
Expand All @@ -83,7 +83,7 @@ func (c *Cache) StoreJSON(name string, v interface{}) error {
}

// Load reads data saved under given name.
func (c *Cache) Load(name string) ([]byte, error) {
func (c Cache) Load(name string) ([]byte, error) {
p := c.path(name)
if _, err := os.Stat(p); err != nil {
return nil, err
Expand All @@ -92,7 +92,7 @@ func (c *Cache) Load(name string) ([]byte, error) {
}

// LoadJSON unmarshals a cache into v.
func (c *Cache) LoadJSON(name string, v interface{}) error {
func (c Cache) LoadJSON(name string, v interface{}) error {
p := c.path(name)
data, err := ioutil.ReadFile(p)
if err != nil {
Expand All @@ -106,7 +106,7 @@ func (c *Cache) LoadJSON(name string, v interface{}) error {
// data are cached & returned.
//
// If maxAge is 0, any cached data are always returned.
func (c *Cache) LoadOrStore(name string, maxAge time.Duration, reload func() ([]byte, error)) ([]byte, error) {
func (c Cache) LoadOrStore(name string, maxAge time.Duration, reload func() ([]byte, error)) ([]byte, error) {
var load bool
age, err := c.Age(name)
if err != nil {
Expand Down Expand Up @@ -134,7 +134,7 @@ func (c *Cache) LoadOrStore(name string, maxAge time.Duration, reload func() ([]
// unmarshalled into v.
//
// If maxAge is 0, any cached data are loaded regardless of age.
func (c *Cache) LoadOrStoreJSON(name string, maxAge time.Duration, reload func() (interface{}, error), v interface{}) error {
func (c Cache) LoadOrStoreJSON(name string, maxAge time.Duration, reload func() (interface{}, error), v interface{}) error {
var (
load bool
data []byte
Expand Down Expand Up @@ -170,10 +170,10 @@ func (c *Cache) LoadOrStoreJSON(name string, maxAge time.Duration, reload func()
}

// Exists returns true if the named cache exists.
func (c *Cache) Exists(name string) bool { return util.PathExists(c.path(name)) }
func (c Cache) Exists(name string) bool { return util.PathExists(c.path(name)) }

// Expired returns true if the named cache does not exist or is older than maxAge.
func (c *Cache) Expired(name string, maxAge time.Duration) bool {
func (c Cache) Expired(name string, maxAge time.Duration) bool {
age, err := c.Age(name)
if err != nil {
return true
Expand All @@ -182,7 +182,7 @@ func (c *Cache) Expired(name string, maxAge time.Duration) bool {
}

// Age returns the age of the data cached at name.
func (c *Cache) Age(name string) (time.Duration, error) {
func (c Cache) Age(name string) (time.Duration, error) {
p := c.path(name)
fi, err := os.Stat(p)
if err != nil {
Expand All @@ -192,7 +192,7 @@ func (c *Cache) Age(name string) (time.Duration, error) {
}

// path returns the path to a named file within cache directory.
func (c *Cache) path(name string) string { return filepath.Join(c.Dir, name) }
func (c Cache) path(name string) string { return filepath.Join(c.Dir, name) }

// Session is a Cache that is tied to the `sessionID` value passed to NewSession().
//
Expand Down Expand Up @@ -234,7 +234,7 @@ func NewSessionID() string {

// Clear removes session-scoped cache data. If current is true, it also removes
// data cached for the current session.
func (s *Session) Clear(current bool) error {
func (s Session) Clear(current bool) error {
prefix := sessionPrefix + "."
curPrefix := fmt.Sprintf("%s.%s.", sessionPrefix, s.SessionID)

Expand All @@ -258,47 +258,47 @@ func (s *Session) Clear(current bool) error {

// Store saves data under the given name. If len(data) is 0, the file is
// deleted.
func (s *Session) Store(name string, data []byte) error {
func (s Session) Store(name string, data []byte) error {
return s.cache.Store(s.name(name), data)
}

// StoreJSON serialises v to JSON and saves it to the cache. If v is nil,
// the cache is deleted.
func (s *Session) StoreJSON(name string, v interface{}) error {
func (s Session) StoreJSON(name string, v interface{}) error {
return s.cache.StoreJSON(s.name(name), v)
}

// Load reads data saved under given name.
func (s *Session) Load(name string) ([]byte, error) {
func (s Session) Load(name string) ([]byte, error) {
return s.cache.Load(s.name(name))
}

// LoadJSON unmarshals a cache into v.
func (s *Session) LoadJSON(name string, v interface{}) error {
func (s Session) LoadJSON(name string, v interface{}) error {
return s.cache.LoadJSON(s.name(name), v)
}

// LoadOrStore loads data from cache if they exist. If data do not exist,
// reload is called, and the resulting data are cached & returned.
//
// If maxAge is 0, any cached data are always returned.
func (s *Session) LoadOrStore(name string, reload func() ([]byte, error)) ([]byte, error) {
func (s Session) LoadOrStore(name string, reload func() ([]byte, error)) ([]byte, error) {
return s.cache.LoadOrStore(s.name(name), 0, reload)
}

// LoadOrStoreJSON loads JSON-serialised data from cache if they exist.
// If the data do not exist, reload is called, and the resulting interface{}
// is cached and returned.
func (s *Session) LoadOrStoreJSON(name string, reload func() (interface{}, error), v interface{}) error {
func (s Session) LoadOrStoreJSON(name string, reload func() (interface{}, error), v interface{}) error {
return s.cache.LoadOrStoreJSON(s.name(name), 0, reload, v)
}

// Exists returns true if the named cache exists.
func (s *Session) Exists(name string) bool {
func (s Session) Exists(name string) bool {
return s.cache.Exists(s.name(name))
}

// name prefixes name with session prefix and session ID.
func (s *Session) name(name string) string {
func (s Session) name(name string) string {
return fmt.Sprintf("%s.%s.%s", sessionPrefix, s.SessionID, name)
}
39 changes: 35 additions & 4 deletions magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,53 @@ func (ma MagicActions) Unregister(actions ...MagicAction) {
//
// If not magic actions are found, it returns args.
func (ma MagicActions) Args(args []string, prefix string) []string {

args, handled := ma.handleArgs(args, prefix)

if handled {
finishLog(false)
os.Exit(0)
}

return args

}

// handleArgs checks args for the magic prefix. Returns args and true if
// it found and handled a magic argument.
func (ma MagicActions) handleArgs(args []string, prefix string) ([]string, bool) {

var handled bool

for _, arg := range args {

arg = strings.TrimSpace(arg)

if strings.HasPrefix(arg, prefix) {

query := arg[len(prefix):]
action := ma[query]

if action != nil {

log.Printf(action.RunText())

NewItem(action.RunText()).
Icon(IconInfo).
Valid(false)

SendFeedback()

if err := action.Run(); err != nil {
log.Printf("Error running magic arg `%s`: %s", action.Description(), err)
finishLog(true)
}
finishLog(false)
os.Exit(0)

handled = true

} else {
for kw, action := range ma {

NewItem(action.Keyword()).
Subtitle(action.Description()).
Valid(false).
Expand All @@ -152,14 +180,17 @@ func (ma MagicActions) Args(args []string, prefix string) []string {
Autocomplete(prefix + kw).
Match(fmt.Sprintf("%s %s", action.Keyword(), action.Description()))
}

Filter(query)
WarnEmpty("No matching action", "Try another query?")
SendFeedback()
os.Exit(0)

handled = true
}
}
}
return args

return args, handled
}

// Opens workflow's log file.
Expand Down
Loading

0 comments on commit 28cc53e

Please sign in to comment.