-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
// LocalCommandStore implements CommandStore for a | ||
// local file as storage device. | ||
type LocalCommandStore struct { | ||
loc string | ||
} | ||
|
||
var _ CommandStore = (*LocalCommandStore)(nil) | ||
|
||
// NewLocalCommandStore creates a new instance of | ||
// LocalCommandStore with the passed file location | ||
// as stoage destination. | ||
func NewLocalCommandStore(loc string) *LocalCommandStore { | ||
return &LocalCommandStore{loc} | ||
} | ||
|
||
// NewDefault returns a new LocalCommandStore with | ||
// default file location (".commandCache.json"). | ||
func NewDefault() *LocalCommandStore { | ||
return NewLocalCommandStore(".commandCache.json") | ||
} | ||
|
||
func (lcs *LocalCommandStore) Store(cmds map[string]string) (err error) { | ||
f, err := os.Create(lcs.loc) | ||
if err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
err = json.NewEncoder(f).Encode(cmds) | ||
return | ||
} | ||
|
||
func (lcs *LocalCommandStore) Load() (cmds map[string]string, err error) { | ||
cmds = map[string]string{} | ||
f, err := os.Open(lcs.loc) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
err = nil | ||
} | ||
return | ||
} | ||
defer f.Close() | ||
err = json.NewDecoder(f).Decode(&cmds) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package store | ||
|
||
// CommandStore allows to store and load registered | ||
// commands so that they can be updated instead | ||
// of deleted and re-created on restarts. | ||
type CommandStore interface { | ||
// Store stores the passed commands map. | ||
Store(cmds map[string]string) error | ||
// Load retrieves a stored commands map or | ||
// an empty map, if no store was applied | ||
// before. | ||
Load() (cmds map[string]string, err error) | ||
} |