Skip to content

Commit 5980d6c

Browse files
author
scttymn
committed
adding logout
1 parent 432cbc6 commit 5980d6c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

cmd/login.go renamed to cmd/auth.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"strings"
78

89
"github.com/spf13/cobra"
910
)
@@ -21,6 +22,18 @@ This command will open a browser window for authentication.`,
2122
},
2223
}
2324

25+
var logoutCmd = &cobra.Command{
26+
Use: "logout",
27+
Short: "Logout from Google Cloud",
28+
Long: `Logout from Google Cloud by revoking authentication credentials.`,
29+
Run: func(cmd *cobra.Command, args []string) {
30+
if err := runLogout(); err != nil {
31+
fmt.Fprintf(os.Stderr, "Error during logout: %v\n", err)
32+
os.Exit(1)
33+
}
34+
},
35+
}
36+
2437
func runLogin() error {
2538
fmt.Println("🔐 Authenticating with Google Cloud...")
2639

@@ -55,4 +68,41 @@ func runLogin() error {
5568

5669
fmt.Println("✅ Authentication complete!")
5770
return nil
71+
}
72+
73+
func runLogout() error {
74+
fmt.Println("🔐 Logging out from Google Cloud...")
75+
76+
// Check if gcloud is installed
77+
if _, err := exec.LookPath("gcloud"); err != nil {
78+
return fmt.Errorf("gcloud CLI not found. Please install the Google Cloud SDK: https://cloud.google.com/sdk/docs/install")
79+
}
80+
81+
// Get current authenticated account
82+
cmd := exec.Command("gcloud", "auth", "list", "--filter=status:ACTIVE", "--format=value(account)")
83+
output, err := cmd.Output()
84+
if err != nil {
85+
fmt.Println("⚠️ No active authentication found")
86+
return nil
87+
}
88+
89+
account := strings.TrimSpace(string(output))
90+
if account == "" {
91+
fmt.Println("⚠️ No active authentication found")
92+
return nil
93+
}
94+
95+
fmt.Printf("🔓 Revoking credentials for: %s\n", account)
96+
97+
// Revoke authentication
98+
revokeCmd := exec.Command("gcloud", "auth", "revoke", account)
99+
revokeCmd.Stdout = os.Stdout
100+
revokeCmd.Stderr = os.Stderr
101+
102+
if err := revokeCmd.Run(); err != nil {
103+
return fmt.Errorf("gcloud auth revoke failed: %w", err)
104+
}
105+
106+
fmt.Println("✅ Successfully logged out from Google Cloud")
107+
return nil
58108
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ func Execute() {
2525

2626
func init() {
2727
rootCmd.AddCommand(loginCmd)
28+
rootCmd.AddCommand(logoutCmd)
2829
}

0 commit comments

Comments
 (0)