From df43032bbeb01d8a09910bc21ccd21f647d97c5f Mon Sep 17 00:00:00 2001 From: Josh Komoroske Date: Sun, 14 Nov 2021 11:41:59 -0800 Subject: [PATCH] feat: load plugin config from env (#6) --- go.mod | 5 ++++- go.sum | 4 ++-- main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a060ec2..1b67462 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index 11f8c12..50cc2ca 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index ba3a1ad..392f4b0 100644 --- a/main.go +++ b/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 +}