paths.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package lifecycle
  2. import (
  3. "errors"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. var (
  12. AppName = "ollama app"
  13. CLIName = "ollama"
  14. AppDir = "/opt/Ollama"
  15. AppDataDir = "/opt/Ollama"
  16. // TODO - should there be a distinct log dir?
  17. UpdateStageDir = "/tmp"
  18. AppLogFile = "/tmp/ollama_app.log"
  19. ServerLogFile = "/tmp/ollama.log"
  20. UpgradeLogFile = "/tmp/ollama_update.log"
  21. Installer = "OllamaSetup.exe"
  22. )
  23. func init() {
  24. if runtime.GOOS == "windows" {
  25. AppName += ".exe"
  26. CLIName += ".exe"
  27. // Logs, configs, downloads go to LOCALAPPDATA
  28. localAppData := os.Getenv("LOCALAPPDATA")
  29. AppDataDir = filepath.Join(localAppData, "Ollama")
  30. UpdateStageDir = filepath.Join(AppDataDir, "updates")
  31. AppLogFile = filepath.Join(AppDataDir, "app.log")
  32. ServerLogFile = filepath.Join(AppDataDir, "server.log")
  33. UpgradeLogFile = filepath.Join(AppDataDir, "upgrade.log")
  34. // Executables are stored in APPDATA
  35. AppDir = filepath.Join(localAppData, "Programs", "Ollama")
  36. // Make sure we have PATH set correctly for any spawned children
  37. paths := strings.Split(os.Getenv("PATH"), ";")
  38. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  39. found := false
  40. for _, path := range paths {
  41. d, err := filepath.Abs(path)
  42. if err != nil {
  43. continue
  44. }
  45. if strings.EqualFold(AppDir, d) {
  46. found = true
  47. }
  48. }
  49. if !found {
  50. paths = append(paths, AppDir)
  51. pathVal := strings.Join(paths, ";")
  52. slog.Debug("setting PATH=" + pathVal)
  53. err := os.Setenv("PATH", pathVal)
  54. if err != nil {
  55. slog.Error(fmt.Sprintf("failed to update PATH: %s", err))
  56. }
  57. }
  58. // Make sure our logging dir exists
  59. _, err := os.Stat(AppDataDir)
  60. if errors.Is(err, os.ErrNotExist) {
  61. if err := os.MkdirAll(AppDataDir, 0o755); err != nil {
  62. slog.Error(fmt.Sprintf("create ollama dir %s: %v", AppDataDir, err))
  63. }
  64. }
  65. } else if runtime.GOOS == "darwin" {
  66. // TODO
  67. AppName += ".app"
  68. // } else if runtime.GOOS == "linux" {
  69. // TODO
  70. }
  71. }