mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
42 lines
951 B
Go
42 lines
951 B
Go
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
|
|
config := common.GetDefaultClientConfig()
|
|
config.DoNotUseToken = true
|
|
|
|
client, err := common.NewClient(common.GetDefaultHttpClient(), config)
|
|
common.CheckError(err)
|
|
|
|
authToken, err := client.Authenticate()
|
|
common.CheckError(err)
|
|
|
|
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"))
|
|
}
|