-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.go
More file actions
62 lines (49 loc) · 1.14 KB
/
settings.go
File metadata and controls
62 lines (49 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tasked
import (
"github.com/dhamidi/tasked/planner"
"os"
"path/filepath"
)
type Settings struct {
DatabaseFile string
Context *planner.Context
}
var GlobalSettings = &Settings{}
func (s *Settings) GetDatabaseFile() string {
if s.DatabaseFile != "" {
return s.DatabaseFile
}
// Default to ~/.tasked/tasks.db
homeDir, err := os.UserHomeDir()
if err != nil {
return "tasks.db"
}
taskedDir := filepath.Join(homeDir, ".tasked")
if err := os.MkdirAll(taskedDir, 0755); err != nil {
return "tasks.db"
}
return filepath.Join(taskedDir, "tasks.db")
}
// GetOrCreateContext returns the current context, detecting it if not already initialized
func (s *Settings) GetOrCreateContext() (*planner.Context, error) {
if s.Context != nil {
return s.Context, nil
}
ctx, err := planner.DetectContext()
if err != nil {
return nil, err
}
s.Context = ctx
return ctx, nil
}
// GetContextPath returns the context path to use for plans
func (s *Settings) GetContextPath() string {
ctx, _ := s.GetOrCreateContext()
if ctx == nil {
return ""
}
if ctx.IsGitRepo && ctx.GitRoot != "" {
return ctx.GitRoot
}
return ctx.CurrentPath
}