From ea33f6d9e8f2ddcffbfc43b54c2f6605322faf8f Mon Sep 17 00:00:00 2001
From: Florent <florent.gluck@hesge.ch>
Date: Sun, 22 Dec 2024 20:56:44 +0100
Subject: [PATCH] libclient: added userUpdatePwd and userWhoAmI

---
 src/client/cmdUser/helper.go        | 37 -----------------------------
 src/client/cmdUser/userList.go      | 20 ++++++++++++++++
 src/client/cmdUser/userUpdatePwd.go | 25 ++++++-------------
 src/client/cmdUser/userWhoami.go    | 20 +++++-----------
 src/libclient/template/helper.go    |  6 ++---
 src/libclient/user/helper.go        |  4 ++--
 src/libclient/user/userUpdatePwd.go | 24 +++++++++++++++++++
 src/libclient/user/userWhoAmI.go    | 28 ++++++++++++++++++++++
 8 files changed, 90 insertions(+), 74 deletions(-)
 delete mode 100644 src/client/cmdUser/helper.go
 create mode 100644 src/libclient/user/userUpdatePwd.go
 create mode 100644 src/libclient/user/userWhoAmI.go

diff --git a/src/client/cmdUser/helper.go b/src/client/cmdUser/helper.go
deleted file mode 100644
index 9d40a73..0000000
--- a/src/client/cmdUser/helper.go
+++ /dev/null
@@ -1,37 +0,0 @@
-package cmdUser
-
-import (
-	"encoding/json"
-	"nexus-client/cmd"
-	u "nexus-client/utils"
-	"nexus-common/params"
-
-	"github.com/go-resty/resty/v2"
-)
-
-func printRegexUsage(c cmd.Command) {
-	for _, desc := range c.GetDesc() {
-		u.PrintlnErr(desc)
-	}
-	u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
-	u.PrintlnErr("USAGE: ", c.GetName(), " [regex ...]")
-	u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
-}
-
-func printRegexUsageDetails() {
-	const usage string = `Only users matching the specified regexes will be listed.
-Any number of regexes can be specified.
-The regex matches the user email, first name and last name and is case-insensitive.
-Regex examples:
-"."   -> matches any users
-"bla" -> matches any users containing "bla"`
-	u.PrintlnErr(usage)
-}
-
-func deserializeUsers(resp *resty.Response) ([]params.UserWithoutPwd, error) {
-	users := []params.UserWithoutPwd{}
-	if err := json.Unmarshal(resp.Body(), &users); err != nil {
-		return nil, err
-	}
-	return users, nil
-}
diff --git a/src/client/cmdUser/userList.go b/src/client/cmdUser/userList.go
index bc02c91..c6dc9ad 100644
--- a/src/client/cmdUser/userList.go
+++ b/src/client/cmdUser/userList.go
@@ -2,6 +2,7 @@ package cmdUser
 
 import (
 	"fmt"
+	"nexus-client/cmd"
 	u "nexus-client/utils"
 	"nexus-common/params"
 	libclient "nexus-libclient/user"
@@ -121,3 +122,22 @@ func (cmd *List) Run(args []string) int {
 
 	return 0
 }
+
+func printRegexUsage(c cmd.Command) {
+	for _, desc := range c.GetDesc() {
+		u.PrintlnErr(desc)
+	}
+	u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
+	u.PrintlnErr("USAGE: ", c.GetName(), " [regex ...]")
+	u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
+}
+
+func printRegexUsageDetails() {
+	const usage string = `Only users matching the specified regexes will be listed.
+Any number of regexes can be specified.
+The regex matches the user email, first name and last name and is case-insensitive.
+Regex examples:
+"."   -> matches any users
+"bla" -> matches any users containing "bla"`
+	u.PrintlnErr(usage)
+}
diff --git a/src/client/cmdUser/userUpdatePwd.go b/src/client/cmdUser/userUpdatePwd.go
index 4c6e38d..be9669e 100644
--- a/src/client/cmdUser/userUpdatePwd.go
+++ b/src/client/cmdUser/userUpdatePwd.go
@@ -3,8 +3,7 @@ package cmdUser
 import (
 	"bytes"
 	u "nexus-client/utils"
-	"nexus-common/params"
-	g "nexus-libclient/globals"
+	libclient "nexus-libclient/user"
 
 	"golang.org/x/crypto/ssh/terminal"
 )
@@ -32,9 +31,6 @@ func (cmd *UpdatePwd) PrintUsage() {
 }
 
 func (cmd *UpdatePwd) Run(args []string) int {
-	client := g.GetInstance().Client
-	host := g.GetInstance().Host
-
 	argc := len(args)
 	if argc != 0 {
 		cmd.PrintUsage()
@@ -57,24 +53,17 @@ func (cmd *UpdatePwd) Run(args []string) int {
 	u.Println("")
 
 	if !bytes.Equal(newPwd, newPwd2) {
-		u.PrintlnErr("Sorry, passwords do not match!")
+		u.PrintlnErr("Error: passwords do not match!")
 		u.PrintlnErr("Password unchanged.")
 		return 1
 	}
 
-	pwdStr := string(newPwd)
-	p := &params.UserSetPwd{pwdStr}
-	resp, err := client.R().SetBody(p).Put(host + "/users/pwd")
+	err = libclient.UserUpdatePwd(string(newPwd))
 	if err != nil {
-		u.PrintlnErr("Error: " + err.Error())
+		u.PrintlnErr(err.Error())
 		return 1
-	} else {
-		if resp.IsSuccess() {
-			u.Println("Password changed successfully.")
-			return 0
-		} else {
-			u.PrintlnErr(resp.Status() + ": " + resp.String())
-			return 1
-		}
 	}
+
+	u.Println("Password changed successfully.")
+	return 0
 }
diff --git a/src/client/cmdUser/userWhoami.go b/src/client/cmdUser/userWhoami.go
index 3af8624..915d302 100644
--- a/src/client/cmdUser/userWhoami.go
+++ b/src/client/cmdUser/userWhoami.go
@@ -2,7 +2,7 @@ package cmdUser
 
 import (
 	u "nexus-client/utils"
-	g "nexus-libclient/globals"
+	libclient "nexus-libclient/user"
 )
 
 type Whoami struct {
@@ -33,20 +33,12 @@ func (cmd *Whoami) Run(args []string) int {
 		return 1
 	}
 
-	client := g.GetInstance().Client
-	host := g.GetInstance().Host
-
-	resp, err := client.R().Get(host + "/users/whoami")
+	user, err := libclient.UserWhoAmI()
 	if err != nil {
-		u.PrintlnErr("Error: " + err.Error())
+		u.PrintlnErr(err.Error())
 		return 1
-	} else {
-		if resp.IsSuccess() {
-			u.Println(resp)
-			return 0
-		} else {
-			u.PrintlnErr(resp.Status() + ": " + resp.String())
-			return 1
-		}
 	}
+
+	u.Println(user.String())
+	return 0
 }
diff --git a/src/libclient/template/helper.go b/src/libclient/template/helper.go
index 969ce54..fe3e580 100644
--- a/src/libclient/template/helper.go
+++ b/src/libclient/template/helper.go
@@ -18,9 +18,9 @@ func deserializeTemplates(resp *resty.Response) ([]t.TemplateSerialized, error)
 
 // Deserialize a single template from an http response.
 func deserializeTemplate(resp *resty.Response) (*t.TemplateSerialized, error) {
-	var tpl *t.TemplateSerialized = &t.TemplateSerialized{}
+	tpl := t.TemplateSerialized{}
 	if err := json.Unmarshal(resp.Body(), &tpl); err != nil {
-		return tpl, err
+		return nil, err
 	}
-	return tpl, nil
+	return &tpl, nil
 }
diff --git a/src/libclient/user/helper.go b/src/libclient/user/helper.go
index 14b869e..9dddadd 100644
--- a/src/libclient/user/helper.go
+++ b/src/libclient/user/helper.go
@@ -18,11 +18,11 @@ func deserializeUsers(resp *resty.Response) ([]params.UserWithoutPwd, error) {
 
 // Deserialize a single user from an http response.
 func deserializeUser(resp *resty.Response) (*params.UserWithoutPwd, error) {
-	var user *params.UserWithoutPwd = &params.UserWithoutPwd{}
+	user := params.UserWithoutPwd{}
 	if err := json.Unmarshal(resp.Body(), &user); err != nil {
 		return nil, err
 	}
-	return user, nil
+	return &user, nil
 }
 
 // Deserialize user password from http response.
diff --git a/src/libclient/user/userUpdatePwd.go b/src/libclient/user/userUpdatePwd.go
new file mode 100644
index 0000000..ee9db92
--- /dev/null
+++ b/src/libclient/user/userUpdatePwd.go
@@ -0,0 +1,24 @@
+package user
+
+import (
+	"nexus-common/params"
+	g "nexus-libclient/globals"
+	"nexus-libclient/response"
+)
+
+func UserUpdatePwd(newPwd string) error {
+	client := g.GetInstance().Client
+	host := g.GetInstance().Host
+
+	p := &params.UserSetPwd{newPwd}
+	resp, err := client.R().SetBody(p).Put(host + "/users/pwd")
+	if err != nil {
+		return err
+	}
+
+	if resp.IsSuccess() {
+		return nil
+	} else {
+		return response.ErrorToMsg(resp)
+	}
+}
diff --git a/src/libclient/user/userWhoAmI.go b/src/libclient/user/userWhoAmI.go
new file mode 100644
index 0000000..f3a0a21
--- /dev/null
+++ b/src/libclient/user/userWhoAmI.go
@@ -0,0 +1,28 @@
+package user
+
+import (
+	"nexus-common/params"
+	g "nexus-libclient/globals"
+	"nexus-libclient/response"
+)
+
+// NOTE: this route is not implemented in the backend yet.
+func UserWhoAmI() (*params.UserWithoutPwd, error) {
+	client := g.GetInstance().Client
+	host := g.GetInstance().Host
+
+	resp, err := client.R().Get(host + "/users/whoami")
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsSuccess() {
+		user, err := deserializeUser(resp)
+		if err != nil {
+			return nil, err
+		}
+		return user, nil
+	} else {
+		return nil, response.ErrorToMsg(resp)
+	}
+}
-- 
GitLab