diff --git a/README.md b/README.md index fe996ca..f00d0eb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ +Automated build system for the RR with plugins. + +
+ +
+ + # Docs: [link](https://roadrunner.dev/docs/app-server-build/2.x/en) -Automated build system for the RR with plugins. diff --git a/config.go b/config.go index 1c10eb2..822a9fd 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package velox import ( "fmt" + "os" "github.com/pkg/errors" ) @@ -13,6 +14,7 @@ const ( ) type Config struct { + // build args Velox map[string][]string `mapstructure:"velox"` // Version @@ -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 } @@ -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 { @@ -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 { diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..932b7b8 --- /dev/null +++ b/config_test.go @@ -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) +} diff --git a/velox.toml b/velox.toml index 0016689..04b39e7 100644 --- a/velox.toml +++ b/velox.toml @@ -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 @@ -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"