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

usersetcaps now takes a csv file in argument to change user capabilities in bulk

parent f154a60d
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ func (cmd *Add)Run(args []string) int {
argc := len(args)
if argc >= 4 {
// Detected syntax: email firstname lastname pwd [caps]
cargs := &cmdArgs {
Email: args[0],
FirstName: args[1],
......@@ -72,11 +73,7 @@ func (cmd *Add)Run(args []string) int {
}
} else if argc == 1 {
// Detected syntax: file.csv
// column1 email
// column2 firstname
// column3 lastname
// column4 password
// column5 capabilities (space separated)`
csvFile := args[0]
file, err := os.Open(csvFile)
if err != nil {
......@@ -110,8 +107,9 @@ func (cmd *Add)Run(args []string) int {
Pwd: columns[3],
Caps: make(map[string]int),
}
if len(columns[4]) > 0 {
caps := strings.Split(columns[4], " ")
capsStr := strings.TrimSpace(columns[4])
if len(capsStr) > 0 {
caps := strings.Split(capsStr, " ")
for _, cap := range caps {
cargs.Caps[cap] = 1
}
......
......@@ -45,6 +45,7 @@ func (cmd *Del)Run(args []string) int {
if argc == 1 && !u.IsEmail(args[0]) {
// Single argument and it's a CSV file
csvFile := args[0]
if u.IsEmail(csvFile) {
......
package cmdUser
import (
"io"
"os"
"errors"
"strings"
"encoding/csv"
u "nexus-client/utils"
g "nexus-client/globals"
)
......@@ -25,41 +30,115 @@ func (cmd *SetCaps)PrintUsage() {
}
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
u.PrintlnErr("USAGE: "+cmd.Name+" email [capability ...]")
u.PrintlnErr(" "+cmd.Name+" file.csv")
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
const usage string = `file.csv 2-column CSV file defining each user's capabilities:
column1 email
column2 capabilities (space separated)`
u.PrintlnErr(usage)
}
func (cmd *SetCaps)Run(args []string) int {
client := g.GetInstance().Client
host := g.GetInstance().Host
type capsArgs struct {
Caps map[string]int `json:"caps" validate:"required"`
}
func (cmd *SetCaps)Run(args []string) int {
argc := len(args)
if argc < 1 {
cmd.PrintUsage()
return 1
}
email := args[0]
statusCode := 0
type CapsArgs struct {
Caps map[string]int `json:"caps" validate:"required"`
}
if argc == 1 && !u.IsEmail(args[0]) {
// Single argument and it's a CSV file
capsArgs := &CapsArgs { make(map[string]int) }
for _, cap := range args[1:] {
capsArgs.Caps[cap] = 1
csvFile := args[0]
if u.IsEmail(csvFile) {
cmd.PrintUsage()
return 1
}
resp, err := client.R().SetBody(capsArgs).Put(host+"/users/"+email+"/caps")
file, err := os.Open(csvFile)
if err != nil {
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 != 2 {
u.PrintlnErr("FAILED reading record on line ",line,": expecting 2 columns")
statusCode = 1
continue
}
userCaps := &capsArgs { make(map[string]int) }
capsStr := strings.TrimSpace(columns[1])
if len(capsStr) > 0 {
caps := strings.Split(capsStr, " ")
for _, cap := range caps {
userCaps.Caps[cap] = 1
}
}
email := columns[0]
if err := cmd.runRequest(email, userCaps); err != nil {
u.PrintlnErr(err.Error())
statusCode = 1
} else {
u.Println("Successfully set capabilities for "+email)
}
}
} else {
// Argument is an email address
email := args[0]
userCaps := &capsArgs { make(map[string]int) }
for _, cap := range args[1:] {
userCaps.Caps[cap] = 1
}
if err := cmd.runRequest(email, userCaps); err != nil {
u.PrintlnErr(err.Error())
statusCode = 1
} else {
u.Println("Successfully set capabilities for "+email)
}
}
return statusCode
}
func (cmd *SetCaps)runRequest(email string, userCaps *capsArgs) error {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().SetBody(userCaps).Put(host+"/users/"+email+"/caps")
if err != nil {
return errors.New("Error: "+err.Error())
}
if resp.IsSuccess() {
u.Println(resp)
return 0
return nil
} else {
u.PrintlnErr("Error: "+resp.Status()+": "+resp.String())
return 1
return errors.New("Error: "+resp.Status()+": "+resp.String())
}
}
......@@ -8,7 +8,7 @@ import (
const (
major = 1
minor = 4
bugfix = 1
bugfix = 2
)
type Version struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment