Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required description #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions cleanenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,11 @@ func GetDescription(cfg interface{}, headerText *string) (string, error) {
}

for idx, env := range m.envList {

elemDescription := fmt.Sprintf("\n %s %s", env, m.fieldValue.Kind())
required := " "
if m.required && idx == 0 {
required = " *"
}
elemDescription := fmt.Sprintf("\n%s%s %s", required, env, m.fieldValue.Kind())
if idx > 0 {
elemDescription += fmt.Sprintf(" (alternative to %s)", m.envList[0])
}
Expand Down
15 changes: 14 additions & 1 deletion cleanenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,11 @@ func TestGetDescription(t *testing.T) {
Three int
}

type testRequired struct {
Required string `env:"REQUIRED" env-required:"true" env-description:"a required field"`
Optional string `env:"OPTIONAL" env-description:"an optional field"`
}

header := "test header:"

tests := []struct {
Expand Down Expand Up @@ -876,7 +881,15 @@ func TestGetDescription(t *testing.T) {
"\n TWO int\n \ttwo",
wantErr: false,
},

{
name: "required",
cfg: &testRequired{},
header: nil,
want: "Environment variables:" +
"\n OPTIONAL string\n \tan optional field" +
"\n *REQUIRED string\n \ta required field",
wantErr: false,
},
{
name: "error",
cfg: 123,
Expand Down