diff --git a/cleanenv.go b/cleanenv.go index ce0d0d2..deef9e1 100644 --- a/cleanenv.go +++ b/cleanenv.go @@ -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]) } diff --git a/cleanenv_test.go b/cleanenv_test.go index 0a13fcb..535d7d0 100644 --- a/cleanenv_test.go +++ b/cleanenv_test.go @@ -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 { @@ -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,