-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Env Var support for access-token-file flag
If the env variable INLETS_ACCESS_TOKEN or INLETS_SECRET_KEY are set then these values will be used AFTER the values for the flags. As in, if you don't specify a flag it will see if these exist, if you set the flag then it won't look in this env variable Signed-off-by: Alistair Hey <[email protected]>
- Loading branch information
1 parent
37742b3
commit a03f5e2
Showing
8 changed files
with
276 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package env | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/pflag" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func GetRequiredFileOrString(flags *pflag.FlagSet, file, value, envVarName string) (string, error) { | ||
return getFileOrString(flags, file, value, envVarName, true) | ||
} | ||
|
||
func getFileOrString(flags *pflag.FlagSet, file, value, envVarName string, required bool) (string, error) { | ||
var val string | ||
|
||
authFile, _ := flags.GetString(file) | ||
envToken := os.Getenv(envVarName) | ||
flagVal, _ := flags.GetString(value) | ||
|
||
if len(authFile) > 0 { | ||
// Fallback to the File Flag, then the Env Var | ||
res, err := ioutil.ReadFile(authFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
val = strings.TrimSpace(string(res)) | ||
} else { | ||
val = flagVal | ||
} | ||
|
||
// Finally if val isn't set we can look in the env variable | ||
if len(val) == 0 && len(envToken) > 0 { | ||
val = strings.TrimSpace(string(envToken)) | ||
} | ||
|
||
if required && len(val) == 0 { | ||
return "", fmt.Errorf("give a value for --%s, --%s or set the environment variable %q", file, value, envVarName) | ||
} | ||
|
||
return val, nil | ||
} |
Oops, something went wrong.