/pkg/cli/utils/utils.go

https://github.com/dnote/dnote · Go · 48 lines · 20 code · 8 blank · 20 comment · 4 complexity · b5c9e4d5b4acaef658f066f00911c30d MD5 · raw file

  1. /* Copyright (C) 2019, 2020 Monomax Software Pty Ltd
  2. *
  3. * This file is part of Dnote.
  4. *
  5. * Dnote is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Dnote is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Dnote. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. package utils
  19. import (
  20. "regexp"
  21. "github.com/google/uuid"
  22. "github.com/pkg/errors"
  23. )
  24. // GenerateUUID returns a uuid v4 in string
  25. func GenerateUUID() (string, error) {
  26. u, err := uuid.NewRandom()
  27. if err != nil {
  28. return "", errors.Wrap(err, "generating uuid")
  29. }
  30. return u.String(), nil
  31. }
  32. // regexNumber is a regex that matches a string that looks like an integer
  33. var regexNumber = regexp.MustCompile(`^\d+$`)
  34. // IsNumber checks if the given string is in the form of a number
  35. func IsNumber(s string) bool {
  36. if s == "" {
  37. return false
  38. }
  39. return regexNumber.MatchString(s)
  40. }