feat(plugins): polish to plugin page, better handling for reading readme file contents

This commit is contained in:
Torkel Ödegaard
2016-03-13 19:21:44 +01:00
parent 4a3c19c666
commit 581ffb862c
9 changed files with 75 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package plugins
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -155,3 +156,31 @@ func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
reader.Seek(0, 0)
return loader.Load(jsonParser, currentDir)
}
func GetPluginReadme(pluginId string) ([]byte, error) {
plug, exists := Plugins[pluginId]
if !exists {
return nil, PluginNotFoundError{pluginId}
}
if plug.Readme != nil {
return plug.Readme, nil
}
readmePath := filepath.Join(plug.PluginDir, "README.md")
if _, err := os.Stat(readmePath); os.IsNotExist(err) {
readmePath = filepath.Join(plug.PluginDir, "readme.md")
}
if _, err := os.Stat(readmePath); os.IsNotExist(err) {
plug.Readme = make([]byte, 0)
return plug.Readme, nil
}
if readmeBytes, err := ioutil.ReadFile(readmePath); err != nil {
return nil, err
} else {
plug.Readme = readmeBytes
return plug.Readme, nil
}
}