promt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package internal
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "github.com/ente-io/cli/internal/api"
  7. "log"
  8. "os"
  9. "strings"
  10. "golang.org/x/term"
  11. )
  12. func GetSensitiveField(label string) (string, error) {
  13. fmt.Printf("%s: ", label)
  14. input, err := term.ReadPassword(int(os.Stdin.Fd()))
  15. if err != nil {
  16. return "", err
  17. }
  18. return string(input), nil
  19. }
  20. func GetUserInput(label string) (string, error) {
  21. fmt.Printf("%s: ", label)
  22. var input string
  23. reader := bufio.NewReader(os.Stdin)
  24. input, err := reader.ReadString('\n')
  25. //_, err := fmt.Scanln(&input)
  26. if err != nil {
  27. return "", err
  28. }
  29. input = strings.TrimSpace(input)
  30. if input == "" {
  31. return "", errors.New("input cannot be empty")
  32. }
  33. return input, nil
  34. }
  35. func GetAppType() api.App {
  36. for {
  37. app, err := GetUserInput("Enter app type (default: photos)")
  38. if err != nil {
  39. fmt.Printf("Use default app type: %s\n", api.AppPhotos)
  40. return api.AppPhotos
  41. }
  42. switch app {
  43. case "photos":
  44. return api.AppPhotos
  45. case "auth":
  46. return api.AppAuth
  47. case "locker":
  48. return api.AppLocker
  49. case "":
  50. return api.AppPhotos
  51. default:
  52. fmt.Println("invalid app type")
  53. continue
  54. }
  55. }
  56. }
  57. func GetCode(promptText string, length int) (string, error) {
  58. for {
  59. ott, err := GetUserInput(promptText)
  60. if err != nil {
  61. return "", err
  62. }
  63. if ott == "" {
  64. log.Fatal("no OTP entered")
  65. return "", errors.New("no OTP entered")
  66. }
  67. if ott == "c" {
  68. return "", errors.New("OTP entry cancelled")
  69. }
  70. if len(ott) != length {
  71. fmt.Printf("OTP must be %d digits", length)
  72. continue
  73. }
  74. return ott, nil
  75. }
  76. }
  77. func GetExportDir() string {
  78. for {
  79. exportDir, err := GetUserInput("Enter export directory")
  80. if err != nil {
  81. log.Printf("invalid export directory input: %s\n", err)
  82. return ""
  83. }
  84. if exportDir == "" {
  85. log.Printf("invalid export directory: %s\n", err)
  86. continue
  87. }
  88. exportDir, err = ResolvePath(exportDir)
  89. if err != nil {
  90. log.Printf("invalid export directory: %s\n", err)
  91. continue
  92. }
  93. _, err = ValidateDirForWrite(exportDir)
  94. if err != nil {
  95. log.Printf("invalid export directory: %s\n", err)
  96. continue
  97. }
  98. return exportDir
  99. }
  100. }
  101. func ValidateDirForWrite(dir string) (bool, error) {
  102. // Check if the path exists
  103. fileInfo, err := os.Stat(dir)
  104. if err != nil {
  105. if os.IsNotExist(err) {
  106. return false, fmt.Errorf("path does not exist: %s", dir)
  107. }
  108. return false, err
  109. }
  110. // Check if the path is a directory
  111. if !fileInfo.IsDir() {
  112. return false, fmt.Errorf("path is not a directory")
  113. }
  114. // Check for write permission
  115. // Check for write permission by creating a temp file
  116. tempFile, err := os.CreateTemp(dir, "write_test_")
  117. if err != nil {
  118. return false, fmt.Errorf("write permission denied: %v", err)
  119. }
  120. // Delete temp file
  121. defer os.Remove(tempFile.Name())
  122. if err != nil {
  123. return false, err
  124. }
  125. return true, nil
  126. }
  127. func ResolvePath(path string) (string, error) {
  128. if path[:2] != "~/" {
  129. return path, nil
  130. }
  131. home, err := os.UserHomeDir()
  132. if err != nil {
  133. return "", err
  134. }
  135. return home + path[1:], nil
  136. }