github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/auth/logout.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package auth 21 22 import ( 23 "bufio" 24 "encoding/json" 25 "fmt" 26 "io" 27 28 "github.com/spf13/cobra" 29 "k8s.io/cli-runtime/pkg/genericiooptions" 30 31 "github.com/1aal/kubeblocks/pkg/cli/cmd/auth/authorize" 32 "github.com/1aal/kubeblocks/pkg/cli/cmd/auth/utils" 33 ) 34 35 type LogOutOptions struct { 36 authorize.Options 37 Region string 38 39 Provider authorize.Provider 40 } 41 42 func NewLogout(streams genericiooptions.IOStreams) *cobra.Command { 43 o := &LogOutOptions{Options: authorize.Options{IOStreams: streams}} 44 cmd := &cobra.Command{ 45 Use: "logout", 46 Short: "Log out of the KubeBlocks Cloud", 47 Run: func(cmd *cobra.Command, args []string) { 48 cobra.CheckErr(o.complete()) 49 cobra.CheckErr(o.validate()) 50 cobra.CheckErr(o.run(cmd)) 51 }, 52 } 53 54 cmd.Flags().StringVarP(&o.Region, "region", "r", "jp", "Specify the region [jp] to log in.") 55 return cmd 56 } 57 58 func (o *LogOutOptions) complete() error { 59 o.AuthURL = getAuthURL(o.Region) 60 61 var err error 62 o.Provider, err = authorize.NewTokenProvider(o.Options) 63 if err != nil { 64 return err 65 } 66 if o.ClientID == "" { 67 return o.loadConfig() 68 } 69 return nil 70 } 71 72 func (o *LogOutOptions) validate() error { 73 if o.ClientID == "" { 74 return fmt.Errorf("client ID is required") 75 } 76 return nil 77 } 78 79 func (o *LogOutOptions) run(cmd *cobra.Command) error { 80 if utils.IsTTY() { 81 fmt.Fprintln(o.Out, "Press Enter to log out of the KubeBlocks Cloud.") 82 _ = waitForEnter(cmd.InOrStdin()) 83 } 84 85 err := o.Provider.Logout(cmd.Context()) 86 if err != nil { 87 return err 88 } 89 90 fmt.Fprintln(o.Out, "Successfully logged out.") 91 return nil 92 } 93 94 func waitForEnter(r io.Reader) error { 95 scanner := bufio.NewScanner(r) 96 scanner.Scan() 97 return scanner.Err() 98 } 99 100 func (o *LogOutOptions) loadConfig() error { 101 data, err := utils.Asset("config/config.enc") 102 if err != nil { 103 return err 104 } 105 106 err = json.Unmarshal(data, &o.Options) 107 if err != nil { 108 return err 109 } 110 111 o.Provider, err = authorize.NewTokenProvider(o.Options) 112 if err != nil { 113 return err 114 } 115 return nil 116 }