Skip to content

Commit

Permalink
Rename package name related to template asset
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 20, 2022
1 parent b49b877 commit c46a606
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
24 changes: 13 additions & 11 deletions pkg/config/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"

afctx "github.com/b4b4r07/afx/pkg/context"
"github.com/b4b4r07/afx/pkg/data"
"github.com/b4b4r07/afx/pkg/errors"
"github.com/b4b4r07/afx/pkg/logging"
"github.com/b4b4r07/afx/pkg/tmpl"
Expand Down Expand Up @@ -51,8 +51,10 @@ type GitHubOption struct {

// Release represents a GitHub release structure
type Release struct {
Name string `yaml:"name" validate:"required"`
Tag string `yaml:"tag"`
Name string `yaml:"name" validate:"required"`
Tag string `yaml:"tag"`

// TODO: (internal change): rename Artifact to Asset
Artifact Artifact `yaml:"asset"`
}

Expand Down Expand Up @@ -353,14 +355,6 @@ func (c GitHub) InstallFromRelease(ctx context.Context) error {
}

func (c GitHub) templateFilename() (string, error) {
data := afctx.New(
afctx.WithPackage(c),
afctx.WithRelease(afctx.Release{
Name: c.Release.Name,
Tag: c.Release.Tag,
}),
)

filename := c.Release.Artifact.Filename
replacements := c.Release.Artifact.Replacements

Expand All @@ -371,6 +365,14 @@ func (c GitHub) templateFilename() (string, error) {

log.Printf("[DEBUG] asset: templating filename from %q", filename)

data := data.New(
data.WithPackage(c),
data.WithRelease(data.Release{
Name: c.Release.Name,
Tag: c.Release.Tag,
}),
)

filename, err := tmpl.New(data).
Replace(replacements).
Apply(filename)
Expand Down
24 changes: 12 additions & 12 deletions pkg/context/context.go → pkg/data/data.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package context
package data

import (
"os"
"runtime"
"strings"
)

type Context struct {
type Data struct {
Env Env
Runtime Runtime
Package Package
Expand Down Expand Up @@ -35,8 +35,8 @@ type Release struct {
Tag string
}

func New(fields ...func(*Context)) *Context {
ctx := &Context{
func New(fields ...func(*Data)) *Data {
d := &Data{
Package: Package{},
Release: Release{},
Env: ToEnv(os.Environ()),
Expand All @@ -46,23 +46,23 @@ func New(fields ...func(*Context)) *Context {
},
}
for _, f := range fields {
f(ctx)
f(d)
}
return ctx
return d
}

func WithPackage(pkg PackageInterface) func(*Context) {
return func(ctx *Context) {
ctx.Package = Package{
func WithPackage(pkg PackageInterface) func(*Data) {
return func(d *Data) {
d.Package = Package{
Home: pkg.GetHome(),
Name: pkg.GetName(),
}
}
}

func WithRelease(r Release) func(*Context) {
return func(ctx *Context) {
ctx.Release = r
func WithRelease(r Release) func(*Data) {
return func(d *Data) {
d.Release = r
}
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"text/template"
"time"

"github.com/b4b4r07/afx/pkg/context"
"github.com/b4b4r07/afx/pkg/data"
)

// Template holds data that can be applied to a template string.
type Template struct {
ctx *context.Context
data *data.Data
fields Fields
}

Expand All @@ -34,19 +34,19 @@ const (
)

// New Template.
func New(ctx *context.Context) *Template {
func New(d *data.Data) *Template {
return &Template{
ctx: ctx,
data: d,
fields: Fields{
env: ctx.Env,
pkgName: ctx.Package.Name,
pkgHome: ctx.Package.Home,
dir: ctx.Package.Home,
goos: ctx.Runtime.Goos,
goarch: ctx.Runtime.Goarch,
env: d.Env,
pkgName: d.Package.Name,
pkgHome: d.Package.Home,
dir: d.Package.Home,
goos: d.Runtime.Goos,
goarch: d.Runtime.Goarch,
release: map[string]string{
releaseName: ctx.Release.Name,
releaseTag: ctx.Release.Tag,
releaseName: d.Release.Name,
releaseTag: d.Release.Tag,
},
},
}
Expand Down Expand Up @@ -81,8 +81,8 @@ func (t *Template) Apply(s string) (string, error) {

// Replace populates Fields from the artifact and replacements.
func (t *Template) Replace(replacements map[string]string) *Template {
t.fields[goos] = replace(replacements, t.ctx.Runtime.Goos)
t.fields[goarch] = replace(replacements, t.ctx.Runtime.Goarch)
t.fields[goos] = replace(replacements, t.data.Runtime.Goos)
t.fields[goarch] = replace(replacements, t.data.Runtime.Goarch)
return t
}

Expand Down

0 comments on commit c46a606

Please sign in to comment.