Skip to content

Commit c032959

Browse files
authored
feat: #173 Adds support for logout command (#176)
Signed-off-by: Harsh4902 <[email protected]>
1 parent 3eeb662 commit c032959

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func NewCommad() *cobra.Command {
4646
command.AddCommand(NewStopCommand())
4747
command.AddCommand(NewContextCommand())
4848
command.AddCommand(NewLoginCommand(&clientOpts))
49+
command.AddCommand(NewLogoutCommand(&clientOpts))
4950

5051
defaultLocalConfigPath, err := config.DefaultLocalConfigPath()
5152
errors.CheckError(err)

cmd/logout.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/microcks/microcks-cli/pkg/config"
9+
"github.com/microcks/microcks-cli/pkg/connectors"
10+
"github.com/microcks/microcks-cli/pkg/errors"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func NewLogoutCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
15+
16+
logoutCmd := &cobra.Command{
17+
Use: "logout CONTEXT",
18+
Short: "Log out from Microcks",
19+
Long: "Log out from Microcks",
20+
Example: `# To log out of Microcks
21+
$ microcks logout`,
22+
23+
Run: func(cmd *cobra.Command, args []string) {
24+
if len(args) == 0 {
25+
cmd.HelpFunc()(cmd, args)
26+
os.Exit(1)
27+
}
28+
29+
context := args[0]
30+
localCfg, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
31+
errors.CheckError(err)
32+
if localCfg == nil {
33+
log.Fatalf("Nothing to logout from")
34+
}
35+
36+
// Remove authToken
37+
ok := localCfg.RemoveToken(context)
38+
if !ok {
39+
log.Fatalf("Context %s does not exist", context)
40+
}
41+
42+
err = config.ValidateLocalConfig(*localCfg)
43+
if err != nil {
44+
log.Fatalf("Error in loging out: %s", err)
45+
}
46+
err = config.WriteLocalConfig(*localCfg, globalClientOpts.ConfigPath)
47+
errors.CheckError(err)
48+
49+
fmt.Printf("Logged out from '%s'\n", context)
50+
},
51+
}
52+
53+
return logoutCmd
54+
}

pkg/config/localconfig.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ func (l *LocalConfig) RemoveContext(serverName string) (string, bool) {
222222
return "", false
223223
}
224224

225+
// Returns true if user was removed successfully
226+
func (l *LocalConfig) RemoveToken(serverName string) bool {
227+
for i, u := range l.Users {
228+
if u.Name == serverName {
229+
l.Users[i].RefreshToken = ""
230+
l.Users[i].AuthToken = ""
231+
return true
232+
}
233+
}
234+
return false
235+
}
236+
225237
func (l *LocalConfig) GetUser(name string) (*User, error) {
226238
for _, u := range l.Users {
227239
if u.Name == name {

0 commit comments

Comments
 (0)