From 128bc51eafe8f88d670e5b92b28cf866d19f5684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Mon, 31 Oct 2022 20:24:26 +0100 Subject: [PATCH] Added `OPSGENIE_API_KEY` from env --- README.md | 12 ++++++++++-- main.go | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6d59488..79b54f0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ go get ``` -apiKey string - # ApiKey for use in that script + # ApiKey for use in that script. + # You can use the export OPSGENIE_API_KEY="XXXXXXXXXXXXXXX" -delete # Delete schedule -scheduleEnabledFlag @@ -38,7 +39,14 @@ go get ### How to use it in the console? ```bash -go run main.go --apiKey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --scheduleTeam TestTeam --scheduleName "YEAR_2023" --scheduleYear 2023 +export OPSGENIE_API_KEY="XXXXXXXXXXXXXXX" +go run main.go --scheduleTeam TestTeam --scheduleName "YEAR_2023" --scheduleYear 2023 +``` + +Alternative use: + +```bash +go run main.go --apiKey XXXXXXXXXXXXXXX --scheduleTeam TestTeam --scheduleName "YEAR_2023" --scheduleYear 2023 ``` ### Output console diff --git a/main.go b/main.go index b9ecaee..0a04d8f 100644 --- a/main.go +++ b/main.go @@ -67,8 +67,12 @@ var defaultSchedule = [...]og.Restriction{ } func createApi(apiKey string) *schedule.Client { - if apiKey == "" { - fmt.Printf("Empty apiKey... Please use -apiKey \n") + apiKeyEnv := os.Getenv("OPSGENIE_API_KEY") + + if apiKeyEnv != "" { + apiKey = apiKeyEnv + } else if apiKey == "" { + fmt.Printf("Empty apiKey...\nPlease use -apiKey or export OPSGENIE_API_KEY=\"XXXXXXXXXXXXXXX\" \n") os.Exit(1) } @@ -183,7 +187,7 @@ func getListRotation(scheduleClient schedule.Client, scheduleID string) *schedul } func main() { - apiKey := flag.String("apiKey", "", "# ApiKey for use in that script") + apiKey := flag.String("apiKey", "", "# ApiKey for use in that script.\n# You can use the export OPSGENIE_API_KEY=\"XXXXXXXXXXXXXXX\"") scheduleName := flag.String("scheduleName", staticScheduleName, "# Name of schedule") scheduleID := flag.String("scheduleID", staticScheduleID, "# ID of schedule") scheduleTimezone := flag.String("scheduleTimezone", staticScheduleTimezone, "# Timezone of the schedule")