Better Plugin Parsing

pull/185/head
Henrique Dias 2017-07-29 11:32:07 +01:00
parent 46fdd869a5
commit 722ca4b47f
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
3 changed files with 69 additions and 49 deletions

View File

@ -11,7 +11,7 @@
<template v-for="plugin in plugins">
<h2>{{ capitalize(plugin.name) }}</h2>
<p v-for="field in plugin.fields" :key="field.name">
<p v-for="field in plugin.fields" :key="field.variable">
<label v-if="field.type !== 'checkbox'">{{ field.name }}</label>
<input v-if="field.type === 'text'" type="text" v-model.trim="field.value">
<input v-else-if="field.type === 'checkbox'" type="checkbox" v-model.trim="field.value">
@ -69,47 +69,8 @@ export default {
api.getPlugins()
.then(plugins => {
console.log(plugins)
let plugin = {}
for (let key in plugins) {
plugin.name = key
plugin.fields = []
for (let field in plugins[key]) {
let value = plugins[key][field]
if (Array.isArray(value)) {
plugin.fields.push({
name: field,
type: 'text',
original: 'array',
value: value.join(' ')
})
continue
}
switch (typeof value) {
case 'boolean':
plugin.fields.push({
name: field,
type: 'checkbox',
original: 'boolean',
value: value
})
break
default:
plugin.fields.push({
name: field,
type: 'text',
original: 'text',
value: value
})
}
}
this.plugins.push(plugin)
this.plugins.push(this.parsePlugin(key, plugins[key]))
}
})
.catch(error => { this.showError(error) })
@ -153,7 +114,7 @@ export default {
let p = {}
for (let field of plugin.fields) {
p[field.name] = field.value
p[field.variable] = field.value
if (field.original === 'array') {
let val = field.value.split(' ')
@ -161,7 +122,7 @@ export default {
val.shift()
}
p[field.name] = val
p[field.variable] = val
}
}
@ -173,6 +134,43 @@ export default {
api.updatePlugins(plugins)
.then(() => { this.showSuccess('Plugins settings updated!') })
.catch(error => { this.showError(error) })
},
parsePlugin (name, plugin) {
let obj = {
name: name,
fields: []
}
for (let option of plugin) {
let value = option.value
let field = {
name: option.name,
variable: option.variable,
type: 'text',
original: 'text',
value: value
}
if (Array.isArray(value)) {
field.original = 'array'
field.value = value.join(' ')
obj.fields.push(field)
continue
}
switch (typeof value) {
case 'boolean':
field.type = 'checkbox'
field.original = 'boolean'
break
}
obj.fields.push(field)
}
return obj
}
}
}

View File

@ -38,15 +38,15 @@ var (
// Hugo is a hugo (https://gohugo.io) plugin.
type Hugo struct {
// Website root
Root string `description:"The relative or absolute path to the place where your website is located."`
Root string `name:"Website Root"`
// Public folder
Public string `description:"The relative or absolute path to the public folder."`
Public string `name:"Public Directory"`
// Hugo executable path
Exe string `description:"The absolute path to the Hugo executable or the command to execute."`
Exe string `name:"Hugo Executable"`
// Hugo arguments
Args []string `description:"The arguments to run when running Hugo"`
Args []string `name:"Hugo Arguments"`
// Indicates if we should clean public before a new publish.
CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
CleanPublic bool `name:"Clean Public"`
}
func (h *Hugo) Find() error {

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"net/http"
"reflect"
"github.com/mitchellh/mapstructure"
)
@ -63,12 +64,33 @@ func pluginsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (
return http.StatusMethodNotAllowed, nil
}
type pluginOption struct {
Variable string `json:"variable"`
Name string `json:"name"`
Value interface{} `json:"value"`
}
func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
return renderJSON(w, c.FM.Plugins)
plugins := map[string][]pluginOption{}
for name, p := range c.FM.Plugins {
plugins[name] = []pluginOption{}
t := reflect.TypeOf(p).Elem()
for i := 0; i < t.NumField(); i++ {
plugins[name] = append(plugins[name], pluginOption{
Variable: t.Field(i).Name,
Name: t.Field(i).Tag.Get("name"),
Value: reflect.ValueOf(p).Elem().FieldByName(t.Field(i).Name).Interface(),
})
}
}
return renderJSON(w, plugins)
}
func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {