Better Plugin Parsing

Former-commit-id: 8f0d86770072714575223249ab59cb0bcb11d1af [formerly 745c8318de08dc06bd9eece0d185528e4ed0872c] [formerly 61cda90693313822ad2e106dbc5a64104f2b5716 [formerly 722ca4b47f]]
Former-commit-id: c2dad3e81d44f3a92c61d3c21eb6d2113b333ed4 [formerly 898013a275bf0e2b86395f3fa8d5479b08c692d8]
Former-commit-id: 6e7c0f06a65cecdb2307df35db361f24d2353e00
This commit is contained in:
Henrique Dias 2017-07-29 11:32:07 +01:00
parent 5968111f3e
commit 3c9762ee97
3 changed files with 69 additions and 49 deletions

View File

@ -11,7 +11,7 @@
<template v-for="plugin in plugins"> <template v-for="plugin in plugins">
<h2>{{ capitalize(plugin.name) }}</h2> <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> <label v-if="field.type !== 'checkbox'">{{ field.name }}</label>
<input v-if="field.type === 'text'" type="text" v-model.trim="field.value"> <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"> <input v-else-if="field.type === 'checkbox'" type="checkbox" v-model.trim="field.value">
@ -69,47 +69,8 @@ export default {
api.getPlugins() api.getPlugins()
.then(plugins => { .then(plugins => {
console.log(plugins)
let plugin = {}
for (let key in plugins) { for (let key in plugins) {
plugin.name = key this.plugins.push(this.parsePlugin(key, plugins[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)
} }
}) })
.catch(error => { this.showError(error) }) .catch(error => { this.showError(error) })
@ -153,7 +114,7 @@ export default {
let p = {} let p = {}
for (let field of plugin.fields) { for (let field of plugin.fields) {
p[field.name] = field.value p[field.variable] = field.value
if (field.original === 'array') { if (field.original === 'array') {
let val = field.value.split(' ') let val = field.value.split(' ')
@ -161,7 +122,7 @@ export default {
val.shift() val.shift()
} }
p[field.name] = val p[field.variable] = val
} }
} }
@ -173,6 +134,43 @@ export default {
api.updatePlugins(plugins) api.updatePlugins(plugins)
.then(() => { this.showSuccess('Plugins settings updated!') }) .then(() => { this.showSuccess('Plugins settings updated!') })
.catch(error => { this.showError(error) }) .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. // Hugo is a hugo (https://gohugo.io) plugin.
type Hugo struct { type Hugo struct {
// Website root // 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 folder
Public string `description:"The relative or absolute path to the public folder."` Public string `name:"Public Directory"`
// Hugo executable path // 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 // 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. // 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 { func (h *Hugo) Find() error {

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"net/http" "net/http"
"reflect"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
) )
@ -63,12 +64,33 @@ func pluginsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (
return http.StatusMethodNotAllowed, nil 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) { func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin { if !c.User.Admin {
return http.StatusForbidden, nil 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) { func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {