From d6c27375216bebeee36e0727f3b9c569eceeb84d Mon Sep 17 00:00:00 2001 From: paul cannon Date: Fri, 18 Jan 2019 10:01:55 -0500 Subject: [PATCH] fix some email list parsing in certificates tool (#1095) this avoids getting an "userID cannot be empty" error when using an emails list that has a newline at the end. --- cmd/certificates/auth.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/certificates/auth.go b/cmd/certificates/auth.go index f7f993719..822c9c9c2 100644 --- a/cmd/certificates/auth.go +++ b/cmd/certificates/auth.go @@ -4,10 +4,10 @@ package main import ( + "bufio" "encoding/csv" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strconv" @@ -77,6 +77,22 @@ func init() { cfgstruct.Bind(authExportCmd.Flags(), &authExportCfg, cfgstruct.ConfDir(defaultConfDir)) } +func parseEmailsList(fileName, delimiter string) (emails []string, err error) { + file, err := os.Open(fileName) + if err != nil { + return nil, errs.Wrap(err) + } + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "#") { + continue + } + emails = append(emails, line) + } + return emails, file.Close() +} + func cmdCreateAuth(cmd *cobra.Command, args []string) error { count, err := strconv.Atoi(args[0]) if err != nil { @@ -94,11 +110,10 @@ func cmdCreateAuth(cmd *cobra.Command, args []string) error { } emails = args[1:] } else { - list, err := ioutil.ReadFile(authCreateCfg.EmailsPath) + emails, err = parseEmailsList(authCreateCfg.EmailsPath, authCreateCfg.Delimiter) if err != nil { return errs.Wrap(err) } - emails = strings.Split(string(list), authCreateCfg.Delimiter) } var incErrs utils.ErrorGroup @@ -122,7 +137,7 @@ func cmdInfoAuth(cmd *cobra.Command, args []string) error { return errs.New("Either use `--emails-path` or positional args, not both.") } emails = args - } else if len(args) == 0 || authExportCfg.All { + } else if len(args) == 0 || authInfoCfg.All { emails, err = authDB.UserIDs() if err != nil { return err @@ -130,11 +145,10 @@ func cmdInfoAuth(cmd *cobra.Command, args []string) error { } else if _, err := os.Stat(authInfoCfg.EmailsPath); err != nil { return errs.New("Emails path error: %s", err) } else { - list, err := ioutil.ReadFile(authInfoCfg.EmailsPath) + emails, err = parseEmailsList(authInfoCfg.EmailsPath, authInfoCfg.Delimiter) if err != nil { return errs.Wrap(err) } - emails = strings.Split(string(list), authInfoCfg.Delimiter) } var emailErrs, printErrs utils.ErrorGroup @@ -225,11 +239,10 @@ func cmdExportAuth(cmd *cobra.Command, args []string) error { return err } } else { - list, err := ioutil.ReadFile(authExportCfg.EmailsPath) + emails, err = parseEmailsList(authExportCfg.EmailsPath, authExportCfg.Delimiter) if err != nil { return errs.Wrap(err) } - emails = strings.Split(string(list), authExportCfg.Delimiter) } var (