summaryrefslogtreecommitdiff
path: root/paths/paths.go
blob: b714a23e6199c0b369d67f3d5d3e42ff5003e098 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
package paths

import (
	"github.com/adrg/xdg"
	"os"
	"path"
	"path/filepath"
	"strings"
)

var (
	Ifs          string
	ConfigDir    = Empty()
	CacheDir     = Empty()
	ContextsFile = Empty()
	HomeDir      = Empty()
	System       = Empty()
	This         = "pik"
)

var Paths = []*Path{
	ConfigDir,
	CacheDir,
	ContextsFile,
	HomeDir,
	System,
}

var DirectoriesToMake = []*Path{
	ConfigDir,
	CacheDir,
}

type paths struct {
	Initialized bool
}

var Component = &paths{
	Initialized: false,
}

func (p *paths) Init() error {

	// if we're asked to initialize for a second time,
	// probably some environment has changed
	if p.Initialized {
		xdg.Reload()
	}

	HomeDir.Set(xdg.Home)
	CacheDir.Set(filepath.Join(xdg.CacheHome, This))
	ConfigDir.Set(filepath.Join(xdg.ConfigHome, This))
	ContextsFile.Set(path.Join(CacheDir.String(), "contexts"))
	System.Set(path.Join(ConfigDir.String()))

	Ifs = os.Getenv("IFS")

	for _, d := range DirectoriesToMake {
		err := os.MkdirAll(d.String(), 0700)
		if err != nil {
			return err
		}
	}

	if Ifs == "" {
		Ifs = "\n"
	}
	p.Initialized = true
	return nil
}

func ReplaceHome(input string) string {
	return strings.Replace(input, HomeDir.String(), "~", 1)
}