forked from mit-dci/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
litinit.go
134 lines (113 loc) · 3.81 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
package main
import (
"bufio"
"io"
"log"
"os"
"path/filepath"
"github.com/jessevdk/go-flags"
"github.com/mit-dci/lit/lnutil"
)
// 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 *config) *[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")
preconf := *conf
preParser := newConfigParser(&preconf, flags.HelpFlag)
_, err := preParser.ParseArgs(os.Args)
if err != nil {
log.Fatal(err)
}
// Load config from file and parse
parser := newConfigParser(conf, flags.Default)
// create home directory
_, err = os.Stat(preconf.LitHomeDir)
if err != nil {
log.Println("Error while creating a directory")
}
if os.IsNotExist(err) {
// first time the guy is running lit, lets set tn3 to true
os.Mkdir(preconf.LitHomeDir, 0700)
log.Println("Creating a new config file")
err := createDefaultConfigFile(preconf.LitHomeDir) // Source of error
if err != nil {
log.Printf("Error creating a default config file: %v", preconf.LitHomeDir)
log.Fatal(err)
}
}
if _, err := os.Stat(filepath.Join(filepath.Join(preconf.LitHomeDir), "lit.conf")); os.IsNotExist(err) {
// if there is no config file found over at the directory, create one
if err != nil {
log.Println(err)
}
log.Println("Creating a new config file")
err := createDefaultConfigFile(filepath.Join(preconf.LitHomeDir)) // Source of error
if err != nil {
log.Fatal(err)
}
}
preconf.ConfigFile = filepath.Join(filepath.Join(preconf.LitHomeDir), "lit.conf")
// lets parse the config file provided, if any
err = flags.NewIniParser(parser).ParseFile(preconf.ConfigFile)
if err != nil {
_, ok := err.(*os.PathError)
if !ok {
log.Fatal(err)
}
}
// Parse command line options again to ensure they take precedence.
_, err = parser.ParseArgs(os.Args) // returns invalid flags
if err != nil {
log.Fatal(err)
}
logFilePath := filepath.Join(conf.LitHomeDir, "lit.log")
logfile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// TODO ... what's this do?
defer logfile.Close()
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
if conf.Verbose {
logOutput := io.MultiWriter(os.Stdout, logfile)
log.SetOutput(logOutput)
} else {
log.SetOutput(logfile)
}
// Allow node with no linked wallets, for testing.
// TODO Should update tests and disallow nodes without wallets later.
// if conf.Tn3host == "" && conf.Lt4host == "" && conf.Reghost == "" {
// log.Fatal("error: no network specified; use -tn3, -reg, -lt4")
// }
// 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 {
log.Fatal(err)
}
return key
}