Add --auth-token global flag to set the auth flag to be used with potainer

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-07-23 20:47:14 -04:00
parent 35a0d089cb
commit 143215b39f
3 changed files with 11 additions and 4 deletions

View File

@ -1,6 +1,7 @@
FROM alpine FROM alpine
ENV PSU_AUTHENTICATION_PASSWORD="" \ ENV PSU_AUTHENTICATION_PASSWORD="" \
PSU_AUTH_TOKEN="" \
PSU_AUTHENTICATION_USER="" \ PSU_AUTHENTICATION_USER="" \
PSU_CONFIG="" \ PSU_CONFIG="" \
PSU_CONFIG_LIST_KEYS="" \ PSU_CONFIG_LIST_KEYS="" \

View File

@ -44,6 +44,7 @@ func init() {
rootCmd.PersistentFlags().StringP("url", "l", "", "Portainer url") rootCmd.PersistentFlags().StringP("url", "l", "", "Portainer url")
rootCmd.PersistentFlags().StringP("user", "u", "", "Portainer user") rootCmd.PersistentFlags().StringP("user", "u", "", "Portainer user")
rootCmd.PersistentFlags().StringP("password", "p", "", "Portainer password") rootCmd.PersistentFlags().StringP("password", "p", "", "Portainer password")
rootCmd.PersistentFlags().StringP("auth-token", "A", "", "Portainer auth token")
rootCmd.PersistentFlags().DurationP("timeout", "t", 0, "waiting time before aborting (like 100ms, 30s, 1h20m)") rootCmd.PersistentFlags().DurationP("timeout", "t", 0, "waiting time before aborting (like 100ms, 30s, 1h20m)")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")) viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
@ -53,6 +54,7 @@ func init() {
viper.BindPFlag("timeout", rootCmd.PersistentFlags().Lookup("timeout")) viper.BindPFlag("timeout", rootCmd.PersistentFlags().Lookup("timeout"))
viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user")) viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user"))
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password")) viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
viper.BindPFlag("auth-token", rootCmd.PersistentFlags().Lookup("auth-token"))
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.

View File

@ -13,10 +13,14 @@ var cachedAuthenticationToken string
func GetAuthenticationToken() (string, error) { func GetAuthenticationToken() (string, error) {
if cachedAuthenticationToken == "" { if cachedAuthenticationToken == "" {
var authenticationTokenRetrievalErr error if viper.GetString("auth-token") != "" {
cachedAuthenticationToken, authenticationTokenRetrievalErr = GetNewAuthenticationToken() cachedAuthenticationToken = viper.GetString("auth-token")
if authenticationTokenRetrievalErr != nil { } else {
return "", authenticationTokenRetrievalErr var authenticationTokenRetrievalErr error
cachedAuthenticationToken, authenticationTokenRetrievalErr = GetNewAuthenticationToken()
if authenticationTokenRetrievalErr != nil {
return "", authenticationTokenRetrievalErr
}
} }
} }
return cachedAuthenticationToken, nil return cachedAuthenticationToken, nil