mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
Merge pull request #221 from lucapette/init-command
Draft of the init command
This commit is contained in:
commit
a04ce04226
@ -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
|
||||
|
@ -5,6 +5,10 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
yaml "gopkg.in/yaml.v1"
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/pipeline"
|
||||
@ -77,3 +81,26 @@ func Release(flags Flags) error {
|
||||
log.Println("Done!")
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitProject creates an example goreleaser.yml in the current directory
|
||||
func InitProject(filename string) error {
|
||||
if _, err := os.Stat(filename); !os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s already exists", filename)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -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,56 @@ 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.Fatalf("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{}
|
||||
assert.NoError(t, yaml.Unmarshal(out, &config))
|
||||
}
|
||||
|
||||
func TestInitProjectFileExist(t *testing.T) {
|
||||
var filename = "test_goreleaser.yml"
|
||||
|
||||
createFile(t, filename, "")
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
assert.Error(t, InitProject(filename))
|
||||
}
|
||||
|
16
main.go
16
main.go
@ -46,6 +46,22 @@ func main() {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "init",
|
||||
Aliases: []string{"i"},
|
||||
Usage: "generate goreleaser.yml",
|
||||
Action: func(c *cli.Context) error {
|
||||
var filename = "goreleaser.yml"
|
||||
if err := goreleaserlib.InitProject(filename); err != nil {
|
||||
return cli.NewExitError(err.Error(), 1)
|
||||
}
|
||||
|
||||
log.Printf("%s created. Please edit accordingly to your needs.", filename)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user