forked from shimmeringbee/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directories.go
72 lines (58 loc) · 2.14 KB
/
directories.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
package main
import (
"context"
"flag"
"github.com/peterbourgon/ff/v3"
"github.com/shimmeringbee/logwrap"
"os"
"path/filepath"
)
const DefaultDirectoryPermissions = 0700
const DefaultFilePermissions = 0600
type Directories struct {
Config string
Data string
Log string
}
func enumerateDirectories(ctx context.Context, l logwrap.Logger) Directories {
fs := flag.NewFlagSet("controller", flag.ExitOnError)
defaultConfigDirectory, err := defaultDirectory("config")
if err != nil {
l.LogFatal(ctx, "Failed to construct default configuration directory.", logwrap.Err(err))
}
defaultDataDirectory, err := defaultDirectory("data")
if err != nil {
l.LogFatal(ctx, "Failed to construct default data directory.", logwrap.Err(err))
}
defaultLogDirectory, err := defaultDirectory("log")
if err != nil {
l.LogFatal(ctx, "Failed to construct default log directory.", logwrap.Err(err))
}
configDirectory := fs.String("config-directory", defaultConfigDirectory, "location of configuration files")
dataDirectory := fs.String("data-directory", defaultDataDirectory, "location of data files")
logDirectory := fs.String("log-directory", defaultLogDirectory, "location of log files")
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
l.LogFatal(ctx, "Failed to parse environment/command line arguments.", logwrap.Err(err))
}
if err := os.MkdirAll(*configDirectory, DefaultDirectoryPermissions); err != nil {
l.LogFatal(ctx, "Failed to initialise configuration directory.", logwrap.Err(err))
}
if err := os.MkdirAll(*dataDirectory, DefaultDirectoryPermissions); err != nil {
l.LogFatal(ctx, "Failed to initialise data directory.", logwrap.Err(err))
}
if err := os.MkdirAll(*logDirectory, DefaultDirectoryPermissions); err != nil {
l.LogFatal(ctx, "Failed to initialise log directory.", logwrap.Err(err))
}
return Directories{
Config: *configDirectory,
Data: *dataDirectory,
Log: *logDirectory,
}
}
func defaultDirectory(t string) (string, error) {
if configDir, err := os.UserConfigDir(); err != nil {
return "", err
} else {
return filepath.Join(configDir, "shimmeringbee", "controller", t), nil
}
}