-
Notifications
You must be signed in to change notification settings - Fork 5
/
logout_cmd.go
49 lines (44 loc) · 1.14 KB
/
logout_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
)
// TODO: finish
var LogoutCmd = &cli.Command{
Name: "logout",
Usage: "[--all] [PHONE NUMBER] -- logout from InPost",
Description: "Without any arguments it asks you what account you want to remove.",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "removes all accounts from the configuration file",
},
},
Action: func(c *cli.Context) error {
cfg := GetConfig(c)
if c.Bool("all") {
log.Printf("Logging out %d accounts", len(cfg.Accounts))
cfg.Accounts = []*ConfigAccount{}
return SaveConfig(c)
}
phoneNumber := c.Args().First()
if phoneNumber == "" {
fmt.Print("Enter the phone number you want to remove: ")
reader := bufio.NewReader(os.Stdin)
phoneNumber, _ = reader.ReadString('\n')
phoneNumber = strings.Replace(phoneNumber, "\r", "", -1)
phoneNumber = strings.Replace(phoneNumber, "\n", "", -1)
}
for i, account := range cfg.Accounts {
if account.PhoneNumber == phoneNumber {
cfg.Accounts = append(cfg.Accounts[:i], cfg.Accounts[i+1:]...)
break
}
}
return SaveConfig(c)
},
}