forked from mit-dci/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitinit.go
148 lines (128 loc) · 4.38 KB
/
litinit.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/jessevdk/go-flags"
"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/lit/logging"
)
// createDefaultConfigFile creates a config file -- only call this if the
// config file isn't already there
func createDefaultConfigFile(destinationPath string) error {
dest, err := os.OpenFile(filepath.Join(destinationPath, defaultConfigFilename),
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer dest.Close()
writer := bufio.NewWriter(dest)
defaultArgs := []byte("tn3=1")
_, err = writer.Write(defaultArgs)
if err != nil {
return err
}
writer.Flush()
return nil
}
// litSetup performs most of the setup when lit is run, such as setting
// configuration variables, reading in key data, reading and creating files if
// they're not yet there. It takes in a config, and returns a key.
// (maybe add the key to the config?
func litSetup(conf *litConfig) *[32]byte {
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified. Any errors aside from the
// help message error can be ignored here since they will be caught by
// the final parse below.
// usageMessage := fmt.Sprintf("Use %s -h to show usage", "./lit")
preParser := newConfigParser(conf, flags.HelpFlag)
_, err := preParser.ParseArgs(os.Args)
if err != nil {
logging.Fatal(err)
}
// Load config from file and parse
parser := newConfigParser(conf, flags.Default)
// set default log level here
logging.SetLogLevel(defaultLogLevel)
// create home directory
_, err = os.Stat(conf.LitHomeDir)
if err != nil {
logging.Errorf("Error while creating a directory")
}
if os.IsNotExist(err) {
// first time the guy is running lit, lets set tn3 to true
os.Mkdir(conf.LitHomeDir, 0700)
logging.Infof("Creating a new config file")
err := createDefaultConfigFile(conf.LitHomeDir) // Source of error
if err != nil {
fmt.Printf("Error creating a default config file: %v", conf.LitHomeDir)
logging.Fatal(err)
}
}
if _, err := os.Stat(filepath.Join(filepath.Join(conf.LitHomeDir), "lit.conf")); os.IsNotExist(err) {
// if there is no config file found over at the directory, create one
if err != nil {
fmt.Println(err)
}
logging.Infof("Creating a new config file")
err := createDefaultConfigFile(filepath.Join(conf.LitHomeDir)) // Source of error
if err != nil {
logging.Fatal(err)
}
}
conf.ConfigFile = filepath.Join(filepath.Join(conf.LitHomeDir), "lit.conf")
// lets parse the config file provided, if any
err = flags.NewIniParser(parser).ParseFile(conf.ConfigFile)
if err != nil {
_, ok := err.(*os.PathError)
if !ok {
logging.Fatal(err)
}
}
// Parse command line options again to ensure they take precedence.
_, err = parser.ParseArgs(os.Args) // returns invalid flags
if err != nil {
logging.Fatal(err)
}
logFilePath := filepath.Join(conf.LitHomeDir, "lit.log")
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
logging.SetLogFile(logFile)
// Log Levels:
// 5: DebugLevel prints Panics, Fatals, Errors, Warnings, Infos and Debugs
// 4: InfoLevel prints Panics, Fatals, Errors, Warnings and Info
// 3: WarnLevel prints Panics, Fatals, Errors and Warnings
// 2: ErrorLevel prints Panics, Fatals and Errors
// 1: FatalLevel prints Panics, Fatals
// 0: PanicLevel prints Panics
// Default is level 3
// Code for tagging logs:
// Debug -> Useful debugging information
// Info -> Something noteworthy happened
// Warn -> You should probably take a look at this
// Error -> Something failed but I'm not quitting
// Fatal -> Bye
// TODO ... what's this do?
defer logFile.Close()
logLevel := -1
if len(conf.LogLevel) == 1 { // -v
logLevel = 1
} else if len(conf.LogLevel) == 2 { // -vv
logLevel = 2
} else if len(conf.LogLevel) >= 3 {
logLevel = 3
}
logging.SetLogLevel(logLevel)
// Allow node with no linked wallets, for testing.
// TODO Should update tests and disallow nodes without wallets later.
// Keys: the litNode, and wallits, all get 32 byte keys.
// Right now though, they all get the *same* key. For lit as a single binary
// now, all using the same key makes sense; could split up later.
keyFilePath := filepath.Join(conf.LitHomeDir, defaultKeyFileName)
// read key file (generate if not found)
key, err := lnutil.ReadKeyFile(keyFilePath)
if err != nil {
logging.Fatal(err)
}
return key
}