summaryrefslogtreecommitdiff
path: root/paths/paths.go
blob: cc0c878567ced7638de7446d3737593341b99928 (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
package paths

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

var (
	Ifs    string
	Config string
	Cache  string
	This   string
	Home   string
)

type paths struct {
	Initialized bool
}

var Paths = &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()
	}

	Home = xdg.Home
	This = "pik"
	Cache = filepath.Join(xdg.CacheHome, This)
	Config = filepath.Join(xdg.ConfigHome, This)
	Ifs = os.Getenv("IFS")

	err := os.MkdirAll(Cache, 0700)
	if err != nil {
		return err
	}
	err = os.MkdirAll(Config, 0700)
	if err != nil {
		return err
	}
	if Ifs == "" {
		Ifs = "\n"
	}
	p.Initialized = true
	return nil
}

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