Skip to content

Commit

Permalink
Switch to pkg over dir for finding config struct (#31)
Browse files Browse the repository at this point in the history
* switch to -pkg over -dir for finding config struct
  • Loading branch information
miniscruff authored Sep 12, 2023
1 parent 08c92da commit e75d6a6
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changes/v0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.1.2 on 2023-09-11
### Fixed
* [#30](https://github.com/miniscruff/envexample/issues/30) Switch to import package path instead of local directory
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).


## v0.1.2 on 2023-09-11
### Fixed
* [#30](https://github.com/miniscruff/envexample/issues/30) Switch to import package path instead of local directory

## v0.1.1 on 2023-09-10
### Fixed
* [#19](https://github.com/miniscruff/envexample/issues/19) Using an external struct not loading fields properly
Expand Down
6 changes: 3 additions & 3 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Config struct {
// Our configs for parsing and building
ExportFile string
ConfigStruct string
Directory string
Package string
Version string
DryRun bool
ShowVersion bool
Expand All @@ -35,8 +35,8 @@ func NewConfig(args []string) (*Config, error) {
}

flagSet.StringVar(&cfg.ExportFile, "export", ".env.example", "`filepath` to export generated example to")
flagSet.StringVar(&cfg.ConfigStruct, "type", "", "`struct` to build example from")
flagSet.StringVar(&cfg.Directory, "dir", ".", "`directory` our config struct is located in")
flagSet.StringVar(&cfg.ConfigStruct, "type", "", "struct to build example from")
flagSet.StringVar(&cfg.Package, "pkg", "", "import package our config struct is located in")
flagSet.BoolVar(&cfg.DryRun, "dry", false, "output to stdout instead of writing to file")
flagSet.BoolVar(&cfg.ShowVersion, "v", false, "show version")
flagSet.BoolVar(&cfg.ShowHelp, "h", false, "show help")
Expand Down
8 changes: 4 additions & 4 deletions pkg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_Config_Defaults(t *testing.T) {
then.Nil(t, err)
then.Equals(t, ".env.example", cfg.ExportFile)
then.Equals(t, "", cfg.ConfigStruct)
then.Equals(t, ".", cfg.Directory)
then.Equals(t, "", cfg.Package)
then.False(t, cfg.DryRun)
then.False(t, cfg.ShowVersion)
then.False(t, cfg.ShowHelp)
Expand All @@ -28,7 +28,7 @@ func Test_Config_OverrideDefaults(t *testing.T) {
args := []string{
"-export", "test.env.example",
"-type", "myStruct",
"-dir", "config",
"-pkg", "github.com/your/project/config",
"-prefix", "C_",
"-tag", "cenv",
"-required-if-no-def",
Expand All @@ -40,7 +40,7 @@ func Test_Config_OverrideDefaults(t *testing.T) {
then.Nil(t, err)
then.Equals(t, "test.env.example", cfg.ExportFile)
then.Equals(t, "myStruct", cfg.ConfigStruct)
then.Equals(t, "config", cfg.Directory)
then.Equals(t, "github.com/your/project/config", cfg.Package)
then.Equals(t, "cenv", cfg.TagName)
then.Equals(t, "C_", cfg.Prefix)
then.True(t, cfg.DryRun)
Expand All @@ -53,7 +53,7 @@ func Test_Config_OverrideDefaults(t *testing.T) {
func Test_Config_ValidArgs(t *testing.T) {
args := []string{
"-type", "MyConfig",
"-dir", "config",
"-pkg", "github.com/your/project/config",
}

cfg, err := NewConfig(args)
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Generator struct {
func NewGenerator(cfg *Config) (*Generator, error) {
builder := parser.New()

err := builder.AddDirRecursive(".")
err := builder.AddDirRecursive(cfg.Package)
if err != nil {
return nil, fmt.Errorf("finding package directory: %w", err)
}
Expand All @@ -47,7 +47,7 @@ func NewGenerator(cfg *Config) (*Generator, error) {
queue: []*StructQueueEntry{
{
TypeName: types.Name{
Package: cfg.Directory,
Package: cfg.Package,
Name: cfg.ConfigStruct,
},
Prefix: cfg.Prefix,
Expand Down
2 changes: 1 addition & 1 deletion pkg/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GoldenTest struct {
func (golden GoldenTest) Run(t *testing.T) {
// override config values used by all golden tests
cfg := golden.Config
cfg.Directory = "."
cfg.Package = "github.com/miniscruff/envexample/testdata"
cfg.Version = "dev"

var writer bytes.Buffer
Expand Down
4 changes: 4 additions & 0 deletions testdata/nested.golden.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

# A is data related to A.
#
# Data related to the a package.
#
# Key of some data.
#A_Key=Value_A

# B is data related to B.
#
# Data related to the b package.
#
# Key of some data.
#B_Key=Value_B

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.1
v0.1.2

0 comments on commit e75d6a6

Please sign in to comment.