1
1
mirror of https://github.com/joshdk/drone-skip-pipeline synced 2024-11-22 13:41:57 +01:00

feat: load plugin config from env (#6)

This commit is contained in:
Josh Komoroske 2021-11-14 11:41:59 -08:00 committed by GitHub
parent ae754e6ce8
commit df43032bbe
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 3 deletions

5
go.mod

@ -2,4 +2,7 @@ module github.com/joshdk/drone-skip-pipeline
go 1.17
require jdk.sh/meta v0.1.1-0.20211021015548-cff3b930ebd9
require (
github.com/kelseyhightower/envconfig v1.4.0
jdk.sh/meta v0.1.1-0.20211021015548-cff3b930ebd9
)

4
go.sum

@ -1,4 +1,4 @@
jdk.sh/meta v0.1.0 h1:BTDH9cL/el15OSZWRcnbdOzynyr7hlRlYrTSJCrcKbo=
jdk.sh/meta v0.1.0/go.mod h1:c2+wWgMZejSQ2nXsbxNVKLLQgTLY4yrX7TH6NIaQle0=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
jdk.sh/meta v0.1.1-0.20211021015548-cff3b930ebd9 h1:f9CEE/IWIjsG3jBxNc/rfJ17031Oe7ghTWkPg6JJ6Bo=
jdk.sh/meta v0.1.1-0.20211021015548-cff3b930ebd9/go.mod h1:c2+wWgMZejSQ2nXsbxNVKLLQgTLY4yrX7TH6NIaQle0=

40
main.go

@ -6,9 +6,11 @@
package main
import (
"errors"
"log"
"os"
"github.com/kelseyhightower/envconfig"
"jdk.sh/meta"
)
@ -21,5 +23,43 @@ func main() {
func mainCmd() error {
log.Printf("joshdk/drone-skip-pipeline %s (%s)\n", meta.Version(), meta.ShortSHA())
_, err := loadConfig()
if err != nil {
return err
}
return nil
}
type config struct {
Event string `envconfig:"DRONE_BUILD_EVENT"`
GithubToken string `envconfig:"GITHUB_TOKEN"`
PullRequest int `envconfig:"DRONE_PULL_REQUEST"`
RepoName string `envconfig:"DRONE_REPO_NAME"`
RepoOwner string `envconfig:"DRONE_REPO_OWNER"`
Rules []string `envconfig:"PLUGIN_RULES"`
}
func loadConfig() (*config, error) {
// Load plugin configuration from current working environment.
var cfg config
err := envconfig.Process("", &cfg)
if err != nil {
return nil, err
}
// Sanity check that plugin is sufficiently configured.
switch {
case cfg.Event == "":
return nil, errors.New("missing DRONE_BUILD_EVENT")
case cfg.PullRequest == 0:
return nil, errors.New("missing DRONE_PULL_REQUEST")
case cfg.RepoName == "":
return nil, errors.New("missing DRONE_REPO_NAME")
case cfg.RepoOwner == "":
return nil, errors.New("missing DRONE_REPO_OWNER")
}
return &cfg, nil
}