-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
plugn.go
128 lines (111 loc) · 2.74 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"embed"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/progrium/go-basher"
)
var Version string
var PluginPath string
var (
//go:embed bashenv/*
fs embed.FS
)
func Asset(name string) ([]byte, error) {
return fs.ReadFile(filepath.ToSlash(name))
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func TomlGet(args []string) {
bytes, err := io.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 := io.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 := io.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 isArg(argument string) bool {
return len(os.Args) > 1 && os.Args[1] == argument
}
func isVersionArg() bool {
return isArg("version") || isArg("--version") || isArg("-v")
}
func main() {
os.Setenv("PLUGN_VERSION", Version)
if data, err := os.ReadFile(".plugn"); err == nil {
if path, err := filepath.Abs(string(data)); err == nil {
os.Setenv("PLUGIN_PATH", path)
}
}
if !isVersionArg() && os.Getenv("PLUGIN_PATH") == "" {
fmt.Println("!! PLUGIN_PATH is not set in environment")
os.Exit(2)
}
if isVersionArg() {
os.Args[1] = "version"
}
PluginPath = os.Getenv("PLUGIN_PATH")
if isArg("gateway") {
runGateway()
return
}
funcs := map[string]func([]string){
"toml-get": TomlGet,
"toml-set": TomlSet,
"toml-export": TomlExport,
"trigger-gateway": TriggerGateway,
"reload-gateway": ReloadGateway,
}
scripts := []string{
"bashenv/bash.bash",
"bashenv/fn.bash",
"bashenv/cmd.bash",
"bashenv/plugn.bash",
}
if os.Getenv("BASH_BIN") == "" {
basher.Application(funcs, scripts, Asset, true)
} else {
basher.ApplicationWithPath(funcs, scripts, Asset, true, os.Getenv("BASH_BIN"))
}
}