From 19f53911ceba97ca269b4af55afd03d405703312 Mon Sep 17 00:00:00 2001 From: gjergj Date: Fri, 26 Apr 2024 09:55:59 +0200 Subject: [PATCH] add signout command --- README.md | 15 ++++++++++----- cmd/testmyapp/client.go | 2 +- cmd/testmyapp/config.go | 16 ++++++++++++++++ cmd/testmyapp/main.go | 1 + cmd/testmyapp/signout.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 cmd/testmyapp/signout.go diff --git a/README.md b/README.md index 32bc7e6..3f358d9 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,6 @@ sudo dpkg -i testmyapp_0.0.68_amd64.deb testmyapp signup -u= ``` -#### Login -```bas -testmyapp login -u= -``` - #### Upload your web site Create an `index.html` file in the current directory and upload it to the project. ```bash @@ -66,6 +61,16 @@ testmyapp watch ``` Refresh browser to see changes. +#### Logout +```bash +testmyapp signout +``` + +#### Login +```bas +testmyapp login -u= +``` + ### Update ```bash brew update diff --git a/cmd/testmyapp/client.go b/cmd/testmyapp/client.go index 69e5b7e..232ec96 100644 --- a/cmd/testmyapp/client.go +++ b/cmd/testmyapp/client.go @@ -206,7 +206,7 @@ func (c *CustomHTTPClient) Login(username, password string) (string, string, str // Check the response status if response.StatusCode != http.StatusOK { - fmt.Printf("HTTP request failed with status code: %d\n", response.StatusCode) + err = fmt.Errorf("HTTP request failed with status code: %d\n", response.StatusCode) return "", "", "", err } diff --git a/cmd/testmyapp/config.go b/cmd/testmyapp/config.go index 6945a68..bf2b778 100644 --- a/cmd/testmyapp/config.go +++ b/cmd/testmyapp/config.go @@ -142,6 +142,22 @@ func (c *Config) Token(username string) (string, string, string) { return p.Token, p.UserID, username } +func (c *Config) Clear(username string) { + //username not specified and more than one account exists + if len(c.Accounts) > 1 && username == "" { + fmt.Println("Please specify an account") + return + } + _, ok := c.Accounts[username] + if !ok { + // return the first account + for k, _ := range c.Accounts { + username = k + } + } + delete(c.Accounts, username) +} + func (c *Config) RefreshToken(username string) (string, string) { //username not specified and more than one account exists if len(c.Accounts) > 1 && username == "" { diff --git a/cmd/testmyapp/main.go b/cmd/testmyapp/main.go index 5777ed8..48d286b 100644 --- a/cmd/testmyapp/main.go +++ b/cmd/testmyapp/main.go @@ -41,6 +41,7 @@ func main() { versionCommand(), deleteCommand(), signupCommand(), + signoutCommand(), }, Exec: run, } diff --git a/cmd/testmyapp/signout.go b/cmd/testmyapp/signout.go new file mode 100644 index 0000000..4b7af6e --- /dev/null +++ b/cmd/testmyapp/signout.go @@ -0,0 +1,35 @@ +package main + +import ( + "context" + "flag" + "github.com/peterbourgon/ff/v3/ffcli" + "log" +) + +// signout Command creates the "signupCommand" subcommand +func signoutCommand() *ffcli.Command { + c, err := getConfig() + if err != nil { + log.Fatal(err) + } + fs := flag.NewFlagSet("testmyapp signout", flag.ExitOnError) + var ( + loginFlags struct { + // Add flags specific to the "login" subcommand + username string + } + ) + + fs.StringVar(&loginFlags.username, "u", "", "u option for login command") + + return &ffcli.Command{ + Name: "signout", + ShortUsage: "signout [flags]", + FlagSet: fs, + Exec: func(_ context.Context, args []string) error { + c.Clear(loginFlags.username) + return c.Save() + }, + } +}