Adds readme example and fixes logical error in execute

This commit is contained in:
sloumdrone 2019-07-03 12:30:49 -07:00
parent 88eda9d6d5
commit 23f01a7290
2 changed files with 59 additions and 6 deletions

View File

@ -1,5 +1,39 @@
# Mailcap
This is a stub for a Go implementation of the Python3 mailcap library. The goal is to enable the opening of files based on mimetype from programs written in golang.
Golang port of the Python3 mailcap library.
## Usage
An eaxmple showing how to launch a program based on its mime/media type:
```
import(
tildegit.org/sloum/mailcap
)
func main() {
// Build the mailcap database
mc := mailcap.NewMailcap()
// Find an avialable command with the required key and gui avialability
command, err := mc.FindMatch("text/plain", "edit", false)
if err != nil {
panic(err)
}
// Run the command with the filepath provided
err := command.Execute("/var/www/index.html")
if err != nil {
panic(err)
}
// Now that the user has been presented the file to edit,
// something can be done with the updated file...
}
```
In general usage a user will build the mailcap db, find a match, and execute the command they find with a certain filepath.
A few other helper methods are available for debugging or lower level mailcap work.

View File

@ -36,11 +36,17 @@ func (m *Mailcap) GetAllMime(mime string) (Fields, error) {
// Retrieves an Entry for a given mimetype and key.
// Key is required to make sure the returned Entry has
// the capability that is desired. Common keys: "view", "edit".
// Also takes a bool, needsTerm, that can be set to true if the
// command must be executable in a terminal only environment.
//
// Returns and Entry and an error
func (m *Mailcap) FindMatch(mime, key string) (Entry, error) {
func (m *Mailcap) FindMatch(mime, key string, needsTerm bool) (Entry, error) {
entries := m.lookup(mime, key)
for _, v := range entries {
exitCode := 0;
if _, ok := v["needsterminal"]; needsTerm && !ok {
continue
}
if t, ok := v["test"]; ok {
cmdArgs := strings.Split(t, " ")
if len(cmdArgs) < 2 {
@ -57,6 +63,7 @@ func (m *Mailcap) FindMatch(mime, key string) (Entry, error) {
if exitCode != 0 {
continue
}
v["action"] = key
return v, nil
}
@ -70,13 +77,17 @@ func (m *Mailcap) FindMatch(mime, key string) (Entry, error) {
// to declare the target of the program being
// called via the key param. Returns an error
// or nil.
func (e Entry) Execute(key, path string) error {
entryVal, ok := e[key]
func (e Entry) Execute(path string) error {
key, ok := e["action"]
if !ok {
return fmt.Errorf("The key %q does not exist", key)
return fmt.Errorf("No action has been set for this entry, was the entry returned by FindMatch?")
}
command, ok := e[key]
if !ok {
return fmt.Errorf("Entry does not have the key %q", key)
}
matchFields := strings.Fields(entryVal)
matchFields := strings.Fields(command)
for index, field := range matchFields {
if field == "%s" || field == "'%s'" {
matchFields[index] = path
@ -96,6 +107,14 @@ func (e Entry) Execute(key, path string) error {
return nil
}
func (e Entry) SetAction(action string) error {
if _, ok := e[action]; !ok {
return fmt.Errorf("This entry does not have the action %q available", action)
}
e["action"] = action
return nil
}
// Look up all of the Entry types available for
// a given mime and key. Returns an Entry slice (Fields)
func (m *Mailcap) lookup(mime, key string) Fields {