Repository: goreleaser/goreleaser
Pull request #2619 (2021-11-01 03:01:59)

refactor: organize config a bit
just organizing the code a bit...

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2619](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (0c814d5) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/5dc82f78da3df5a86f2317f716f59b79c648a0ce?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (5dc82f7) will **increase** coverage by `0.00%`.
> The diff coverage is `91.30%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2619/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@           Coverage Diff           @@
##           master    #2619   +/-   ##
=======================================
  Coverage   84.65%   84.65%           
=======================================
  Files         100      100           
  Lines        7325     7327    +2     
=======================================
+ Hits         6201     6203    +2     
  Misses        929      929           
  Partials      195      195           
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2619/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `87.15% <90.90%> (+0.14%)` | :arrow_up: |
| [internal/pipe/build/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2619/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9idWlsZC9idWlsZC5nbw==) | `88.39% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [5dc82f7...0c814d5](https://codecov.io/gh/goreleaser/goreleaser/pull/2619?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go
index 9acfec4d..67c30bc0 100644
--- a/internal/pipe/build/build.go
+++ b/internal/pipe/build/build.go
@@ -114,7 +114,7 @@ func runPipeOnBuild(ctx *context.Context, build config.Build) error {
 	return g.Wait()
 }
 
-func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.BuildHooks) error {
+func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.Hooks) error {
 	if len(hooks) == 0 {
 		return nil
 	}
diff --git a/pkg/config/config.go b/pkg/config/config.go
index caf01841..febe5b71 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -44,6 +44,14 @@ type Repo struct {
 	Name  string `yaml:"name,omitempty"`
 }
 
+// String of the repo, e.g. owner/name.
+func (r Repo) String() string {
+	if r.Owner == "" && r.Name == "" {
+		return ""
+	}
+	return r.Owner + "/" + r.Name
+}
+
 // RepoRef represents any kind of repo which may differ
 // from the one we are building from and may therefore
 // also require separate authentication
@@ -98,14 +106,6 @@ func (a HomebrewDependency) JSONSchemaType() *jsonschema.Type {
 	}
 }
 
-// String of the repo, e.g. owner/name.
-func (r Repo) String() string {
-	if r.Owner == "" && r.Name == "" {
-		return ""
-	}
-	return r.Owner + "/" + r.Name
-}
-
 // GoFish contains the gofish section.
 type GoFish struct {
 	Name                  string       `yaml:"name,omitempty"`
@@ -170,8 +170,8 @@ type CommitAuthor struct {
 	Email string `yaml:"email,omitempty"`
 }
 
-// Hooks define actions to run before and/or after something.
-type Hooks struct {
+// BuildHooks define actions to run before and/or after something.
+type BuildHooks struct { // renamed on pro
 	Pre  string `yaml:"pre,omitempty"`
 	Post string `yaml:"post,omitempty"`
 }
@@ -245,89 +245,91 @@ func (a FlagArray) JSONSchemaType() *jsonschema.Type {
 
 // Build contains the build configuration section.
 type Build struct {
-	ID              string         `yaml:"id,omitempty"`
-	Goos            []string       `yaml:"goos,omitempty"`
-	Goarch          []string       `yaml:"goarch,omitempty"`
-	Goarm           []string       `yaml:"goarm,omitempty"`
-	Gomips          []string       `yaml:"gomips,omitempty"`
-	Targets         []string       `yaml:"targets,omitempty"`
-	Ignore          []IgnoredBuild `yaml:"ignore,omitempty"`
-	Dir             string         `yaml:"dir,omitempty"`
-	Main            string         `yaml:"main,omitempty"`
-	Ldflags         StringArray    `yaml:"ldflags,omitempty"`
-	Tags            FlagArray      `yaml:"tags,omitempty"`
-	Flags           FlagArray      `yaml:"flags,omitempty"`
-	Binary          string         `yaml:"binary,omitempty"`
-	Hooks           HookConfig     `yaml:"hooks,omitempty"`
-	Env             []string       `yaml:"env,omitempty"`
-	Builder         string         `yaml:"builder,omitempty"`
-	Asmflags        StringArray    `yaml:"asmflags,omitempty"`
-	Gcflags         StringArray    `yaml:"gcflags,omitempty"`
-	ModTimestamp    string         `yaml:"mod_timestamp,omitempty"`
-	Skip            bool           `yaml:"skip,omitempty"`
-	GoBinary        string         `yaml:"gobinary,omitempty"`
-	NoUniqueDistDir bool           `yaml:"no_unique_dist_dir,omitempty"`
-	UnproxiedMain   string         `yaml:"-"` // used by gomod.proxy
-	UnproxiedDir    string         `yaml:"-"` // used by gomod.proxy
-}
-
-type HookConfig struct {
-	Pre  BuildHooks `yaml:"pre,omitempty"`
-	Post BuildHooks `yaml:"post,omitempty"`
-}
-
-type BuildHooks []BuildHook
+	ID              string          `yaml:"id,omitempty"`
+	Goos            []string        `yaml:"goos,omitempty"`
+	Goarch          []string        `yaml:"goarch,omitempty"`
+	Goarm           []string        `yaml:"goarm,omitempty"`
+	Gomips          []string        `yaml:"gomips,omitempty"`
+	Targets         []string        `yaml:"targets,omitempty"`
+	Ignore          []IgnoredBuild  `yaml:"ignore,omitempty"`
+	Dir             string          `yaml:"dir,omitempty"`
+	Main            string          `yaml:"main,omitempty"`
+	Ldflags         StringArray     `yaml:"ldflags,omitempty"`
+	Tags            FlagArray       `yaml:"tags,omitempty"`
+	Flags           FlagArray       `yaml:"flags,omitempty"`
+	Binary          string          `yaml:"binary,omitempty"`
+	Hooks           BuildHookConfig `yaml:"hooks,omitempty"`
+	Env             []string        `yaml:"env,omitempty"`
+	Builder         string          `yaml:"builder,omitempty"`
+	Asmflags        StringArray     `yaml:"asmflags,omitempty"`
+	Gcflags         StringArray     `yaml:"gcflags,omitempty"`
+	ModTimestamp    string          `yaml:"mod_timestamp,omitempty"`
+	Skip            bool            `yaml:"skip,omitempty"`
+	GoBinary        string          `yaml:"gobinary,omitempty"`
+	NoUniqueDistDir bool            `yaml:"no_unique_dist_dir,omitempty"`
+	UnproxiedMain   string          `yaml:"-"` // used by gomod.proxy
+	UnproxiedDir    string          `yaml:"-"` // used by gomod.proxy
+}
+
+type BuildHookConfig struct {
+	Pre  Hooks `yaml:",omitempty"`
+	Post Hooks `yaml:",omitempty"`
+}
+
+type Hooks []Hook
 
 // UnmarshalYAML is a custom unmarshaler that allows simplified declaration of single command.
-func (bhc *BuildHooks) UnmarshalYAML(unmarshal func(interface{}) error) error {
+func (bhc *Hooks) UnmarshalYAML(unmarshal func(interface{}) error) error {
 	var singleCmd string
 	err := unmarshal(&singleCmd)
 	if err == nil {
-		*bhc = []BuildHook{{Cmd: singleCmd}}
+		*bhc = []Hook{{Cmd: singleCmd}}
 		return nil
 	}
 
-	type t BuildHooks
+	type t Hooks
 	var hooks t
 	if err := unmarshal(&hooks); err != nil {
 		return err
 	}
-	*bhc = (BuildHooks)(hooks)
+	*bhc = (Hooks)(hooks)
 	return nil
 }
 
-func (bhc BuildHooks) JSONSchemaType() *jsonschema.Type {
-	type t BuildHooks
+func (bhc Hooks) JSONSchemaType() *jsonschema.Type {
+	type t Hooks
 	reflector := jsonschema.Reflector{
 		ExpandedStruct: true,
 	}
 	schema := reflector.Reflect(&t{})
 	return &jsonschema.Type{
-		OneOf: []*jsonschema.Type{
-			{
-				Type: "string",
+		Items: &jsonschema.Type{
+			OneOf: []*jsonschema.Type{
+				{
+					Type: "string",
+				},
+				schema.Type,
 			},
-			schema.Type,
 		},
 	}
 }
 
-type BuildHook struct {
+type Hook struct {
 	Dir string   `yaml:"dir,omitempty"`
 	Cmd string   `yaml:"cmd,omitempty"`
 	Env []string `yaml:"env,omitempty"`
 }
 
 // UnmarshalYAML is a custom unmarshaler that allows simplified declarations of commands as strings.
-func (bh *BuildHook) UnmarshalYAML(unmarshal func(interface{}) error) error {
+func (bh *Hook) UnmarshalYAML(unmarshal func(interface{}) error) error {
 	var cmd string
 	if err := unmarshal(&cmd); err != nil {
-		type t BuildHook
+		type t Hook
 		var hook t
 		if err := unmarshal(&hook); err != nil {
 			return err
 		}
-		*bh = (BuildHook)(hook)
+		*bh = (Hook)(hook)
 		return nil
 	}
 
@@ -335,8 +337,8 @@ func (bh *BuildHook) UnmarshalYAML(unmarshal func(interface{}) error) error {
 	return nil
 }
 
-func (bh BuildHook) JSONSchemaType() *jsonschema.Type {
-	type t BuildHook
+func (bh Hook) JSONSchemaType() *jsonschema.Type {
+	type t Hook
 	reflector := jsonschema.Reflector{
 		ExpandedStruct: true,
 	}
diff --git a/www/docs/static/schema-pro.json b/www/docs/static/schema-pro.json
index e073ea43..0f8b22bf 100644
--- a/www/docs/static/schema-pro.json
+++ b/www/docs/static/schema-pro.json
@@ -2268,4 +2268,4 @@
 				"type": "object"
 			}
 		}
-	}
+	}
\ No newline at end of file


Test patch:
diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go
index d5170020..aae34c51 100644
--- a/internal/builders/golang/build_test.go
+++ b/internal/builders/golang/build_test.go
@@ -565,7 +565,7 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
 			Builds: []config.Build{
 				{
 					Binary: "no-main",
-					Hooks:  config.HookConfig{},
+					Hooks:  config.BuildHookConfig{},
 					Targets: []string{
 						runtimeTarget,
 					},
@@ -680,7 +680,7 @@ func TestRunPipeWithMainFuncNotInMainGoFile(t *testing.T) {
 			{
 				Env:    []string{"GO111MODULE=off"},
 				Binary: "foo",
-				Hooks:  config.HookConfig{},
+				Hooks:  config.BuildHookConfig{},
 				Targets: []string{
 					runtimeTarget,
 				},
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index a5757790..7163c4cb 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -124,11 +124,11 @@ func TestRunFullPipe(t *testing.T) {
 				Binary:  "testing",
 				Flags:   []string{"-v"},
 				Ldflags: []string{"-X main.test=testing"},
-				Hooks: config.HookConfig{
-					Pre: []config.BuildHook{
+				Hooks: config.BuildHookConfig{
+					Pre: []config.Hook{
 						{Cmd: "touch " + pre},
 					},
-					Post: []config.BuildHook{
+					Post: []config.Hook{
 						{Cmd: "touch " + post},
 					},
 				},
@@ -161,11 +161,11 @@ func TestRunFullPipeFail(t *testing.T) {
 				Binary:  "testing",
 				Flags:   []string{"-v"},
 				Ldflags: []string{"-X main.test=testing"},
-				Hooks: config.HookConfig{
-					Pre: []config.BuildHook{
+				Hooks: config.BuildHookConfig{
+					Pre: []config.Hook{
 						{Cmd: "touch " + pre},
 					},
-					Post: []config.BuildHook{
+					Post: []config.Hook{
 						{Cmd: "touch " + post},
 					},
 				},
@@ -188,7 +188,7 @@ func TestRunPipeFailingHooks(t *testing.T) {
 			{
 				Builder: "fake",
 				Binary:  "hooks",
-				Hooks:   config.HookConfig{},
+				Hooks:   config.BuildHookConfig{},
 				Targets: []string{"linux_amd64"},
 			},
 		},
@@ -196,15 +196,15 @@ func TestRunPipeFailingHooks(t *testing.T) {
 	t.Run("pre-hook", func(t *testing.T) {
 		ctx := context.New(cfg)
 		ctx.Git.CurrentTag = "2.3.4"
-		ctx.Config.Builds[0].Hooks.Pre = []config.BuildHook{{Cmd: "exit 1"}}
-		ctx.Config.Builds[0].Hooks.Post = []config.BuildHook{{Cmd: "echo post"}}
+		ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "exit 1"}}
+		ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "echo post"}}
 		require.EqualError(t, Pipe{}.Run(ctx), `pre hook failed: "": exec: "exit": executable file not found in $PATH`)
 	})
 	t.Run("post-hook", func(t *testing.T) {
 		ctx := context.New(cfg)
 		ctx.Git.CurrentTag = "2.3.4"
-		ctx.Config.Builds[0].Hooks.Pre = []config.BuildHook{{Cmd: "echo pre"}}
-		ctx.Config.Builds[0].Hooks.Post = []config.BuildHook{{Cmd: "exit 1"}}
+		ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "echo pre"}}
+		ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "exit 1"}}
 		require.EqualError(t, Pipe{}.Run(ctx), `post hook failed: "": exec: "exit": executable file not found in $PATH`)
 	})
 }
@@ -459,11 +459,11 @@ func TestBuild_hooksKnowGoosGoarch(t *testing.T) {
 		Targets: []string{
 			"linux_amd64",
 		},
-		Hooks: config.HookConfig{
-			Pre: []config.BuildHook{
+		Hooks: config.BuildHookConfig{
+			Pre: []config.Hook{
 				{Cmd: "touch pre-hook-{{.Arch}}-{{.Os}}", Dir: tmpDir},
 			},
-			Post: config.BuildHooks{
+			Post: config.Hooks{
 				{Cmd: "touch post-hook-{{.Arch}}-{{.Os}}", Dir: tmpDir},
 			},
 		},
@@ -491,11 +491,11 @@ func TestPipeOnBuild_hooksRunPerTarget(t *testing.T) {
 			"darwin_amd64",
 			"windows_amd64",
 		},
-		Hooks: config.HookConfig{
-			Pre: []config.BuildHook{
+		Hooks: config.BuildHookConfig{
+			Pre: []config.Hook{
 				{Cmd: "touch pre-hook-{{.Target}}", Dir: tmpDir},
 			},
-			Post: config.BuildHooks{
+			Post: config.Hooks{
 				{Cmd: "touch post-hook-{{.Target}}", Dir: tmpDir},
 			},
 		},
@@ -658,8 +658,8 @@ func TestRunHookFailWithLogs(t *testing.T) {
 				Builder: "fakeFail",
 				Binary:  "testing",
 				Flags:   []string{"-v"},
-				Hooks: config.HookConfig{
-					Pre: []config.BuildHook{
+				Hooks: config.BuildHookConfig{
+					Pre: []config.Hook{
 						{Cmd: "sh -c 'echo foo; exit 1'"},
 					},
 				},
diff --git a/pkg/config/config_build_hook_test.go b/pkg/config/config_build_hook_test.go
index 7131ac7e..442747cb 100644
--- a/pkg/config/config_build_hook_test.go
+++ b/pkg/config/config_build_hook_test.go
@@ -8,18 +8,18 @@ import (
 )
 
 func TestBuildHook_justString(t *testing.T) {
-	var actual HookConfig
+	var actual BuildHookConfig
 
 	err := yaml.UnmarshalStrict([]byte(`pre: ./script.sh`), &actual)
 	require.NoError(t, err)
-	require.Equal(t, BuildHook{
+	require.Equal(t, Hook{
 		Cmd: "./script.sh",
 		Env: nil,
 	}, actual.Pre[0])
 }
 
 func TestBuildHook_stringCmds(t *testing.T) {
-	var actual HookConfig
+	var actual BuildHookConfig
 
 	err := yaml.UnmarshalStrict([]byte(`pre:
  - ./script.sh
@@ -27,7 +27,7 @@ func TestBuildHook_stringCmds(t *testing.T) {
 `), &actual)
 	require.NoError(t, err)
 
-	require.Equal(t, BuildHooks{
+	require.Equal(t, Hooks{
 		{
 			Cmd: "./script.sh",
 			Env: nil,
@@ -40,7 +40,7 @@ func TestBuildHook_stringCmds(t *testing.T) {
 }
 
 func TestBuildHook_complex(t *testing.T) {
-	var actual HookConfig
+	var actual BuildHookConfig
 
 	err := yaml.UnmarshalStrict([]byte(`pre:
  - cmd: ./script.sh
@@ -48,7 +48,7 @@ func TestBuildHook_complex(t *testing.T) {
     - TEST=value
 `), &actual)
 	require.NoError(t, err)
-	require.Equal(t, BuildHook{
+	require.Equal(t, Hook{
 		Cmd: "./script.sh",
 		Env: []string{"TEST=value"},
 	}, actual.Pre[0])