generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
238 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package dotenv is a Ferrite mode that exports the environment variables to a | ||
// file suitable use as a .env file. | ||
package dotenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dotenv | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/dogmatiq/ferrite/internal/mode" | ||
"github.com/dogmatiq/iago/must" | ||
) | ||
|
||
// Run generates and env file describing the environment variables and their | ||
// current values. | ||
func Run(opts mode.Options) { | ||
for i, v := range opts.Registry.Variables() { | ||
s := v.Spec() | ||
|
||
if i > 0 { | ||
must.Fprintf(opts.Out, "\n") | ||
} | ||
|
||
must.Fprintf(opts.Out, "# %s (", s.Description()) | ||
|
||
if def, ok := s.Default(); ok { | ||
x := def.Quote() | ||
if s.IsSensitive() { | ||
x = strings.Repeat("*", len(def.String)) | ||
} | ||
must.Fprintf(opts.Out, "default: %s", x) | ||
} else if s.IsDeprecated() { | ||
must.Fprintf(opts.Out, "deprecated") | ||
} else if s.IsRequired() { | ||
must.Fprintf(opts.Out, "required") | ||
} else { | ||
must.Fprintf(opts.Out, "optional") | ||
} | ||
|
||
if s.IsSensitive() { | ||
must.Fprintf(opts.Out, ", sensitive") | ||
} | ||
|
||
must.Fprintf(opts.Out, ")\n") | ||
must.Fprintf(opts.Out, "%s=", s.Name()) | ||
|
||
value, ok, err := v.Value() | ||
if err != nil { | ||
must.Fprintf( | ||
opts.Out, | ||
" # %s is invalid: %s", | ||
err.Literal().Quote(), | ||
err.Unwrap(), | ||
) | ||
} else if ok && v.IsExplicit() && !s.IsSensitive() { | ||
must.Fprintf( | ||
opts.Out, | ||
"%s", | ||
value.Verbatim().Quote(), | ||
) | ||
|
||
if value.Verbatim() != value.Canonical() { | ||
must.Fprintf( | ||
opts.Out, | ||
" # equivalent to %s", | ||
value.Canonical().Quote(), | ||
) | ||
} | ||
} | ||
|
||
must.Fprintf(opts.Out, "\n") | ||
} | ||
|
||
opts.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package ferrite_test | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/dogmatiq/ferrite" | ||
) | ||
|
||
func ExampleInit_exportDotEnvFile() { | ||
defer example()() | ||
|
||
os.Setenv("FERRITE_BOOL", "true") | ||
ferrite. | ||
Bool("FERRITE_BOOL", "example bool"). | ||
Required() | ||
|
||
os.Setenv("FERRITE_DURATION", "620s") | ||
ferrite. | ||
Duration("FERRITE_DURATION", "example duration"). | ||
WithDefault(1 * time.Hour). | ||
Required() | ||
|
||
ferrite. | ||
Enum("FERRITE_ENUM", "example enum"). | ||
WithMembers("foo", "bar", "baz"). | ||
WithDefault("bar"). | ||
Required() | ||
|
||
os.Setenv("FERRITE_NETWORK_PORT", "8080") | ||
ferrite. | ||
NetworkPort("FERRITE_NETWORK_PORT", "example network port"). | ||
Optional() | ||
|
||
ferrite. | ||
Float[float32]("FERRITE_NUM_FLOAT", "example floating-point"). | ||
Required() | ||
|
||
ferrite. | ||
Signed[int16]("FERRITE_NUM_SIGNED", "example signed integer"). | ||
Required() | ||
|
||
ferrite. | ||
Unsigned[uint16]("FERRITE_NUM_UNSIGNED", "example unsigned integer"). | ||
Required() | ||
|
||
os.Setenv("FERRITE_STRING", "hello, world!") | ||
ferrite. | ||
String("FERRITE_STRING", "example string"). | ||
Required() | ||
|
||
os.Setenv("FERRITE_STRING_SENSITIVE", "hunter2") | ||
ferrite. | ||
String("FERRITE_STRING_SENSITIVE", "example sensitive string"). | ||
WithDefault("password"). | ||
WithSensitiveContent(). | ||
Required() | ||
|
||
os.Setenv("FERRITE_SVC_SERVICE_HOST", "host.example.org") | ||
os.Setenv("FERRITE_SVC_SERVICE_PORT", "443") | ||
ferrite. | ||
KubernetesService("ferrite-svc"). | ||
Deprecated() | ||
|
||
os.Setenv("FERRITE_URL", "https//example.org") | ||
ferrite. | ||
URL("FERRITE_URL", "example URL"). | ||
Required() | ||
|
||
// Tell ferrite to export an env file containing the environment variables. | ||
os.Setenv("FERRITE_MODE", "export/dotenv") | ||
|
||
ferrite.Init() | ||
|
||
// Output: | ||
// # example bool (required) | ||
// FERRITE_BOOL=true | ||
// | ||
// # example duration (default: 1h) | ||
// FERRITE_DURATION=620s # equivalent to 10m20s | ||
// | ||
// # example enum (default: bar) | ||
// FERRITE_ENUM= | ||
// | ||
// # example network port (optional) | ||
// FERRITE_NETWORK_PORT=8080 | ||
// | ||
// # example floating-point (required) | ||
// FERRITE_NUM_FLOAT= | ||
// | ||
// # example signed integer (required) | ||
// FERRITE_NUM_SIGNED= | ||
// | ||
// # example unsigned integer (required) | ||
// FERRITE_NUM_UNSIGNED= | ||
// | ||
// # example string (required) | ||
// FERRITE_STRING='hello, world!' | ||
// | ||
// # example sensitive string (default: ********, sensitive) | ||
// FERRITE_STRING_SENSITIVE= | ||
// | ||
// # kubernetes "ferrite-svc" service host (deprecated) | ||
// FERRITE_SVC_SERVICE_HOST=host.example.org | ||
// | ||
// # kubernetes "ferrite-svc" service port (deprecated) | ||
// FERRITE_SVC_SERVICE_PORT=443 | ||
// | ||
// # example URL (required) | ||
// FERRITE_URL= # https//example.org is invalid: URL must have a scheme | ||
// <process exited successfully> | ||
} |