This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
forked from dokku/plugn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugn.go
99 lines (88 loc) · 2.24 KB
/
plugn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/progrium/go-basher"
)
var Version string
var PluginPath string
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func TomlGet(args []string) {
bytes, err := ioutil.ReadAll(os.Stdin)
assert(err)
var t map[string]interface{}
_, err = toml.Decode(string(bytes), &t)
assert(err)
fmt.Println(t[args[0]].(map[string]interface{})[args[1]])
}
func TomlExport(args []string) {
plugin := args[0]
bytes, err := ioutil.ReadAll(os.Stdin)
assert(err)
var c map[string]map[string]string
_, err = toml.Decode(string(bytes), &c)
assert(err)
config := c[plugin]
prefix := strings.ToUpper(strings.Replace(plugin, "-", "_", -1))
var p map[string]map[string]interface{}
_, err = toml.DecodeFile(PluginPath+"/available/"+plugin+"/plugin.toml", &p)
assert(err)
config_def := p["plugin"]["config"].(map[string]interface{})
for key := range config_def {
k := strings.ToUpper(strings.Replace(key, "-", "_", -1))
fmt.Println("export CONFIG_" + k + "=\"${" + prefix + "_" + k + ":-\"" + config[key] + "\"}\"")
}
}
func TomlSet(args []string) {
bytes, err := ioutil.ReadAll(os.Stdin)
assert(err)
var t map[string]map[string]string
_, err = toml.DecodeFile(args[0], &t)
assert(err)
if t[args[1]] == nil {
t[args[1]] = make(map[string]string)
}
t[args[1]][args[2]] = string(bytes)
f, err := os.Create(args[0])
assert(err)
assert(toml.NewEncoder(f).Encode(t))
f.Close()
}
func main() {
if data, err := ioutil.ReadFile(".plugn"); err == nil {
if path, err := filepath.Abs(string(data)); err == nil {
os.Setenv("PLUGIN_PATH", path)
}
}
if os.Getenv("PLUGIN_PATH") == "" {
fmt.Println("!! PLUGIN_PATH is not set in environment")
os.Exit(2)
}
PluginPath = os.Getenv("PLUGIN_PATH")
if len(os.Args) > 1 && os.Args[1] == "gateway" {
runGateway()
return
}
os.Setenv("VERSION", Version)
basher.Application(map[string]func([]string){
"toml-get": TomlGet,
"toml-set": TomlSet,
"toml-export": TomlExport,
"trigger-gateway": TriggerGateway,
"reload-gateway": ReloadGateway,
}, []string{
"bashenv/bash.bash",
"bashenv/fn.bash",
"bashenv/cmd.bash",
"bashenv/plugn.bash",
}, Asset, true)
}