Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
rename EnvconfigAnnotation to EnvconfigAnnot
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Apr 1, 2021
1 parent 2440f43 commit e210162
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Generate usage documentation ([USAGE.md](USAGE.md)) and .env file
```go
// in typical-build

&typcfg.EnvconfigAnnotation{
&typcfg.EnvconfigAnnot{
DotEnv: ".env", // generate .env file
UsageDoc: "USAGE.md", // generate USAGE.md
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

type (
// EnvconfigAnnotation handle @envconfig annotation
// EnvconfigAnnot handle @envconfig annotation
// e.g. `@envconfig (prefix: "PREFIX" ctor:"CTOR")`
EnvconfigAnnotation struct {
TagName string // By default is `@envconfig`
Template string // By default defined in defaultCfgTemplate variable
Target string // By default is `cmd/PROJECT_NAME/envconfig_annotated.go`
DotEnv string // Dotenv path. It will be generated if not empty
UsageDoc string // Usage documentation path. It will be if not emtpy
EnvconfigAnnot struct {
TagName string // By default is `@envconfig`
Template string // By default defined in defaultCfgTemplate variable
Target string // By default is `cmd/PROJECT_NAME/envconfig_annotated.go`
GenDotEnv string // Dotenv path. It will be generated if not empty
GenDoc string // Usage documentation path. It will be if not emtpy
}
// EnvconfigTmplData template
EnvconfigTmplData struct {
Expand Down Expand Up @@ -80,13 +80,13 @@ func {{$c.FnName}}() (*{{$c.SpecType}}, error) {
`

//
// EnvconfigAnnotation
// EnvconfigAnnot
//

var _ typast.Annotator = (*EnvconfigAnnotation)(nil)
var _ typast.Annotator = (*EnvconfigAnnot)(nil)

// Annotate Envconfig to prepare dependency-injection and env-file
func (m *EnvconfigAnnotation) Annotate(c *typast.Context) error {
func (m *EnvconfigAnnot) Annotate(c *typast.Context) error {
context := m.Context(c)
target := m.getTarget(context)

Expand All @@ -96,14 +96,14 @@ func (m *EnvconfigAnnotation) Annotate(c *typast.Context) error {
return err
}

if m.DotEnv != "" {
if err := GenerateAndLoadDotEnv(m.DotEnv, context); err != nil {
if m.GenDotEnv != "" {
if err := GenerateAndLoadDotEnv(m.GenDotEnv, context); err != nil {
return err
}
}

if m.UsageDoc != "" {
if err := GenerateUsage(m.UsageDoc, context); err != nil {
if m.GenDoc != "" {
if err := GenerateDoc(m.GenDoc, context); err != nil {
return err
}
}
Expand All @@ -112,7 +112,7 @@ func (m *EnvconfigAnnotation) Annotate(c *typast.Context) error {
}

// Context create context instance
func (m *EnvconfigAnnotation) Context(c *typast.Context) *Context {
func (m *EnvconfigAnnot) Context(c *typast.Context) *Context {
var configs []*Envconfig

importAliases := typast.NewImportAliases()
Expand All @@ -129,7 +129,7 @@ func (m *EnvconfigAnnotation) Context(c *typast.Context) *Context {
return &Context{Context: c, Configs: configs, Imports: importAliases.Map}
}

func (m *EnvconfigAnnotation) generate(c *Context, target string) error {
func (m *EnvconfigAnnot) generate(c *Context, target string) error {

dest := filepath.Dir(target)
os.MkdirAll(dest, 0777)
Expand All @@ -147,21 +147,21 @@ func (m *EnvconfigAnnotation) generate(c *Context, target string) error {
return nil
}

func (m *EnvconfigAnnotation) getTagName() string {
func (m *EnvconfigAnnot) getTagName() string {
if m.TagName == "" {
m.TagName = "@envconfig"
}
return m.TagName
}

func (m *EnvconfigAnnotation) getTemplate() string {
func (m *EnvconfigAnnot) getTemplate() string {
if m.Template == "" {
m.Template = defaultCfgTemplate
}
return m.Template
}

func (m *EnvconfigAnnotation) getTarget(c *Context) string {
func (m *EnvconfigAnnot) getTarget(c *Context) string {
if m.Target == "" {
m.Target = defaultCfgTarget
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCfgAnnotation_Annotate(t *testing.T) {
typgo.ProjectPkg = "github.com/user/project"
defer os.RemoveAll("internal")

EnvconfigAnnotation := &typcfg.EnvconfigAnnotation{}
EnvconfigAnnot := &typcfg.EnvconfigAnnot{}
var out strings.Builder
c := &typgo.Context{Logger: typgo.Logger{Stdout: &out}}
defer c.PatchBash([]*typgo.MockBash{})(t)
Expand All @@ -44,7 +44,7 @@ func TestCfgAnnotation_Annotate(t *testing.T) {
},
}

require.NoError(t, EnvconfigAnnotation.Annotate(ac))
require.NoError(t, EnvconfigAnnot.Annotate(ac))

b, _ := ioutil.ReadFile("internal/generated/envcfg/envcfg.go")
require.Equal(t, `package envcfg
Expand Down Expand Up @@ -82,11 +82,11 @@ func TestCfgAnnotation_Annotate_GenerateDotEnvAndUsageDoc(t *testing.T) {
defer os.Clearenv()
defer os.RemoveAll("folder")

a := &typcfg.EnvconfigAnnotation{
Target: "folder/some-target",
Template: "some-template",
DotEnv: ".env33",
UsageDoc: "some-usage.md",
a := &typcfg.EnvconfigAnnot{
Target: "folder/some-target",
Template: "some-template",
GenDotEnv: ".env33",
GenDoc: "some-usage.md",
}

var out strings.Builder
Expand Down Expand Up @@ -118,13 +118,13 @@ func TestCfgAnnotation_Annotate_GenerateDotEnvAndUsageDoc(t *testing.T) {

require.NoError(t, a.Annotate(ac))
defer os.Remove(a.Target)
defer os.Remove(a.DotEnv)
defer os.Remove(a.UsageDoc)
defer os.Remove(a.GenDotEnv)
defer os.Remove(a.GenDoc)

b, _ := ioutil.ReadFile(a.Target)
require.Equal(t, `some-template`, string(b))

b, _ = ioutil.ReadFile(a.DotEnv)
b, _ = ioutil.ReadFile(a.GenDotEnv)
require.Equal(t, "SS_SOMEFIELD1=some-text\nSS_SOMEFIELD2=9876\n", string(b))
require.Equal(t, "some-text", os.Getenv("SS_SOMEFIELD1"))
require.Equal(t, "9876", os.Getenv("SS_SOMEFIELD2"))
Expand All @@ -136,7 +136,7 @@ func TestCfgAnnotation_Annotate_Predefined(t *testing.T) {

defer os.RemoveAll("predefined")

EnvconfigAnnotation := &typcfg.EnvconfigAnnotation{
EnvconfigAnnot := &typcfg.EnvconfigAnnot{
TagName: "@some-tag",
Template: "some-template",
Target: "predefined/cfg-target",
Expand All @@ -160,7 +160,7 @@ func TestCfgAnnotation_Annotate_Predefined(t *testing.T) {
},
},
}
require.NoError(t, EnvconfigAnnotation.Annotate(ac))
require.NoError(t, EnvconfigAnnot.Annotate(ac))

b, _ := ioutil.ReadFile("predefined/cfg-target")
require.Equal(t, `some-template`, string(b))
Expand All @@ -175,8 +175,8 @@ func TestCfgAnnotation_Annotate_RemoveTargetWhenNoAnnotation(t *testing.T) {
Summary: &typast.Summary{},
}

EnvconfigAnnotation := &typcfg.EnvconfigAnnotation{Target: target}
require.NoError(t, EnvconfigAnnotation.Annotate(c))
EnvconfigAnnot := &typcfg.EnvconfigAnnot{Target: target}
require.NoError(t, EnvconfigAnnot.Annotate(c))
_, err := os.Stat(target)
require.True(t, os.IsNotExist(err))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/typcfg/generate_usage.go → pkg/typcfg/generate_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const usageTmpl = `# {{.ProjectName}}
{{.EnvSnippet}}
`

// GenerateUsage generate usage document
func GenerateUsage(target string, c *Context) error {
// GenerateDoc generate usage document
func GenerateDoc(target string, c *Context) error {
fields := fields(c)
c.Infof("Generate '%s'\n", target)
return tmplkit.WriteFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/urfave/cli/v2"
)

func TestGenerateUsage(t *testing.T) {
func TestGenerateDoc(t *testing.T) {
var out strings.Builder
target := "sample.md"
c := &typcfg.Context{
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestGenerateUsage(t *testing.T) {
},
},
}
err := typcfg.GenerateUsage(target, c)
err := typcfg.GenerateDoc(target, c)
require.NoError(t, err)
defer os.Remove(target)

Expand Down
2 changes: 1 addition & 1 deletion tools/typical-build/typical-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var descriptor = typgo.Descriptor{
Annotators: []typast.Annotator{
&typapp.CtorAnnotation{},
&typrepo.EntityAnnotation{},
&typcfg.EnvconfigAnnotation{DotEnv: ".env", UsageDoc: "USAGE.md"},
&typcfg.EnvconfigAnnot{GenDotEnv: ".env", GenDoc: "USAGE.md"},
},
},
// test
Expand Down

0 comments on commit e210162

Please sign in to comment.