main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/ente-io/cli/cmd"
  5. "github.com/ente-io/cli/internal"
  6. "github.com/ente-io/cli/internal/api"
  7. "github.com/ente-io/cli/pkg"
  8. "github.com/ente-io/cli/pkg/secrets"
  9. "github.com/ente-io/cli/utils/constants"
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. )
  15. func main() {
  16. cliDBPath, err := GetCLIConfigPath()
  17. if secrets.IsRunningInContainer() {
  18. cliDBPath = constants.CliDataPath
  19. _, err := internal.ValidateDirForWrite(cliDBPath)
  20. if err != nil {
  21. log.Fatalf("Please mount a volume to %s to persist cli data\n%v\n", cliDBPath, err)
  22. }
  23. }
  24. if err != nil {
  25. log.Fatalf("Could not create cli config path\n%v\n", err)
  26. }
  27. newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath)
  28. if !strings.HasPrefix(cliDBPath, "/") {
  29. oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath)
  30. if _, err := os.Stat(oldCliPath); err == nil {
  31. log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliPath)
  32. if err := os.Rename(oldCliPath, newCliPath); err != nil {
  33. log.Fatalf("Could not rename old cli db\n%v\n", err)
  34. }
  35. }
  36. }
  37. db, err := pkg.GetDB(newCliPath)
  38. if err != nil {
  39. if strings.Contains(err.Error(), "timeout") {
  40. log.Fatalf("Please close all other instances of the cli and try again\n%v\n", err)
  41. } else {
  42. panic(err)
  43. }
  44. }
  45. ctrl := pkg.ClICtrl{
  46. Client: api.NewClient(api.Params{
  47. Debug: false,
  48. //Host: "http://localhost:8080",
  49. }),
  50. DB: db,
  51. KeyHolder: secrets.NewKeyHolder(secrets.GetOrCreateClISecret()),
  52. }
  53. err = ctrl.Init()
  54. if err != nil {
  55. panic(err)
  56. }
  57. defer func() {
  58. if err := db.Close(); err != nil {
  59. panic(err)
  60. }
  61. }()
  62. cmd.Execute(&ctrl)
  63. }
  64. // GetCLIConfigPath returns the path to the .ente-cli folder and creates it if it doesn't exist.
  65. func GetCLIConfigPath() (string, error) {
  66. if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" {
  67. return os.Getenv("ENTE_CLI_CONFIG_PATH"), nil
  68. }
  69. // Get the user's home directory
  70. homeDir, err := os.UserHomeDir()
  71. if err != nil {
  72. return "", err
  73. }
  74. cliDBPath := filepath.Join(homeDir, ".ente")
  75. // Check if the folder already exists, if not, create it
  76. if _, err := os.Stat(cliDBPath); os.IsNotExist(err) {
  77. err := os.MkdirAll(cliDBPath, 0755)
  78. if err != nil {
  79. return "", err
  80. }
  81. }
  82. return cliDBPath, nil
  83. }