From 51d7b894746027cc5c8040ccd7cbaa9658e56f24 Mon Sep 17 00:00:00 2001
From: tss24 <tss24@inf.ufpr.br>
Date: Thu, 27 Feb 2025 09:42:59 -0300
Subject: [PATCH] Improve some stuff

---
 README.md                         | 53 ++++++++++++++++++++++++++-----
 cmd/user.go                       |  2 +-
 cmd/user/mod.go                   |  5 +--
 cmd/user/{delete.go => remove.go} | 17 +++++-----
 cmd/user/show.go                  |  5 +++
 5 files changed, 62 insertions(+), 20 deletions(-)
 rename cmd/user/{delete.go => remove.go} (92%)

diff --git a/README.md b/README.md
index 6e26693..2462509 100644
--- a/README.md
+++ b/README.md
@@ -10,8 +10,7 @@ To install, just clone the repo:
 
 and build the binary:
 
-    cd useradm
-    go build -o useradm
+    cd useradm && go build -o useradm
 
 if you want add it the go path use:
 
@@ -42,7 +41,6 @@ in flags the command needs identifiers to make the search, such as:
 here we are searching for a user with name with pedro, grr with 2024, is part 
 of the group 'bcc' and -i is to make the search case insensitive.
 
-
 ### Create
 
 To create a user we can use:
@@ -77,7 +75,6 @@ be removed from LDAP and Kerberos, their directories will end up at:
     webdir: /home/contas_removidas/html/<Year of removal>/
     webdir: /nobackup/contas_removidas/<Year of removal>/
 
-
 ### Modify
 
 To modify a user we can do:
@@ -141,7 +138,9 @@ Then we generate the request that will give us all the users, get the response,
 and store the result in structs.
 
 Many other requests are made in the code and you can learn the syntax by reading
-it or via the docs of the module.
+it or via the docs of the module. One thing I want to add it that in LDAP, group
+assingments need to be done by setting the user object to be member of the group
+and set the group object to have the user as a member. Interesting...
 
 ### Kerberos
 
@@ -163,10 +162,48 @@ that minimizes those misfortunes.
 
 `useradm.py` had 3 function to generate names that used a very wierd algorithm.
 At first I just copied them, then I thought it was too long and repetitive so
-I merged them into one (See commit e303dcc1). It was VERY ugly, so Fernando K. 
-helped and rewrote/improved the algorithm toa more mantainable state, thanks :) 
+I merged them into one (See commit e49eca75). It was VERY ugly, so Fernando K. 
+helped and rewrote/improved the algorithm to a more mantainable state, thanks :) 
 (See commit e9762f8b)
 
-TODO: explain it
+There are 3 types of login generation: ini, first, last. Instead of explaining
+how the algorithm works, I believe it is best to just show examples. So for the
+user with name Fabiano Antunes Pereira de Souza, with GRR 20241982:
+
+'ini' would create the following (based on `variance` variable):
+
+    faps24
+    faaps24
+    faanps24
+    faanpes24
+    faanpeso24
+    fabanpeso24
+
+and so on. 'first' would create something like this:
+
+    fabiano
+    fabianoa
+    fabianoap
+    fabianoaps
+    fabianoanps
+    fabianoanpes
+    fabianoanpeso
+
+and so on. 'last' would generate:
+
+    fapsouza
+    faapsouza
+    faanpsouza
+    faanpesouza
+    fabanpesouza
+    fabantpesouza
+
+and so on...
+
+Essentially we add one letter to each name part for each variance added. We also
+remove any connectives (do, da, de, dos, von...) from the name. If there comes a 
+time a person appears with a new connective, update the formatName() function
+currently at `/cmd/user/create.go` to account for that :)
+
 
 TODO: finish...
diff --git a/cmd/user.go b/cmd/user.go
index 177e376..0b3ae43 100644
--- a/cmd/user.go
+++ b/cmd/user.go
@@ -15,7 +15,7 @@ new users, please do so with the bulk subcommand.`,
 
 func init() {
 	userCmd.AddCommand(user.CreateUserCmd)
-	userCmd.AddCommand(user.DeleteUserCmd)
+	userCmd.AddCommand(user.RemoveUserCmd)
 	userCmd.AddCommand(user.ModCmd)
 	userCmd.AddCommand(user.ShowCmd)
 	userCmd.AddCommand(user.ResetCmd)
diff --git a/cmd/user/mod.go b/cmd/user/mod.go
index c53b005..4cfc382 100644
--- a/cmd/user/mod.go
+++ b/cmd/user/mod.go
@@ -99,8 +99,8 @@ func modifyUserFunc(cmd *cobra.Command, args []string) error {
 	}
 
 	if err := clearCache(); err != nil {
-		fmt.Printf(`Failed to reload cache
-all is ok but may take a while to apply
+		fmt.Printf(`Failed to reload cache!
+all is ok but may take a while to apply the changes
 Output: %v`, err)
 	} else {
 		fmt.Printf("Changes applied!\n")
@@ -174,6 +174,7 @@ func applyChangesToUser(c model.User, n cfg) model.User {
 	return c
 }
 
+// generates a yaml file of a config state for the user to edit
 func promptUserYaml(state cfg) (cfg, error) {
 	var newState cfg
 	tmpFile, err := os.CreateTemp("", "config-*.yaml")
diff --git a/cmd/user/delete.go b/cmd/user/remove.go
similarity index 92%
rename from cmd/user/delete.go
rename to cmd/user/remove.go
index d39b84d..0796886 100644
--- a/cmd/user/delete.go
+++ b/cmd/user/remove.go
@@ -21,20 +21,18 @@ var (
 	WEB_TRASH    = "/home/contas_removidas/html/" + ANO
 )
 
-var DeleteUserCmd = &cobra.Command{
-	Use:   "delete",
+var RemoveUserCmd = &cobra.Command{
+	Use:   "remove [username]",
 	Short: "Delete a user",
-	RunE:  deleteUserFunc,
+    Args:  cobra.ExactArgs(1),
+	RunE:  removeUserFunc,
 }
 
 func init() {
-	DeleteUserCmd.Flags().StringP("login", "l", "", "User login for removal")
-	DeleteUserCmd.Flags().BoolP("confirm", "y", false, "Skip confirmation prompt")
-
-	DeleteUserCmd.MarkFlagRequired("login")
+	RemoveUserCmd.Flags().BoolP("confirm", "y", false, "Skip confirmation prompt")
 }
 
-func deleteUserFunc(cmd *cobra.Command, args []string) error {
+func removeUserFunc(cmd *cobra.Command, args []string) error {
 	var opts model.Opts
 	success := false
 
@@ -43,7 +41,8 @@ func deleteUserFunc(cmd *cobra.Command, args []string) error {
 		return err
 	}
 
-	u, err := locateUser(opts.UID)
+    login := args[0]
+	u, err := locateUser(login)
 	if err != nil {
 		return err
 	}
diff --git a/cmd/user/show.go b/cmd/user/show.go
index 882705b..1bbbc57 100644
--- a/cmd/user/show.go
+++ b/cmd/user/show.go
@@ -54,6 +54,11 @@ func searchUserFunc(cmd *cobra.Command, args []string) error {
 	filtered := searchUser(users, o.Ignore, o.UID, o.GID,
 		o.Name, o.GRR, o.Status, o.Homedir)
 
+    if len(filtered) == 0 {
+        fmt.Printf("No user matched the search!")
+        return nil
+    }
+
 	for i := range filtered {
 		fmt.Printf("%v\n\n", filtered[i].ToString())
 	}
-- 
GitLab