Skip to content

Commit

Permalink
Improve exported API, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Apr 4, 2020
1 parent 9933d7f commit 35c6866
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 46 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ Supports Caddy 2 and up.
## Library usage

```go
caddyVersion := "v2.0.0-beta.20"
plugins := []xcaddy.Dependency{
xcaddy.Dependency{
ModulePath: "github.com/caddyserver/nginx-adapter",
Version: "6c484552e630ccac384d2d9c43c9d14c4e8d2e56",
},
builder := xcaddy.Builder{
CaddyVersion: "v2.0.0-rc.1",
Plugins: []xcaddy.Dependency{
{
{
ModulePath: "github.com/caddyserver/nginx-adapter",
Version: "bdbb6db7ff9ad6ceb0dcd93f89e396f6365aa5d4",
},
}
}
}
output := "./caddy"

err := xcaddy.Build(caddyVersion, plugins, output)
err := builder.Build("./caddy")
```

Versions can be anything compatible with `go get`.
Expand Down Expand Up @@ -60,8 +62,8 @@ Where:
For example:

```bash
$ xcaddy build v2.0.0-beta.20 \
--with github.com/caddyserver/nginx-adapter@6c484552e630ccac384d2d9c43c9d14c4e8d2e56
$ xcaddy build v2.0.0-rc.1 \
--with github.com/caddyserver/nginx-adapter@bdbb6db7ff9ad6ceb0dcd93f89e396f6365aa5d4
```

### For plugin development
Expand Down
31 changes: 23 additions & 8 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ import (
"github.com/Masterminds/semver/v3"
)

// Build builds Caddy at the given version with the given plugins and
// plops a binary down at outputFile.
func Build(caddyVersion string, deps []Dependency, outputFile string) error {
if caddyVersion == "" {
return fmt.Errorf("caddy version is required")
// Builder can produce a custom Caddy build with the
// configuration it represents.
type Builder struct {
CaddyVersion string `json:"caddy_version,omitempty"`
Plugins []Dependency `json:"plugins,omitempty"`
}

// Build builds Caddy at the configured version with the
// configured plugins and plops down a binary at outputFile.
func (b Builder) Build(outputFile string) error {
if b.CaddyVersion == "" {
return fmt.Errorf("CaddyVersion must be set")
}
if outputFile == "" {
return fmt.Errorf("output file path is required")
Expand All @@ -48,7 +55,7 @@ func Build(caddyVersion string, deps []Dependency, outputFile string) error {
return err
}

env, err := newEnvironment(caddyVersion, deps)
env, err := b.newEnvironment()
if err != nil {
return err
}
Expand All @@ -74,9 +81,17 @@ func Build(caddyVersion string, deps []Dependency, outputFile string) error {

// Dependency pairs a Go module path with a version.
type Dependency struct {
// The name (path) of the Go module. If at a version > 1, it
// should contain semantic import version suffix (i.e. "/v2").
// Used with `go get`
ModulePath string
Version string
Replace string

// The version of the Go module, like used with `go get`.
Version string

// Optional path to a replacement module. Equivalent to
// a `replace` directive in go.mod.
Replace string
}

// newTempFolder creates a new folder in a temporary location.
Expand Down
22 changes: 15 additions & 7 deletions cmd/xcaddy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
}

// TODO: the caddy version needs to be settable by the user... maybe an env var?
if err := runDev("v2.0.0-beta.20", os.Args[1:]); err != nil {
if err := runDev("v2.0.0-rc.1", os.Args[1:]); err != nil {
log.Fatalf("[ERROR] %v", err)
}
}
Expand Down Expand Up @@ -89,7 +89,11 @@ func runBuild(args []string) error {
}

// perform the build
err := xcaddy.Build(caddyVersion, plugins, output)
builder := xcaddy.Builder{
CaddyVersion: caddyVersion,
Plugins: plugins,
}
err := builder.Build(output)
if err != nil {
log.Fatalf("[FATAL] %v", err)
}
Expand Down Expand Up @@ -131,12 +135,16 @@ func runDev(caddyVersion string, args []string) error {
moduleDir := strings.TrimSpace(string(out))

// build caddy with this module plugged in
err = xcaddy.Build(caddyVersion, []xcaddy.Dependency{
{
ModulePath: currentModule,
Replace: moduleDir,
builder := xcaddy.Builder{
CaddyVersion: caddyVersion,
Plugins: []xcaddy.Dependency{
{
ModulePath: currentModule,
Replace: moduleDir,
},
},
}, binOutput)
}
err = builder.Build(binOutput)
if err != nil {
return err
}
Expand Down
41 changes: 21 additions & 20 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ import (
"time"
)

func newEnvironment(caddyVersion string, plugins []Dependency) (*environment, error) {
// assume v2 if no semantic version is provided
func (b Builder) newEnvironment() (*environment, error) {
// assume Caddy v2 if no semantic version is provided
caddyModulePath := defaultCaddyModulePath
if !strings.HasPrefix(caddyVersion, "v") || !strings.Contains(caddyVersion, ".") {
if !strings.HasPrefix(b.CaddyVersion, "v") || !strings.Contains(b.CaddyVersion, ".") {
caddyModulePath += "/v2"
}
caddyModulePath, err := versionedModulePath(caddyModulePath, caddyVersion)
caddyModulePath, err := versionedModulePath(caddyModulePath, b.CaddyVersion)
if err != nil {
return nil, err
}

// clean up any SIV-incompatible module paths real quick
for i, p := range plugins {
plugins[i].ModulePath, err = versionedModulePath(p.ModulePath, p.Version)
for i, p := range b.Plugins {
b.Plugins[i].ModulePath, err = versionedModulePath(p.ModulePath, p.Version)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +50,7 @@ func newEnvironment(caddyVersion string, plugins []Dependency) (*environment, er
ctx := moduleTemplateContext{
CaddyModule: caddyModulePath,
}
for _, p := range plugins {
for _, p := range b.Plugins {
ctx.Plugins = append(ctx.Plugins, p.ModulePath)
}

Expand Down Expand Up @@ -89,8 +89,8 @@ func newEnvironment(caddyVersion string, plugins []Dependency) (*environment, er
}

env := &environment{
caddyVersion: caddyVersion,
plugins: plugins,
caddyVersion: b.CaddyVersion,
plugins: b.Plugins,
caddyModulePath: caddyModulePath,
tempFolder: tempFolder,
}
Expand All @@ -104,25 +104,26 @@ func newEnvironment(caddyVersion string, plugins []Dependency) (*environment, er
}

// specify module replacements before pinning versions
for _, p := range plugins {
if p.Replace != "" {
log.Printf("[INFO] Replace %s => %s", p.ModulePath, p.Replace)
cmd := env.newCommand("go", "mod", "edit",
"-replace", fmt.Sprintf("%s=%s", p.ModulePath, p.Replace))
err := env.runCommand(cmd, 10*time.Second)
if err != nil {
return nil, err
}
for _, p := range b.Plugins {
if p.Replace == "" {
continue
}
log.Printf("[INFO] Replace %s => %s", p.ModulePath, p.Replace)
cmd := env.newCommand("go", "mod", "edit",
"-replace", fmt.Sprintf("%s=%s", p.ModulePath, p.Replace))
err := env.runCommand(cmd, 10*time.Second)
if err != nil {
return nil, err
}
}

// pin versions by populating go.mod, first for Caddy itself and then plugins
log.Println("[INFO] Pinning versions")
err = env.execGoGet(caddyModulePath, caddyVersion)
err = env.execGoGet(caddyModulePath, b.CaddyVersion)
if err != nil {
return nil, err
}
for _, p := range plugins {
for _, p := range b.Plugins {
if p.Replace != "" {
continue
}
Expand Down

0 comments on commit 35c6866

Please sign in to comment.