From 0405b36ab4ee6456cd6c0b97c0fecc87c76b76d1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 11 Feb 2021 06:41:11 -0500 Subject: [PATCH] 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 :) --- docs/development/plugin-triggers.md | 15 ++++++++++ plugins/20_events/config-export | 1 + plugins/config/Makefile | 2 +- plugins/config/functions.go | 38 +++++++++++++++++++++++++ plugins/config/src/triggers/triggers.go | 6 ++++ plugins/config/subcommands.go | 33 +-------------------- plugins/config/triggers.go | 19 ++++++++++++- 7 files changed, 80 insertions(+), 34 deletions(-) create mode 120000 plugins/20_events/config-export create mode 100644 plugins/config/functions.go diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index 0f6db0532..37acb4e32 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -261,6 +261,21 @@ help_content 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` - Description: Fetches the app config value for a key diff --git a/plugins/20_events/config-export b/plugins/20_events/config-export new file mode 120000 index 000000000..5178a749f --- /dev/null +++ b/plugins/20_events/config-export @@ -0,0 +1 @@ +hook \ No newline at end of file diff --git a/plugins/config/Makefile b/plugins/config/Makefile index 8fd01f747..7be217943 100644 --- a/plugins/config/Makefile +++ b/plugins/config/Makefile @@ -1,5 +1,5 @@ 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 PLUGIN_NAME = config diff --git a/plugins/config/functions.go b/plugins/config/functions.go new file mode 100644 index 000000000..72b46aa7a --- /dev/null +++ b/plugins/config/functions.go @@ -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 +} diff --git a/plugins/config/src/triggers/triggers.go b/plugins/config/src/triggers/triggers.go index 0ad9f4446..f55d80786 100644 --- a/plugins/config/src/triggers/triggers.go +++ b/plugins/config/src/triggers/triggers.go @@ -18,6 +18,12 @@ func main() { var err error 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": appName := flag.Arg(0) key := flag.Arg(1) diff --git a/plugins/config/subcommands.go b/plugins/config/subcommands.go index fdf30af52..033b45dd2 100644 --- a/plugins/config/subcommands.go +++ b/plugins/config/subcommands.go @@ -33,38 +33,7 @@ func CommandClear(appName string, global bool, noRestart bool) error { // CommandExport implements config:export func CommandExport(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 + return export(appName, global, merged, format) } // CommandGet implements config:get diff --git a/plugins/config/triggers.go b/plugins/config/triggers.go index 6bebf8230..7bcfbb50f 100644 --- a/plugins/config/triggers.go +++ b/plugins/config/triggers.go @@ -1,6 +1,23 @@ 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 func TriggerConfigGet(appName string, key string) error {