diff --git a/libclient/globals/globals.go b/libclient/globals/globals.go deleted file mode 100644 index ac1739f538441982090aec9c0c493cf13299244a..0000000000000000000000000000000000000000 --- 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 3fb9745962b5ced0797782d361600a44c582f9a5..58597d197b19b37d4ef3ccf3c128b9ecb9fced08 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 58d336703797a9cd74729bbceaa21de7831ad7f0..ca974ead0b1b5fe50e6448f40ed10a35f2b6e057 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 44f53594d56fb7757af6e88ff17795cbc89ff1d0..7eb93f7f701948d329c5ab10d305f61d5ac61e1e 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 32d5c2d20b7e0fb64617b7eee045828a0f2fe8a9..0e0f7865c453d71b067c33c7e03b7f7f2dfbf154 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 48d3af174f988b445e99b116df808de54b5b6fea..7e1386331c8ac2d7c1d856dfb1eecfc3e291e73e 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 87c9edb16c0d9f94d6829837be9e1242b913fb1a..e9a8e007e411f4e9febf9621826ebae711c547c1 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 134b547f59b545dd41a4de7e33af8c4d2d872b5c..7225b2602233eb5ee65e456906a6ef778afa1762 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 f0d029d5728d3b9c6502119fe373a4e297729edb..804a2edf4714d6adddf3b0bd3f8fd4eb1b4fb1ab 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 bba08451744025b81579e9589e5d999b792e2314..699f27e948a12240c52da4ad9599c6a383a0132f 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 f9029982d2ff68c67b9052b42573b2695dddf1dd..f97ff701a46f325df8e49adc02b3dce16a76cba9 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 }