-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
114 lines (96 loc) · 2.36 KB
/
main.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/ericm/yup/config"
"github.com/ericm/yup/search"
"github.com/ericm/yup/cmd"
"github.com/ericm/yup/output"
)
const configFileName = "config.json"
// Global variables
var (
configDir string
cacheDir string
configFile string
configSet *config.Config
)
// Entrypoint to the program.
// Runs intitialization and calls other packages.
func main() {
if os.Geteuid() == 0 {
output.PrintErr("No need to run yup as sudo/root")
os.Exit(1)
}
if runtime.GOOS != "linux" {
fmt.Fprintln(os.Stderr, "Requires linux I'm afraid")
os.Exit(1)
}
exitError(search.Init())
exitError(paths())
exitError(makePaths())
exitError(config.ReadConfigFile(cmd.Version))
exitError(cmd.Execute())
}
// Exits the program upon a core function returning an error.
func exitError(err error) {
if err != nil {
if str := err.Error(); str != "" {
fmt.Fprintln(os.Stderr, output.Errorf(str))
os.Exit(1)
}
}
}
// Sets global path variables
func paths() error {
configSet = &config.Config{}
configDir = os.Getenv("HOME")
if len(configDir) == 0 {
return fmt.Errorf("HOME environment variable unset")
}
cacheDir = filepath.Join(configDir, ".cache/yup")
configDir = filepath.Join(configDir, ".config/yup")
configFile = filepath.Join(configDir, configFileName)
return nil
}
// Checks and creates the directories and file required by yup.
func makePaths() error {
// Set config
configSet.ConfigDir = configDir
configSet.ConfigFile = configFile
// Config
if _, err := os.Stat(configDir); os.IsNotExist(err) {
if err = os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("Failed to create config directory: %s", err)
}
} else if err != nil {
return err
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// Create file
file, err := os.OpenFile(configFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
if err != nil {
}
if err := config.InitConfig(file, cmd.Version); err != nil {
return err
}
if err != nil {
return err
}
} else if err != nil {
return err
}
// Cache
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err = os.MkdirAll(cacheDir, 0755); err != nil {
return fmt.Errorf("Failed to create cache directory: %s", err)
}
} else if err != nil {
return err
}
configSet.CacheDir = cacheDir
config.SetConfig(configSet)
return nil
}