From 6200b6c66791bd9f023f899de1877ba09f42f080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Tue, 23 Jul 2019 22:12:45 -0400 Subject: [PATCH] Add login command to log in to a Portainer instance --- Dockerfile | 1 + cmd/login.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 cmd/login.go diff --git a/Dockerfile b/Dockerfile index d2a1df2..f0fb129 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ ENV PSU_AUTHENTICATION_PASSWORD="" \ PSU_CONNECTION_URL="" \ PSU_DEBUG="" \ PSU_ENDPOINT_LIST_FORMAT="" \ + PSU_LOGIN_PRINT="" \ PSU_STACK_DEPLOY_ENDPOINT="" \ PSU_STACK_DEPLOY_ENV_FILE="" \ PSU_STACK_DEPLOY_REPLACE_ENV="" \ diff --git a/cmd/login.go b/cmd/login.go new file mode 100644 index 0000000..8d76db3 --- /dev/null +++ b/cmd/login.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "fmt" + "github.com/greenled/portainer-stack-utils/common" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// loginCmd represents the login command +var loginCmd = &cobra.Command{ + Use: "login", + Short: "Log in to a Portainer instance", + Run: func(cmd *cobra.Command, args []string) { + // Get auth token + authToken, authenticationTokenRetrievalError := common.GetNewAuthenticationToken() + common.CheckError(authenticationTokenRetrievalError) + + if viper.GetBool("login.print") { + fmt.Println(authToken) + } + + // Save auth token + configSettingErr := setConfig("auth-token", authToken) + common.CheckError(configSettingErr) + }, +} + +func init() { + rootCmd.AddCommand(loginCmd) + + loginCmd.Flags().Bool("print", false, "prints retrieved auth token") + viper.BindPFlag("login.print", loginCmd.Flags().Lookup("print")) +}