-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
54 lines (44 loc) · 1.33 KB
/
options.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
)
const (
DROPBOX_TOKEN = "DROPBOX_TOKEN"
LOCAL_DIRECTORY = "LOCAL_DIRECTORY"
DROPBOX_DIRECTORY = "DROPBOX_DIRECTORY"
)
// options are the runtime configurations which affect
// application behavior.
type options struct {
DropboxToken string
DropboxDirectory string
LocalDirectory string
}
// optsFromEnv loads the configuration options for the
// application from the environment or an error if the
// provided options are invalid.
func optsFromEnv() (*options, error) {
opts := &options{
DropboxToken: os.Getenv(DROPBOX_TOKEN),
DropboxDirectory: os.Getenv(DROPBOX_DIRECTORY),
}
if opts.DropboxToken == "" {
return nil, fmt.Errorf("dropbox token must be provided in the %s environment variable", DROPBOX_TOKEN)
}
if opts.DropboxDirectory == "" {
return nil, fmt.Errorf("dropbox directory must be provided in the %s environment variable", DROPBOX_DIRECTORY)
}
var err error
opts.LocalDirectory, err = filepath.Abs(os.Getenv(LOCAL_DIRECTORY))
if err != nil {
return nil, errors.Wrapf(err, "invalid directory in %s environment variable", LOCAL_DIRECTORY)
}
err = os.MkdirAll(opts.LocalDirectory, os.ModePerm)
if err != nil {
return nil, errors.Wrapf(err, "unable to create directory %s", opts.LocalDirectory)
}
return opts, nil
}