Skip to content
Snippets Groups Projects
Commit f154a60d authored by Florent Gluck's avatar Florent Gluck
Browse files

Completed userdel with csv support

Refined useradd
Bumped to version 1.4.1
parent cccd6cce
No related branches found
No related tags found
No related merge requests found
...@@ -80,7 +80,7 @@ func (cmd *Add)Run(args []string) int { ...@@ -80,7 +80,7 @@ func (cmd *Add)Run(args []string) int {
csvFile := args[0] csvFile := args[0]
file, err := os.Open(csvFile) file, err := os.Open(csvFile)
if err != nil { if err != nil {
u.PrintlnErr("Failed opening \""+csvFile+"\" "+err.Error()) u.PrintlnErr("Error: "+err.Error())
return 1 return 1
} }
defer file.Close() defer file.Close()
...@@ -93,13 +93,13 @@ func (cmd *Add)Run(args []string) int { ...@@ -93,13 +93,13 @@ func (cmd *Add)Run(args []string) int {
break break
} }
if err != nil { if err != nil {
u.PrintlnErr("Failed reading "+err.Error()) u.PrintlnErr("FAILED reading "+err.Error())
continue continue
} }
columnCount := len(columns) columnCount := len(columns)
if columnCount < 5 { if columnCount < 5 {
u.PrintlnErr("Failed reading record on line ",line,": 5 columns are expected") u.PrintlnErr("FAILED reading record on line ",line,": expecting 5 columns")
continue continue
} }
...@@ -118,7 +118,7 @@ func (cmd *Add)Run(args []string) int { ...@@ -118,7 +118,7 @@ func (cmd *Add)Run(args []string) int {
} }
if err := cmd.runRequest(cargs); err != nil { if err := cmd.runRequest(cargs); err != nil {
u.PrintlnErr("Failed creating user "+cargs.Email+": "+err.Error()) u.PrintlnErr("FAILED creating user "+cargs.Email+": "+err.Error())
} else { } else {
u.Println("Successfully created user "+cargs.Email) u.Println("Successfully created user "+cargs.Email)
} }
...@@ -141,7 +141,6 @@ func (cmd *Add)runRequest(args *cmdArgs) error { ...@@ -141,7 +141,6 @@ func (cmd *Add)runRequest(args *cmdArgs) error {
} }
if resp.IsSuccess() { if resp.IsSuccess() {
u.Println(resp)
return nil return nil
} else { } else {
return errors.New("Error: "+resp.Status()+": "+resp.String()) return errors.New("Error: "+resp.Status()+": "+resp.String())
......
package cmdUser package cmdUser
import ( import (
"io"
"os"
"errors"
"encoding/csv"
u "nexus-client/utils" u "nexus-client/utils"
g "nexus-client/globals" g "nexus-client/globals"
) )
...@@ -25,13 +29,12 @@ func (cmd *Del)PrintUsage() { ...@@ -25,13 +29,12 @@ func (cmd *Del)PrintUsage() {
} }
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
u.PrintlnErr("USAGE: "+cmd.Name+" email [email ...]") u.PrintlnErr("USAGE: "+cmd.Name+" email [email ...]")
u.PrintlnErr(" "+cmd.Name+" file.csv")
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
u.PrintlnErr("file.csv 1-column CSV file containing emails of users to delete.")
} }
func (cmd *Del)Run(args []string) int { func (cmd *Del)Run(args []string) int {
client := g.GetInstance().Client
host := g.GetInstance().Host
argc := len(args) argc := len(args)
if argc < 1 { if argc < 1 {
cmd.PrintUsage() cmd.PrintUsage()
...@@ -40,21 +43,80 @@ func (cmd *Del)Run(args []string) int { ...@@ -40,21 +43,80 @@ func (cmd *Del)Run(args []string) int {
statusCode := 0 statusCode := 0
// Iterates through each user (email) to delete if argc == 1 && !u.IsEmail(args[0]) {
for i:= range(args) { // Single argument and it's a CSV file
email := args[i] csvFile := args[0]
resp, err := client.R().Delete(host+"/users/"+email)
if u.IsEmail(csvFile) {
cmd.PrintUsage()
return 1
}
file, err := os.Open(csvFile)
if err != nil { if err != nil {
u.PrintlnErr("Error: "+err.Error()) u.PrintlnErr("Error: "+err.Error())
return 1
}
defer file.Close()
reader := csv.NewReader(file)
line := 0
for {
line += 1
columns, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
u.PrintlnErr("FAILED reading "+err.Error())
statusCode = 1
continue
}
columnCount := len(columns)
if columnCount != 1 {
u.PrintlnErr("FAILED reading record on line ",line,": expecting 1 column")
statusCode = 1
continue
}
email := columns[0]
if err := cmd.runRequest(email); err != nil {
u.PrintlnErr(err.Error())
statusCode = 1 statusCode = 1
} else { } else {
if resp.IsSuccess() { u.Println("Successfully deleted user "+email)
u.Println("User "+email+" deleted.") }
}
} else { } else {
u.PrintlnErr("Error: "+resp.Status()+": "+resp.String()) // Argument(s) is/are email addresses
// Iterates through each user (email) to delete
for i:= range(args) {
email := args[i]
if err := cmd.runRequest(email); err != nil {
u.PrintlnErr(err.Error())
statusCode = 1 statusCode = 1
} else {
u.Println("Successfully deleted user "+email)
} }
} }
} }
return statusCode return statusCode
} }
func (cmd *Del)runRequest(email string) error {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Delete(host+"/users/"+email)
if err != nil {
return errors.New("Error: "+err.Error())
}
if resp.IsSuccess() {
return nil
} else {
return errors.New("Error: "+resp.Status()+": "+resp.String())
}
}
...@@ -36,7 +36,7 @@ template ID of the template to VM will be based on. ...@@ -36,7 +36,7 @@ template ID of the template to VM will be based on.
count Number of VMs to create (if not specified, one is created). count Number of VMs to create (if not specified, one is created).
If specified and > 1, each VM's name is postfixed with [n], If specified and > 1, each VM's name is postfixed with [n],
where n ranges from 1..count. where n ranges from 1..count.
file.csv Single-column CSV file defining the students names. file.csv 1-column CSV file defining the students names.
Each VM's name is postfixed with each entry in the CSV file.` Each VM's name is postfixed with each entry in the CSV file.`
u.PrintlnErr(usage) u.PrintlnErr(usage)
} }
...@@ -71,10 +71,9 @@ func (cmd *Create)Run(args []string) int { ...@@ -71,10 +71,9 @@ func (cmd *Create)Run(args []string) int {
if argc == 6 { if argc == 6 {
count, err = strconv.Atoi(args[5]) count, err = strconv.Atoi(args[5])
if err != nil { if err != nil {
// It's not a number, so we assume it's a CSV file. // It's not a number, we assume it's a CSV file and parse it.
// Checks if the file exists. csvFile := args[5]
file := args[5] csvEntries, err = u.ReadCSVColumn(csvFile, 0)
csvEntries, err = u.ReadCSVColumn(file, 0)
if err != nil { if err != nil {
u.PrintlnErr(err.Error()) u.PrintlnErr(err.Error())
return 1 return 1
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
func ReadCSVColumn(csvFile string, column int) ([]string, error) { func ReadCSVColumn(csvFile string, column int) ([]string, error) {
file, err := os.Open(csvFile) file, err := os.Open(csvFile)
if err != nil { if err != nil {
return nil, errors.New("Failed opening \""+csvFile+"\"") return nil, errors.New("Error: "+err.Error())
} }
defer file.Close() defer file.Close()
reader := csv.NewReader(file) reader := csv.NewReader(file)
...@@ -24,7 +24,7 @@ func ReadCSVColumn(csvFile string, column int) ([]string, error) { ...@@ -24,7 +24,7 @@ func ReadCSVColumn(csvFile string, column int) ([]string, error) {
break break
} }
if err != nil { if err != nil {
return nil, errors.New("Failed reading \""+csvFile+"\"") return nil, errors.New("Failed reading "+err.Error())
} }
if column >= len(record) { if column >= len(record) {
return nil, errors.New("Failed reading \""+csvFile+"\": column out of bound") return nil, errors.New("Failed reading \""+csvFile+"\": column out of bound")
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"bytes" "bytes"
"errors" "errors"
"net/mail"
"archive/tar" "archive/tar"
"path/filepath" "path/filepath"
"compress/gzip" "compress/gzip"
...@@ -99,3 +100,8 @@ func GetRandomTempFilename() (string, error) { ...@@ -99,3 +100,8 @@ func GetRandomTempFilename() (string, error) {
randName := "temp_"+uuid.String() randName := "temp_"+uuid.String()
return filepath.Join(tempDir, randName), nil return filepath.Join(tempDir, randName), nil
} }
func IsEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
const ( const (
major = 1 major = 1
minor = 4 minor = 4
bugfix = 0 bugfix = 1
) )
type Version struct { type Version struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment