diff --git a/config/config.go b/config/config.go index ad4736ed3..480706875 100644 --- a/config/config.go +++ b/config/config.go @@ -10,8 +10,8 @@ import ( // Repo represents any kind of repo (github, gitlab, etc) type Repo struct { - Owner string - Name string + Owner string `yaml:"owner,omitempty"` + Name string `yaml:"name,omitempty"` } // String of the repo, e.g. owner/name @@ -21,48 +21,48 @@ func (r Repo) String() string { // Homebrew contains the brew section type Homebrew struct { - GitHub Repo - Folder string - Caveats string - Plist string - Install string - Dependencies []string - Conflicts []string - Description string - Homepage string + GitHub Repo `yaml:"github,omitempty"` + Folder string `yaml:"folder,omitempty"` + Caveats string `yaml:"caveats,omitempty"` + Plist string `yaml:"plist,omitempty"` + Install string `yaml:"install,omitempty"` + Dependencies []string `yaml:"dependencies,omitempty"` + Conflicts []string `yaml:"conflicts,omitempty"` + Description string `yaml:"description,omitempty"` + Homepage string `yaml:"homepage,omitempty"` } // Hooks define actions to run before and/or after something type Hooks struct { - Pre string - Post string + Pre string `yaml:"pre,omitempty"` + Post string `yaml:"post,omitempty"` } // Build contains the build configuration section type Build struct { - Goos []string - Goarch []string - Goarm []string - Main string - Ldflags string - Flags string - Binary string - Hooks Hooks + Goos []string `yaml:"goos,omitempty"` + Goarch []string `yaml:"goarch,omitempty"` + Goarm []string `yaml:"goarm,omitempty"` + Main string `yaml:"main,omitempty"` + Ldflags string `yaml:"ldflags,omitempty"` + Flags string `yaml:"flags,omitempty"` + Binary string `yaml:"binary,omitempty"` + Hooks Hooks `yaml:"hooks,omitempty"` } // FormatOverride is used to specify a custom format for a specific GOOS. type FormatOverride struct { - Goos string - Format string + Goos string `yaml:"goos,omitempty"` + Format string `yaml:"format,omitempty"` } // Archive config used for the archive type Archive struct { - Format string - FormatOverrides []FormatOverride `yaml:"format_overrides"` - NameTemplate string `yaml:"name_template"` - Replacements map[string]string - Files []string + Format string `yaml:"format,omitempty"` + FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"` + NameTemplate string `yaml:"name_template,omitempty"` + Replacements map[string]string `yaml:"replacemnts,omitempty"` + Files []string `yaml:"files,omitempty"` } // Release config used for the GitHub release @@ -73,14 +73,14 @@ type Release struct { // FPM config type FPM struct { - Formats []string - Dependencies []string - Conflicts []string - Vendor string - Homepage string - Maintainer string - Description string - License string + Formats []string `yaml:"formats,omitempty"` + Dependencies []string `yaml:"dependencies,omitempty"` + Conflicts []string `yaml:"conflicts,omitempty"` + Vendor string `yaml:"vendor,omitempty"` + Homepage string `yaml:"homepage,omitempty"` + Maintainer string `yaml:"maintainer,omitempty"` + Description string `yaml:"description,omitempty"` + License string `yaml:"license,omitempty"` } // Project includes all project configuration @@ -104,16 +104,3 @@ func Load(file string) (config Project, err error) { err = yaml.Unmarshal(data, &config) return } - -// DefaultConfig returns a default configuration for the init command -func DefaultConfig() string { - return ` -build: - binary: drum-roll - goos: - - windows - - darwin - - linux - goarch: - - amd64` -} diff --git a/config/config_test.go b/config/config_test.go deleted file mode 100644 index 092319b6b..000000000 --- a/config/config_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package config - -import ( - "testing" - - yaml "gopkg.in/yaml.v1" -) - -func TestDefaultConfig(t *testing.T) { - cfg := DefaultConfig() - config := Project{} - - if err := yaml.Unmarshal([]byte(cfg), &config); err != nil { - t.Errorf("Not a valid config. %s", err.Error()) - } -} diff --git a/goreleaserlib/goreleaser.go b/goreleaserlib/goreleaser.go index daee1b006..ea725936e 100644 --- a/goreleaserlib/goreleaser.go +++ b/goreleaserlib/goreleaser.go @@ -5,6 +5,8 @@ import ( "log" "os" + yaml "gopkg.in/yaml.v1" + "fmt" "github.com/goreleaser/goreleaser/config" @@ -81,8 +83,7 @@ func Release(flags Flags) error { } // InitProject creates an example goreleaser.yml in the current directory -func InitProject() error { - filename := "goreleaser.yml" +func InitProject(filename string) error { if _, err := os.Stat(filename); !os.IsNotExist(err) { if err != nil { return err @@ -91,5 +92,15 @@ func InitProject() error { return fmt.Errorf("%s already exists", filename) } - return ioutil.WriteFile(filename, []byte(config.DefaultConfig()), 0644) + var ctx = context.New(config.Project{}) + var pipe = defaults.Pipe{} + if err := pipe.Run(ctx); err != nil { + return err + } + out, err := yaml.Marshal(ctx.Config) + if err != nil { + return err + } + + return ioutil.WriteFile(filename, out, 0644) } diff --git a/goreleaserlib/goreleaser_test.go b/goreleaserlib/goreleaser_test.go index 34286fa16..e57b3b785 100644 --- a/goreleaserlib/goreleaser_test.go +++ b/goreleaserlib/goreleaser_test.go @@ -8,6 +8,9 @@ import ( "path/filepath" "testing" + yaml "gopkg.in/yaml.v1" + + "github.com/goreleaser/goreleaser/config" "github.com/stretchr/testify/assert" ) @@ -152,3 +155,38 @@ release: ` createFile(t, "goreleaser.yml", yaml) } + +func TestInitProject(t *testing.T) { + var filename = "test_goreleaser.yml" + + defer func() { + if _, err := os.Stat(filename); !os.IsNotExist(err) { + if err != nil { + t.Fatal(err.Error()) + } + + if err := os.Remove(filename); err != nil { + t.Fatal(err.Error()) + } + } + }() + + if err := InitProject(filename); err != nil { + t.Errorf("exepcted InitProject() to run, but got %v", err.Error()) + } + + file, err := os.Open(filename) + if err != nil { + t.Fatal(err.Error()) + } + + out, err := ioutil.ReadAll(file) + if err != nil { + t.Fatal(err.Error()) + } + + config := config.Project{} + if err := yaml.Unmarshal(out, &config); err != nil { + t.Errorf("Not a valid config. %s", err.Error()) + } +} diff --git a/main.go b/main.go index 95889d795..837074e05 100644 --- a/main.go +++ b/main.go @@ -52,12 +52,13 @@ func main() { Aliases: []string{"i"}, Usage: "generate goreleaser.yml", Action: func(c *cli.Context) error { - err := goreleaserlib.InitProject() + var filename = "goreleaser.yml" + err := goreleaserlib.InitProject(filename) if err != nil { return err } - log.Printf("goreleaser.yml created. Please edit accordingly to your needs.") + log.Printf("%s created. Please edit accordingly to your needs.", filename) return nil }, },