Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Envvar and allow user to upload env. #132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

Expand All @@ -14,6 +14,21 @@ var (
build = "0"
)

func loadEnvVar() {
fileName := os.Getenv("ENV_FILE")
if fileName == "" {
fileName = os.Getenv("PLUGIN_ENV-FILE")
}

if fileName != "" {
err := godotenv.Overload(fileName)
log.Info(fmt.Sprintf("Successfully loaded/overloaded environment variables from %s", fileName))
if err != nil {
log.Error(fmt.Sprintf("Error reading env file %s - %v", fileName, err))
}
}
}

func main() {
app := cli.NewApp()
app.Name = "s3 plugin"
Expand All @@ -29,28 +44,28 @@ func main() {
cli.StringFlag{
Name: "access-key",
Usage: "aws access key",
EnvVar: "PLUGIN_ACCESS_KEY,AWS_ACCESS_KEY_ID",
EnvVar: "PLUGIN_ACCESS-KEY,AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: "secret-key",
Usage: "aws secret key",
EnvVar: "PLUGIN_SECRET_KEY,AWS_SECRET_ACCESS_KEY",
EnvVar: "PLUGIN_SECRET-KEY,AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: "assume-role",
Usage: "aws iam role to assume",
EnvVar: "PLUGIN_ASSUME_ROLE,ASSUME_ROLE",
EnvVar: "PLUGIN_ASSUME-ROLE,ASSUME_ROLE",
},
cli.StringFlag{
Name: "assume-role-session-name",
Usage: "aws iam role session name to assume",
Value: "drone-s3",
EnvVar: "PLUGIN_ASSUME_ROLE_SESSION_NAME,ASSUME_ROLE_SESSION_NAME",
EnvVar: "PLUGIN_ASSUME-ROLE-SESSION-NAME,ASSUME_ROLE_SESSION_NAME",
},
cli.StringFlag{
Name: "user-role-arn",
Usage: "AWS user role",
EnvVar: "PLUGIN_USER_ROLE_ARN,AWS_USER_ROLE_ARN",
EnvVar: "PLUGIN_USER-ROLE-ARN,AWS_USER_ROLE_ARN",
},
cli.StringFlag{
Name: "bucket",
Expand Down Expand Up @@ -82,7 +97,7 @@ func main() {
cli.StringFlag{
Name: "strip-prefix",
Usage: "strip the prefix from the target",
EnvVar: "PLUGIN_STRIP_PREFIX",
EnvVar: "PLUGIN_STRIP-PREFIX",
},
cli.StringSliceFlag{
Name: "exclude",
Expand All @@ -97,57 +112,56 @@ func main() {
cli.BoolFlag{
Name: "dry-run",
Usage: "dry run for debug purposes",
EnvVar: "PLUGIN_DRY_RUN",
EnvVar: "PLUGIN_DRY-RUN",
},
cli.BoolFlag{
Name: "path-style",
Usage: "use path style for bucket paths",
EnvVar: "PLUGIN_PATH_STYLE",
EnvVar: "PLUGIN_PATH-STYLE",
},
cli.GenericFlag{
Name: "content-type",
Usage: "set content type header for uploaded objects",
EnvVar: "PLUGIN_CONTENT_TYPE",
EnvVar: "PLUGIN_CONTENT-TYPE",
Value: &StringMapFlag{},
},
cli.GenericFlag{
Name: "content-encoding",
Usage: "set content encoding header for uploaded objects",
EnvVar: "PLUGIN_CONTENT_ENCODING",
EnvVar: "PLUGIN_CONTENT-ENCODING",
Value: &StringMapFlag{},
},
cli.GenericFlag{
Name: "cache-control",
Usage: "set cache-control header for uploaded objects",
EnvVar: "PLUGIN_CACHE_CONTROL",
EnvVar: "PLUGIN_CACHE-CONTROL",
Value: &StringMapFlag{},
},
cli.StringFlag{
Name: "storage-class",
Usage: "set storage class to choose the best backend",
EnvVar: "PLUGIN_STORAGE_CLASS",
EnvVar: "PLUGIN_STORAGE-CLASS",
},
cli.StringFlag{
Name: "env-file",
Usage: "source env file",
Name: "env-file",
Usage: "source env file",
EnvVar: "PLUGIN_ENV-FILE,ENV_FILE",
},
cli.StringFlag{
Name: "external-id",
Usage: "external ID to use when assuming role",
EnvVar: "PLUGIN_EXTERNAL_ID",
EnvVar: "PLUGIN_EXTERNAL-ID",
},
}

loadEnvVar()

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
log.Fatal(err)
}
}

func run(c *cli.Context) error {
if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
}

plugin := Plugin{
Endpoint: c.String("endpoint"),
Key: c.String("access-key"),
Expand Down