-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
104 lines (91 loc) · 3.34 KB
/
bootstrap.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
package config
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/getlantern/yaml"
"github.com/getlantern/flashlight/v7/common"
)
var (
name = ".packaged-lantern.yaml"
)
// BootstrapSettings provides access to configuration embedded directly in Lantern installation
// packages. On OSX, that means data embedded in the Lantern.app app bundle in
// Lantern.app/Contents/Resources/.lantern.yaml, while on Windows that means data embedded
// in AppData/Roaming/Lantern/.lantern.yaml. This allows customization embedded in the
// installer outside of the auto-updated binary that should only be used under special
// circumstances.
type BootstrapSettings struct {
StartupUrl string
}
// ReadBootstrapSettings reads packaged settings from pre-determined paths
// on the various OSes.
func ReadBootstrapSettings(configDir string) (*BootstrapSettings, error) {
_, yamlPath, err := bootstrapPath(name)
if err != nil {
return &BootstrapSettings{}, err
}
ps, er := readSettingsFromFile(yamlPath)
if er != nil {
// This is the local copy of our embedded ration file. This is necessary
// to ensure we remember the embedded ration across auto-updated
// binaries. We write to the local file system instead of to the package
// itself (app bundle on OSX, install directory on Windows) because
// we're not always sure we can write to that directory.
return readSettingsFromFile(filepath.Join(configDir, name))
}
return ps, nil
}
// ReadSettingsFromFile reads BootstrapSettings from the yaml file at the specified
// path.
func readSettingsFromFile(yamlPath string) (*BootstrapSettings, error) {
log.Debugf("Opening file at: %v", yamlPath)
data, err := ioutil.ReadFile(yamlPath)
if err != nil {
// This will happen whenever there's no packaged settings, which is often
log.Debugf("Error reading file %v", err)
return &BootstrapSettings{}, err
}
trimmed := strings.TrimSpace(string(data))
log.Debugf("Read bytes: %v", trimmed)
if trimmed == "" {
log.Debugf("Ignoring empty string")
return &BootstrapSettings{}, errors.New("empty string")
}
var s BootstrapSettings
err = yaml.Unmarshal([]byte(trimmed), &s)
if err != nil {
log.Errorf("Could not read yaml: %v", err)
return &BootstrapSettings{}, err
}
return &s, nil
}
func bootstrapPath(fileName string) (string, string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Errorf("Could not get current directory %v", err)
return "", "", err
}
var yamldir string
if common.Platform == "windows" {
yamldir = dir
} else if common.Platform == "darwin" {
// Code signing doesn't like this file in the current directory
// for whatever reason, so we grab it from the Resources/en.lproj
// directory in the app bundle. See:
// https://developer.apple.com/library/mac/technotes/tn2206/_index.html#//apple_ref/doc/uid/DTS40007919-CH1-TNTAG402
yamldir = dir + "/../Resources/en.lproj"
if _, err := ioutil.ReadDir(yamldir); err != nil {
// This likely means the user originally installed with an older version that didn't include en.lproj
// in the app bundle, so just look in the old location in Resources.
yamldir = dir + "/../Resources"
}
} else if common.Platform == "linux" {
yamldir = dir + "/../"
}
fullPath := filepath.Join(yamldir, fileName)
log.Debugf("Opening bootstrap file from: %v", fullPath)
return yamldir, fullPath, nil
}