-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.tcl
More file actions
110 lines (96 loc) · 3.42 KB
/
init.tcl
File metadata and controls
110 lines (96 loc) · 3.42 KB
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
# Copyright (C) 2011-2014 Richard Alpe <rical@highwind.se>
#
# This file is part of 9pm.
#
# 9pm is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 9pm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package require yaml
package require cmdline
package provide 9pm::init 1.0
namespace eval ::9pm {
set root_path [file normalize [file dirname [info script]]]
}
# We can't call other procedures in the 9pm:: namespace here
# as this is running early.
namespace eval ::9pm::core {
proc parse_rc {} {
variable rc
# Only keys in this dict are allowed in the 9pm rc
lappend allowed "ssh_opts" "console_opts" "log_path"
if {[file exists "~/.9pm.rc"]} {
set rc_raw [parse_yaml "~/.9pm.rc"]
} else {
set rc_raw [parse_yaml "$::9pm::root_path/etc/9pm.rc"]
}
set rc {}
foreach {key val} $rc_raw {
set key [string tolower $key]
if {[lsearch -nocase $allowed $key] < 0} {
puts "ERROR:: Invalid 9pm.rc option \"$key\""
exit 2
}
dict set rc $key $val
}
}
proc parse_yaml {filename} {
# Check that configuration file exists
if {![file exists $filename]} {
puts "ERROR:: Can't parse configuration \"$filename\" (file not found)"
exit 2
}
set fp [open $filename r]
set data [read $fp]
close $fp
return [::yaml::yaml2dict $data]
}
parse_rc
# Parse command line
set options {
{b.arg "" "Runtime database path"}
{c.arg "" "Configuration file"}
{l.arg "" "Logging base path"}
{s.arg "" "Scratch dir path"}
{d "Output debug info and write exp_internal logfile"}
{dd "Output debug2 info"}
{t "Output TAP"}
}
if {[catch {array set cmdl [::cmdline::getoptions argv $options]}]} {
puts [::cmdline::usage $options "- usage:"]
exit 1
}
if {[::info exists ::env(NINEPM_DATABASE)] && $cmdl(b) == ""} {
array set cmdl [list "b" $::env(NINEPM_DATABASE)]
}
if {[::info exists ::env(NINEPM_CONFIG)] && $cmdl(c) == ""} {
array set cmdl [list "c" $::env(NINEPM_CONFIG)]
}
if {[::info exists ::env(NINEPM_LOG_PATH)] && $cmdl(l) == ""} {
array set cmdl [list "l" $::env(NINEPM_LOG_PATH)]
}
if {[::info exists ::env(NINEPM_SCRATCHDIR)] && $cmdl(s) == ""} {
array set cmdl [list "s" $::env(NINEPM_SCRATCHDIR)]
}
if {[::info exists ::env(NINEPM_DEBUG)]} {
array set cmdl [list "d" TRUE]
}
if {[::info exists ::env(NINEPM_DEBUG2)]} {
array set cmdl [list "dd" TRUE]
}
if {[::info exists ::env(NINEPM_TAP)]} {
array set cmdl [list "t" TRUE]
}
if {[dict exists $rc "log_path"] && $cmdl(l) == ""} {
array set cmdl [list "l" [dict get $rc "log_path"]]
}
}