Skip to content

Commit

Permalink
[#35]: feat: env variables support
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Jul 6, 2022
2 parents e1d471a + 8f1a982 commit abda4ea
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
Automated build system for the RR with plugins.

<p align="center">
<img src="https://user-images.githubusercontent.com/796136/50286124-6f7f3780-046f-11e9-9f45-e8fedd4f786d.png" height="75px" alt="RoadRunner">
</p>
<p align="center">
<a href="https://packagist.org/packages/spiral/roadrunner"><img src="https://poser.pugx.org/spiral/roadrunner/version"></a>
<a href="https://pkg.go.dev/github.com/roadrunner-server/velox/v2?tab=doc"><img src="https://godoc.org/github.com/roadrunner-server/velox/v2?status.svg"></a>
<a href="https://twitter.com/spiralphp"><img src="https://img.shields.io/twitter/follow/spiralphp?style=social"></a>
<a href="https://github.com/roadrunner-server/velox/actions"><img src="https://github.com/roadrunner-server/velox/workflows/Linters/badge.svg" alt=""></a>
<a href="https://github.com/roadrunner-server/velox/actions"><img src="https://github.com/roadrunner-server/velox/workflows/Linux/badge.svg" alt=""></a>
<a href="https://discord.gg/TFeEmCs"><img src="https://img.shields.io/badge/discord-chat-magenta.svg"></a>
</p>

# Docs: [link](https://roadrunner.dev/docs/app-server-build/2.x/en)

Automated build system for the RR with plugins.
23 changes: 18 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package velox

import (
"fmt"
"os"

"github.com/pkg/errors"
)
Expand All @@ -13,6 +14,7 @@ const (
)

type Config struct {
// build args
Velox map[string][]string `mapstructure:"velox"`

// Version
Expand Down Expand Up @@ -43,14 +45,21 @@ type CodeHosting struct {
}

type PluginConfig struct {
Ref string `mapstructure:"ref"`
Owner string `mapstructure:"owner"`
Repo string `mapstructure:"repository"`
Replace string `mapstructure:"replace"`
BuildFlags []string `mapstructure:"build-flags"`
Ref string `mapstructure:"ref"`
Owner string `mapstructure:"owner"`
Repo string `mapstructure:"repository"`
Replace string `mapstructure:"replace"`
}

func (c *Config) Validate() error { //nolint:gocognit,gocyclo
// build_args
for k := range c.Velox {
for j := 0; j < len(c.Velox[k]); j++ {
s := os.ExpandEnv(c.Velox[k][j])
c.Velox[k][j] = s
}
}

if _, ok := c.Roadrunner[ref]; !ok {
c.Roadrunner[ref] = defaultBranch
}
Expand All @@ -77,6 +86,8 @@ func (c *Config) Validate() error { //nolint:gocognit,gocyclo
if c.GitHub.Token == nil || c.GitHub.Token.Token == "" {
return errors.New("github.token should not be empty, create a token with any permissions: https://github.com/settings/tokens")
}

c.GitHub.Token.Token = os.ExpandEnv(c.GitHub.Token.Token)
}

if c.GitLab != nil {
Expand All @@ -101,6 +112,8 @@ func (c *Config) Validate() error { //nolint:gocognit,gocyclo
if c.GitLab.Token == nil || c.GitLab.Token.Token == "" {
return errors.New("gitlab.token should not be empty, create a token with at least [api, read_api] permissions: https://gitlab.com/-/profile/personal_access_tokens")
}

c.GitLab.Token.Token = os.ExpandEnv(c.GitLab.Token.Token)
}

if len(c.Log) == 0 {
Expand Down
53 changes: 53 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package velox

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExpandEnvs(t *testing.T) {
tn := time.Now().Format(time.Kitchen)
token := "foobarbaz"

require.NoError(t, os.Setenv("TIME", tn))
require.NoError(t, os.Setenv("VERSION", "v2.10.5"))
require.NoError(t, os.Setenv("TOKEN", token))
c := &Config{
Velox: map[string][]string{"build_args": {"github.com/roadrunner-server/roadrunner/v2/internal/meta.buildTime=${TIME}", "github.com/roadrunner-server/roadrunner/v2/internal/meta.version=${VERSION}"}},
Roadrunner: map[string]string{"": ""},
GitHub: &CodeHosting{
BaseURL: nil,
Token: &Token{Token: "${TOKEN}"},
Plugins: map[string]*PluginConfig{"foo": {
Ref: "master",
Owner: "roadrunner-server",
Repo: "logger",
Replace: "",
},
},
},
GitLab: &CodeHosting{
BaseURL: nil,
Token: &Token{Token: "${TOKEN}"},
Plugins: map[string]*PluginConfig{"foo": {
Ref: "master",
Owner: "roadrunner-server",
Repo: "logger",
Replace: "",
},
},
},
Log: nil,
}

require.NoError(t, c.Validate())

assert.Equal(t, "github.com/roadrunner-server/roadrunner/v2/internal/meta.buildTime="+tn, c.Velox["build_args"][0])
assert.Equal(t, "github.com/roadrunner-server/roadrunner/v2/internal/meta.version=v2.10.5", c.Velox["build_args"][1])
assert.Equal(t, token, c.GitHub.Token.Token)
assert.Equal(t, token, c.GitLab.Token.Token)
}
8 changes: 4 additions & 4 deletions velox.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[velox]
build_args = ['-trimpath', '-ldflags', '-s -X github.com/roadrunner-server/roadrunner/v2/internal/meta.version=v2.10.1 -X github.com/roadrunner-server/roadrunner/v2/internal/meta.buildTime=10:00:00']
build_args = ['-trimpath', '-ldflags', '-s -X github.com/roadrunner-server/roadrunner/v2/internal/meta.version=${VERSION} -X github.com/roadrunner-server/roadrunner/v2/internal/meta.buildTime=${TIME}']

[roadrunner]
ref = "v2.10.1"
ref = "v2.10.5"

[github]
[github.token]
token = "token"
token = "${RT_TOKEN}"

[github.plugins]
# ref -> master, commit or tag
Expand Down Expand Up @@ -43,7 +43,7 @@ ref = "v2.10.1"
[gitlab]
[gitlab.token]
# api, read-api, read-repo
token = "token"
token = "${GL_TOKEN}"

[gitlab.endpoint]
endpoint = "https://gitlab.com"
Expand Down

0 comments on commit abda4ea

Please sign in to comment.