From b99ddb3d2c097be34dfbb18354cf342cf462ecbd Mon Sep 17 00:00:00 2001 From: Florent Gluck <florent.gluck@hesge.ch> Date: Tue, 25 Mar 2025 21:53:42 +0100 Subject: [PATCH] simplified nexush/libclient code (csv reader) --- libclient/globals/globals.go | 7 ------ libclient/utils/csv.go | 42 ++++++++++++++++++++++++++++------- nexush/cmdUser/userCreate.go | 11 +++------ nexush/cmdUser/userDel.go | 13 ++++------- nexush/cmdUser/userSetCaps.go | 13 ++++------- nexush/cmdVM/vmAddAccess.go | 12 +++------- nexush/cmdVM/vmCreds2csv.go | 4 ++-- nexush/cmdVM/vmDelAccess.go | 13 ++++------- nexush/go.mod | 2 ++ nexush/go.sum | 2 -- nexush/nexush.go | 9 ++++---- 11 files changed, 61 insertions(+), 67 deletions(-) delete mode 100644 libclient/globals/globals.go diff --git a/libclient/globals/globals.go b/libclient/globals/globals.go deleted file mode 100644 index ac1739f5..00000000 --- a/libclient/globals/globals.go +++ /dev/null @@ -1,7 +0,0 @@ -package globals - -const ( - ENV_NEXUS_SERVER = "NEXUS_SERVER" - ENV_NEXUS_TOKEN = "NEXUS_TOKEN" - CsvFieldSeparator = ';' -) diff --git a/libclient/utils/csv.go b/libclient/utils/csv.go index 3fb97459..58597d19 100644 --- a/libclient/utils/csv.go +++ b/libclient/utils/csv.go @@ -6,21 +6,47 @@ import ( "io" "os" "strconv" - - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" ) +const CsvFieldSeparator = ';' + +// Simple wrapper over csv.Reader +type CsvReader struct { + file *os.File + reader *csv.Reader +} + +func NewCsvReader(file string) (*CsvReader, error) { + csvReader := &CsvReader{} + var err error + csvReader.file, err = os.Open(file) + if err != nil { + return nil, err + } + + csvReader.reader = csv.NewReader(csvReader.file) + csvReader.reader.Comma = CsvFieldSeparator // specify the column separator + csvReader.reader.Comment = '#' // lines starting with this symbol are to be ignored + + return csvReader, nil +} + +func (csvr *CsvReader) Read() (record []string, err error) { + return csvr.reader.Read() +} + +func (csvr *CsvReader) Close() { + csvr.file.Close() +} + // Returns the specified column, as a slice of strings, from a CSV file. // Columns are numbered starting at 0. func ReadCSVColumn(csvFile string, column int) ([]string, error) { - file, err := os.Open(csvFile) + reader, err := NewCsvReader(csvFile) if err != nil { - return nil, errors.New("Error: " + err.Error()) + return nil, err } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() var entries []string for { diff --git a/nexush/cmdUser/userCreate.go b/nexush/cmdUser/userCreate.go index 58d33670..ca974ead 100644 --- a/nexush/cmdUser/userCreate.go +++ b/nexush/cmdUser/userCreate.go @@ -1,14 +1,11 @@ package cmdUser import ( - "encoding/csv" "io" - "os" "strings" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/caps" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/params" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" @@ -88,15 +85,13 @@ func (cmd *Add) Run(client *nc.NexusClient, args []string) int { } else if argc == 1 { // Detected syntax: file.csv csvFile := args[0] - file, err := os.Open(csvFile) + reader, err := utils.NewCsvReader(csvFile) if err != nil { u.PrintlnErr(err) return 1 } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() + line := 0 for { line += 1 diff --git a/nexush/cmdUser/userDel.go b/nexush/cmdUser/userDel.go index 44f53594..7eb93f7f 100644 --- a/nexush/cmdUser/userDel.go +++ b/nexush/cmdUser/userDel.go @@ -1,11 +1,8 @@ package cmdUser import ( - "encoding/csv" "io" - "os" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" @@ -55,15 +52,13 @@ func (cmd *Del) Run(client *nc.NexusClient, args []string) int { return 1 } - file, err := os.Open(csvFile) + reader, err := utils.NewCsvReader(csvFile) if err != nil { - u.PrintlnErr("Error: " + err.Error()) + u.PrintlnErr(err) return 1 } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() + line := 0 for { line += 1 diff --git a/nexush/cmdUser/userSetCaps.go b/nexush/cmdUser/userSetCaps.go index 32d5c2d2..0e0f7865 100644 --- a/nexush/cmdUser/userSetCaps.go +++ b/nexush/cmdUser/userSetCaps.go @@ -1,14 +1,11 @@ package cmdUser import ( - "encoding/csv" "io" - "os" "strings" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/caps" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/params" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" @@ -78,15 +75,13 @@ func (cmd *SetCaps) Run(client *nc.NexusClient, args []string) int { return 1 } - file, err := os.Open(csvFile) + reader, err := utils.NewCsvReader(csvFile) if err != nil { - u.PrintlnErr("Error: " + err.Error()) + u.PrintlnErr(err) return 1 } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() + line := 0 for { line += 1 diff --git a/nexush/cmdVM/vmAddAccess.go b/nexush/cmdVM/vmAddAccess.go index 48d3af17..7e138633 100644 --- a/nexush/cmdVM/vmAddAccess.go +++ b/nexush/cmdVM/vmAddAccess.go @@ -1,16 +1,13 @@ package cmdVM import ( - "encoding/csv" "errors" "io" "net/mail" - "os" "strings" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/caps" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/params" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" @@ -76,15 +73,12 @@ func (cmd *AddAccess) Run(client *nc.NexusClient, args []string) int { // Single argument and it's a CSV file csvFile := args[0] - file, err := os.Open(csvFile) + reader, err := utils.NewCsvReader(csvFile) if err != nil { - u.PrintlnErr("Error: " + err.Error()) + u.PrintlnErr(err) return 1 } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() line := 0 for { diff --git a/nexush/cmdVM/vmCreds2csv.go b/nexush/cmdVM/vmCreds2csv.go index 87c9edb1..e9a8e007 100644 --- a/nexush/cmdVM/vmCreds2csv.go +++ b/nexush/cmdVM/vmCreds2csv.go @@ -4,8 +4,8 @@ import ( "fmt" "os" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" + "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" ) @@ -64,7 +64,7 @@ func (cmd *Creds2csv) Run(client *nc.NexusClient, args []string) int { defer f.Close() for _, creds := range credsList { - sep := string(g.CsvFieldSeparator) + sep := string(utils.CsvFieldSeparator) _, err := fmt.Fprintln(f, creds.ID.String()+sep+creds.Name+sep+creds.Pwd) if err != nil { u.PrintlnErr("Error: " + err.Error()) diff --git a/nexush/cmdVM/vmDelAccess.go b/nexush/cmdVM/vmDelAccess.go index 134b547f..7225b260 100644 --- a/nexush/cmdVM/vmDelAccess.go +++ b/nexush/cmdVM/vmDelAccess.go @@ -1,13 +1,10 @@ package cmdVM import ( - "encoding/csv" "errors" "io" "net/mail" - "os" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils" u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/utils" @@ -55,15 +52,13 @@ func (cmd *DelAccess) Run(client *nc.NexusClient, args []string) int { // Single argument and it's a CSV file csvFile := args[0] - file, err := os.Open(csvFile) + reader, err := utils.NewCsvReader(csvFile) if err != nil { - u.PrintlnErr("Error: " + err.Error()) + u.PrintlnErr(err) return 1 } - defer file.Close() - reader := csv.NewReader(file) - reader.Comma = g.CsvFieldSeparator // specify the column separator - reader.Comment = '#' // lines starting with this symbol are to be ignored + defer reader.Close() + line := 0 for { line += 1 diff --git a/nexush/go.mod b/nexush/go.mod index f0d029d5..804a2edf 100644 --- a/nexush/go.mod +++ b/nexush/go.mod @@ -2,6 +2,8 @@ module gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush go 1.22.2 +replace gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient => ../libclient + require ( gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common v0.0.0-20250320135605-9ac7ec3df57a gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient v0.0.0-20250320135605-9ac7ec3df57a diff --git a/nexush/go.sum b/nexush/go.sum index bba08451..699f27e9 100644 --- a/nexush/go.sum +++ b/nexush/go.sum @@ -1,7 +1,5 @@ gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common v0.0.0-20250320135605-9ac7ec3df57a h1:D4M2OnN+9vUALGPo6yNyWmcpvgGIzo/2HJj8SA4bGls= gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common v0.0.0-20250320135605-9ac7ec3df57a/go.mod h1:HzVsiYhPk7BhhvspjLchF3HlY1z+qIF89jG6+Hs8m3c= -gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient v0.0.0-20250325145622-5e5739fb0b5d h1:FooD8RWXNz2Y2fsVRm/tMjcGo0J9QWuiH30YP3uIhR8= -gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient v0.0.0-20250325145622-5e5739fb0b5d/go.mod h1:FTufL1WbpOEXB509PnsxzFFrYBx2RRSidxAqdDYZgGM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= diff --git a/nexush/nexush.go b/nexush/nexush.go index f9029982..f97ff701 100644 --- a/nexush/nexush.go +++ b/nexush/nexush.go @@ -11,7 +11,6 @@ import ( "syscall" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/buildversion" - g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals" nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/version" "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/nexush/cmd" @@ -26,6 +25,8 @@ import ( "golang.org/x/term" ) +const env_nexus_server = "NEXUS_SERVER" + var ( //go:embed build/server nexus_server string @@ -104,12 +105,12 @@ func run() int { return 1 } - serverEnvVar, found := os.LookupEnv(g.ENV_NEXUS_SERVER) + serverEnvVar, found := os.LookupEnv(env_nexus_server) if !found { serverEnvVar = nexus_server - // u.PrintlnErr("Environment variable \""+g.ENV_NEXUS_SERVER+"\" must be set!") + // u.PrintlnErr("Environment variable \""+env_nexus_server+"\" must be set!") // u.PrintlnErr("It defines the nexus server to connect to along the port number.") - // u.PrintlnErr("Example: export "+g.ENV_NEXUS_SERVER+"=192.168.1.42:1077") + // u.PrintlnErr("Example: export "+env_nexus_server+"=192.168.1.42:1077") // return 1 } -- GitLab