utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package v2beta1
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "github.com/fatih/color"
  6. )
  7. type MessageLevel string
  8. const (
  9. Warning MessageLevel = "WARN"
  10. Error MessageLevel = "ERR"
  11. Success MessageLevel = "OK"
  12. Info MessageLevel = "INFO"
  13. )
  14. func composePreviewMessage(msg string, level MessageLevel) string {
  15. return fmt.Sprintf("[porter.yaml v2beta1][%s] -- %s", level, msg)
  16. }
  17. func printWarningMessage(msg string) {
  18. color.New(color.FgYellow).Printf(fmt.Sprintf("%s\n", composePreviewMessage(msg, Warning)))
  19. }
  20. func printErrorMessage(msg string) {
  21. color.New(color.FgRed).Printf(fmt.Sprintf("%s\n", composePreviewMessage(msg, Error)))
  22. }
  23. func printSuccessMessage(msg string) {
  24. color.New(color.FgGreen).Printf(fmt.Sprintf("%s\n", composePreviewMessage(msg, Success)))
  25. }
  26. func printInfoMessage(msg string) {
  27. color.New(color.FgBlue).Printf(fmt.Sprintf("%s\n", composePreviewMessage(msg, Info)))
  28. }
  29. func booleanptr(b bool) *bool {
  30. copy := b
  31. return &copy
  32. }
  33. func stringptr(s string) *string {
  34. copy := s
  35. return &copy
  36. }
  37. func randomString(length uint, charset string) string {
  38. ll := len(charset)
  39. b := make([]byte, length)
  40. rand.Read(b) // generates len(b) random bytes
  41. for i := uint(0); i < length; i++ {
  42. b[i] = charset[int(b[i])%ll]
  43. }
  44. return string(b)
  45. }