Skip to content

Commit

Permalink
feat: load and use output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Oct 24, 2024
1 parent 34381a5 commit 24fd031
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
13 changes: 12 additions & 1 deletion internal/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ import (
)

func Run(ctx context.Context) {
conf := Config{
OutputDir: global.Config.Output,
}
switch global.Config.OutputFormat {
case "":
case "post":
conf.OutputFormatter = PostOutputFormatter{}
default:
log.Warnln("Unsupported output format:", global.Config.OutputFormat)
}

if err := DownloadGoMod(ctx); err != nil {
log.Fatalln("download go mod failed:", err)
}
builder, err := NewBuilder(global.Config.Output)
builder, err := NewBuilder(conf)
if err != nil {
log.Fatalln(err)
}
Expand Down
34 changes: 22 additions & 12 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import (
"sync"
)

func NewBuilder(outputDir string) (*Builder, error) {
type Config struct {
OutputDir string
OutputFormatter OutputFormatter
}

func NewBuilder(conf Config) (*Builder, error) {
outputName := LoadBinaryName()

goBuilder := goCMD.NewBuilder(global.Config.Target).TrimPath()
Expand All @@ -33,10 +38,14 @@ func NewBuilder(outputDir string) (*Builder, error) {
}

builder := &Builder{
OutputName: outputName,
OutputDir: outputDir,
Platforms: platforms,
GoCMD: goBuilder,
OutputName: outputName,
OutputFormatter: conf.OutputFormatter,
OutputDir: conf.OutputDir,
Platforms: platforms,
GoCMD: goBuilder,
}
if builder.OutputFormatter == nil {
builder.OutputFormatter = DefaultOutputFormatter{}
}

if global.Config.Compress == "" {
Expand Down Expand Up @@ -76,19 +85,20 @@ func NewBuilder(outputDir string) (*Builder, error) {
builder.Cgo = "CGO_ENABLED=0"
}

if err := PrepareDirs(outputDir); err != nil {
if err := PrepareDirs(conf.OutputDir); err != nil {
return nil, err
}

return builder, nil
}

type Builder struct {
OutputName string
OutputDir string
Cgo string
Platforms map[string][]string
GoCMD goCMD.BuildCommand
OutputName string
OutputFormatter OutputFormatter
OutputDir string
Cgo string
Platforms map[string][]string
GoCMD goCMD.BuildCommand

WaitGroup *sync.WaitGroup
TaskChan chan *Task
Expand Down Expand Up @@ -126,7 +136,7 @@ func (b *Builder) NewTask(ctx TaskContext) *Task {
if ctx.GOOS == "windows" {
outputName += ".exe"
}
buildName := BuildName(outputName, global.Config.Divider, append([]string{ctx.GOOS, ctx.GOARCH}, ctx.NameSuffix...)...)
buildName := b.OutputFormatter.Format(outputName, global.Config.Divider, append([]string{ctx.GOOS, ctx.GOARCH}, ctx.NameSuffix...)...)
outputPath := path.Join(b.OutputDir, buildName)

cmd := b.GoCMD.OutputName(outputPath).ExecContext(ctx)
Expand Down
10 changes: 5 additions & 5 deletions internal/builder/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
)

type OutputFormatter interface {
Format(name, divider string, args ...string) (string, error)
Format(name, divider string, args ...string) string
}

type DefaultOutputFormatter struct{}

func (DefaultOutputFormatter) Format(filename, divider string, args ...string) (string, error) {
func (DefaultOutputFormatter) Format(filename, divider string, args ...string) string {
ext := path.Ext(filename)
name := strings.TrimSuffix(filename, ext)
for _, s := range args {
name += divider + s
}
name += ext
return name, nil
return name
}

type PostOutputFormatter struct{}

func (PostOutputFormatter) Format(filename, divider string, args ...string) (string, error) {
func (PostOutputFormatter) Format(filename, divider string, args ...string) string {
var name string
for _, s := range args {
name += s + divider
}
return name + filename, nil
return name + filename
}
11 changes: 0 additions & 11 deletions internal/builder/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Mmx233/GoReleaseCli/internal/global"
"github.com/Mmx233/GoReleaseCli/pkg/goCMD"
"os"
"path"
"strings"
)

Expand All @@ -25,16 +24,6 @@ func LoadBinaryName() string {
return temp[len(temp)-1]
}

func BuildName(binaryName, divider string, suffix ...string) string {
ext := path.Ext(binaryName)
name := strings.TrimSuffix(binaryName, ext)
for _, s := range suffix {
name += divider + s
}
name += ext
return name
}

// MatchTargetPlatforms can only be executed once because permanent modify of global arch map
func MatchTargetPlatforms() (map[string][]string, error) {
var targetPlatforms map[string][]string
Expand Down
1 change: 1 addition & 0 deletions internal/global/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewCommands(version string) *kingpin.Application {
app.Flag("extra-arches-show-default", "Show default extra arch name.").BoolVar(&Config.ExtraArchesShowDefault)

app.Flag("output", "Output dir path.").Short('d').Default("build").StringVar(&Config.Output)
app.Flag("output-format", "Output file name format").Short('f').EnumVar(&Config.OutputFormat, "", "post")
app.Flag("name", "Output binary file name.").Short('o').StringVar(&Config.Name)
app.Flag("divider", "Divider in name.").Default("_").StringVar(&Config.Divider)
app.Arg("target", "Target package.").Required().StringVar(&Config.Target)
Expand Down
9 changes: 5 additions & 4 deletions internal/global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ type _Config struct {
OS string `env:"OS"`
Arch string `env:"ARCH"`
// If Platforms is set, OS and Arch will not be used.
Platforms string `env:"PLATFORMS"`
Output string `env:"OUTPUT,notEmpty" envDefault:"build"`
Divider string `env:"DIVIDER" envDefault:"_"`
Name string `env:"NAME"`
Platforms string `env:"PLATFORMS"`
Output string `env:"OUTPUT,notEmpty" envDefault:"build"`
OutputFormat string `env:"OUTPUT_FORMAT"`
Divider string `env:"DIVIDER" envDefault:"_"`
Name string `env:"NAME"`

ModDownloadArgs string `env:"MOD-DOWNLOAD-ARGS"`

Expand Down

0 comments on commit 24fd031

Please sign in to comment.