fix: implement config-export hook
This will allow dropping many of the "source config/functions" calls littered over the codebase. Plugin triggers are the way to perform cross-plugin communication :)
This commit is contained in:
@@ -261,6 +261,21 @@ help_content
|
|||||||
esac
|
esac
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `config-export`
|
||||||
|
|
||||||
|
- Description: Returns the environment variables in a specified format
|
||||||
|
- Invoked by: app-json plugin
|
||||||
|
- Arguments: `$APP $GLOBAL $MERGED $FORMAT`
|
||||||
|
- Example:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
```
|
||||||
|
|
||||||
### `config-get`
|
### `config-get`
|
||||||
|
|
||||||
- Description: Fetches the app config value for a key
|
- Description: Fetches the app config value for a key
|
||||||
|
|||||||
1
plugins/20_events/config-export
Symbolic link
1
plugins/20_events/config-export
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
hook
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
SUBCOMMANDS = subcommands/bundle subcommands/clear subcommands/export subcommands/get subcommands/keys subcommands/show subcommands/set subcommands/unset
|
SUBCOMMANDS = subcommands/bundle subcommands/clear subcommands/export subcommands/get subcommands/keys subcommands/show subcommands/set subcommands/unset
|
||||||
TRIGGERS = triggers/config-get triggers/config-get-global
|
TRIGGERS = triggers/config-export triggers/config-get triggers/config-get-global
|
||||||
BUILD = commands subcommands triggers
|
BUILD = commands subcommands triggers
|
||||||
PLUGIN_NAME = config
|
PLUGIN_NAME = config
|
||||||
|
|
||||||
|
|||||||
38
plugins/config/functions.go
Normal file
38
plugins/config/functions.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func export(appName string, global bool, merged bool, format string) error {
|
||||||
|
appName, err := getAppNameOrGlobal(appName, global)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
env := getEnvironment(appName, merged)
|
||||||
|
exportType := ExportFormatExports
|
||||||
|
suffix := "\n"
|
||||||
|
|
||||||
|
exportTypes := map[string]ExportFormat{
|
||||||
|
"exports": ExportFormatExports,
|
||||||
|
"envfile": ExportFormatEnvfile,
|
||||||
|
"docker-args": ExportFormatDockerArgs,
|
||||||
|
"docker-args-keys": ExportFormatDockerArgsKeys,
|
||||||
|
"shell": ExportFormatShell,
|
||||||
|
"pretty": ExportFormatPretty,
|
||||||
|
"json": ExportFormatJSON,
|
||||||
|
"json-list": ExportFormatJSONList,
|
||||||
|
}
|
||||||
|
|
||||||
|
exportType, ok := exportTypes[format]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Unknown export format: %v", format)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exportType == ExportFormatShell {
|
||||||
|
suffix = " "
|
||||||
|
}
|
||||||
|
|
||||||
|
exported := env.Export(exportType)
|
||||||
|
fmt.Print(exported + suffix)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -18,6 +18,12 @@ func main() {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
switch trigger {
|
switch trigger {
|
||||||
|
case "config-export":
|
||||||
|
appName := flag.Arg(0)
|
||||||
|
global := flag.Arg(1)
|
||||||
|
merged := flag.Arg(2)
|
||||||
|
format := flag.Arg(3)
|
||||||
|
config.TriggerConfigExport(appName, global, merged, format)
|
||||||
case "config-get":
|
case "config-get":
|
||||||
appName := flag.Arg(0)
|
appName := flag.Arg(0)
|
||||||
key := flag.Arg(1)
|
key := flag.Arg(1)
|
||||||
|
|||||||
@@ -33,38 +33,7 @@ func CommandClear(appName string, global bool, noRestart bool) error {
|
|||||||
|
|
||||||
// CommandExport implements config:export
|
// CommandExport implements config:export
|
||||||
func CommandExport(appName string, global bool, merged bool, format string) error {
|
func CommandExport(appName string, global bool, merged bool, format string) error {
|
||||||
appName, err := getAppNameOrGlobal(appName, global)
|
return export(appName, global, merged, format)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
env := getEnvironment(appName, merged)
|
|
||||||
exportType := ExportFormatExports
|
|
||||||
suffix := "\n"
|
|
||||||
|
|
||||||
exportTypes := map[string]ExportFormat{
|
|
||||||
"exports": ExportFormatExports,
|
|
||||||
"envfile": ExportFormatEnvfile,
|
|
||||||
"docker-args": ExportFormatDockerArgs,
|
|
||||||
"docker-args-keys": ExportFormatDockerArgsKeys,
|
|
||||||
"shell": ExportFormatShell,
|
|
||||||
"pretty": ExportFormatPretty,
|
|
||||||
"json": ExportFormatJSON,
|
|
||||||
"json-list": ExportFormatJSONList,
|
|
||||||
}
|
|
||||||
|
|
||||||
exportType, ok := exportTypes[format]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Unknown export format: %v", format)
|
|
||||||
}
|
|
||||||
|
|
||||||
if exportType == ExportFormatShell {
|
|
||||||
suffix = " "
|
|
||||||
}
|
|
||||||
|
|
||||||
exported := env.Export(exportType)
|
|
||||||
fmt.Print(exported + suffix)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandGet implements config:get
|
// CommandGet implements config:get
|
||||||
|
|||||||
@@ -1,6 +1,23 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TriggerConfigExport returns a global config value by key
|
||||||
|
func TriggerConfigExport(appName string, global string, merged string, format string) error {
|
||||||
|
g, err := strconv.ParseBool(global)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := strconv.ParseBool(merged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return export(appName, g, m, format)
|
||||||
|
}
|
||||||
|
|
||||||
// TriggerConfigGet returns an app config value by key
|
// TriggerConfigGet returns an app config value by key
|
||||||
func TriggerConfigGet(appName string, key string) error {
|
func TriggerConfigGet(appName string, key string) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user