store.go 2.15 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
8
9
10
package main

import (
	"io/ioutil"
	"os"
	"os/user"
	"path/filepath"
)

var (
11
12
	storeDefaultPlayer        = "" // Contains path created by init()
	storeDefaultSubscriptions = "" // Contains path created by init()
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
13
	storeDefaultScripts       = "" // Contains path created by init()
Jonny Schäfer's avatar
Jonny Schäfer committed
14
15
)

16
17
18
19
// init checks if the configuration directory exist and creates
// it if necessary. It also initializes the global variables
// storeDefaultPlayer = ~/.config/tubus/subscriptions
// storeDefaultSubscriptions = ~/.config/tubus/player
Jonny Schäfer's avatar
Jonny Schäfer committed
20
21
22
23
24
func init() {
	user, err := user.Current()
	errorCare(err)
	home := user.HomeDir

25
	conf := filepath.Join(home, ".config/tubus")
26
27
28

	storeDefaultScripts = filepath.Join(conf, "scripts")
	exists, err := storeFileExists(storeDefaultScripts)
Jonny Schäfer's avatar
Jonny Schäfer committed
29
30
	errorCare(err)
	if !exists {
31
		errorCare(storeCreateDirectory(storeDefaultScripts))
Jonny Schäfer's avatar
Jonny Schäfer committed
32
33
34
35
	}

	storeDefaultSubscriptions = filepath.Join(conf, "subscriptions")
	exists, err = storeFileExists(storeDefaultSubscriptions)
36
	errorCare(err)
Jonny Schäfer's avatar
Jonny Schäfer committed
37
38
39
40
41
42
	if !exists {
		errorCare(storeCreateFile(storeDefaultSubscriptions))
	}

	storeDefaultPlayer = filepath.Join(conf, "player")
	exists, err = storeFileExists(storeDefaultPlayer)
43
	errorCare(err)
Jonny Schäfer's avatar
Jonny Schäfer committed
44
45
46
47
48
	if !exists {
		errorCare(storeCreateFile(storeDefaultPlayer))
	}
}

49
// storeCreateDirectory creates an empty file.
Jonny Schäfer's avatar
Jonny Schäfer committed
50
51
52
53
54
55
func storeCreateFile(path string) error {
	file, err := os.Create(path)
	file.Close()
	return err
}

56
57
// storeCreateDirectory creates the given new directory and parent
// directories if needed.
Jonny Schäfer's avatar
Jonny Schäfer committed
58
func storeCreateDirectory(path string) error {
59
	return os.MkdirAll(path, os.ModePerm)
Jonny Schäfer's avatar
Jonny Schäfer committed
60
61
62
63
64
65
66
}

// storeWriteFile writes a file with the given content
func storeWriteFile(file, content string) error {
	return ioutil.WriteFile(file, []byte(content), os.ModePerm)
}

67
// storeReadFile returns the content of the given file.
Jonny Schäfer's avatar
Jonny Schäfer committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
func storeReadFile(file string) (content string, err error) {
	cnt, err := ioutil.ReadFile(file)
	content = string(cnt)
	return
}

// storeFileExists returns true if the given directory or file exists.
func storeFileExists(file string) (bool, error) {
	_, err := os.Stat(file)
	if err != nil {
		if os.IsNotExist(err) {
			return false, nil
		} else {
			return false, err
		}
	}
	return true, nil
}