-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeychain.tcl
More file actions
executable file
·165 lines (143 loc) · 4.17 KB
/
Copy pathkeychain.tcl
File metadata and controls
executable file
·165 lines (143 loc) · 4.17 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /bin/tclsh
package require Tcl 8.6
set LOCAL_DIR [file dirname [file normalize [info script]]]
proc @comment {comment} {}
@comment {
TODO;
- check that the HOME variable is set;
otherwise throw error
- other error handling
- cleanup + documentation
}
# initialize empty list to store accounts by name
set accounts [list]
proc {git account} {username email host {sshkey ""} {note ""}} {
# initialize an account given a username, email, and host
# and add to global accounts list
# set a prefix for the account array so that no variable
# names get overwritten
set {account name} "git__$username"
# set up the account
upvar ${account name} account
set account(username) $username
set account(email) $email
set account(host) $host
# ssh key defaults to username unless otherwise specified
set account(sshkey) [expr {$sshkey eq "" ? $username : $sshkey}]
set account(note) $note
# append the account to the accounts list
lappend ::accounts ${account name}
}
proc {git account username} {acc {upvar_lvl 1}} {
# get an account username given an account array
upvar $upvar_lvl $acc account
return $account(username)
}
proc {print array} {a {prefix ""} {upvar_lvl 1}} {
# print an array
upvar $upvar_lvl $a arr
foreach key [array names arr] {
puts "$prefix$key: $arr($key)"
}
}
proc {print accounts} {} {
# todo: selective account print (i.e., don't print sshkey if
# standard name, don't print note if not available, print
# note at top of account info block if available
for {set i 0} {$i < [llength $::accounts]} {incr i} {
set account [lindex $::accounts $i]
puts "$i: [{git account username} $account 2]"
{print array} $account "\t" 2
}
}
# import accounts from accounts.conf file
source [file nativename "$LOCAL_DIR/accounts.conf"]
array set LOG_TAGS {
INFO {[ INFO ]}
ERROR {[ ERROR ]}
}
proc {write config} {id} {
# write ~/.ssh/config file
set accountArr [lindex $::accounts $id]
upvar 2 $accountArr account
set config "Host $account(host)
HostName $account(host)
User git
IdentityFile $::HOME/.ssh/$account(sshkey)
IdentitiesOnly yes"
set filename [file nativename "$::HOME/.ssh/$::SSHCONFIGFILE"]
set {config file} [open $filename w]
puts ${config file} $config
close ${config file}
}
proc {write gitconfig} {id} {
# write ~/.gitconfig file
puts "$::LOG_TAGS(INFO) set to account $id"
set accountArr [lindex $::accounts $id]
upvar $accountArr account
set gitconfig "\[user\]\n\tname = $account(username)\n\temail = $account(email)"
set filename [file nativename "$::HOME/.gitconfig"]
set {gitconfig file} [open $filename w]
puts ${gitconfig file} $gitconfig
close ${gitconfig file}
{write config} $id
puts "make sure the following line is in your $::HOME/.ssh/config file:"
puts "Include $::SSHCONFIGFILE"
}
proc {validate account selection} {selection} {
# determine if the account selection is
# valid
if {![string is integer $selection]} {
# return false if the selection
# is not an integer
return false
}
if {[lindex $::accounts $selection] eq ""} {
# return false if the selection
# does not reference an account
# index
return false
}
if {[string length $selection] == 0} {
# return false if the selection
# is an empty string
return false
}
return true
}
if {[lindex $argv 0] eq {list}} {
{print accounts}
}
if {[lindex $argv 0] eq {set}} {
set accID [lindex $argv 1]
if {[{validate account selection} $accID]} {
{write gitconfig} $accID
} else {
{print accounts}
puts "select account:"
gets stdin accID
if {$accID eq ""} {
puts "$LOG_TAGS(INFO) no selection made - exiting"
} else {
if {![{validate account selection} $accID]} {
puts "$LOG_TAGS(ERROR) invalid selection - please try again"
} else {
puts "$LOG_TAGS(INFO) selected account: $accID"
{print array} [lindex $::accounts $accID] "\t"
{write gitconfig} $accID
}
}
}
}
if {!$argc || [lindex $argv 0] eq {help}} {
puts "$argv0
a cli written in tcl to manage git accounts across
multiple platforms
commands:
help display this help text
list list all available accounts
set <index> list all available accounts and then set account by index
(you may optionally specify the account index to skip
listing all accounts)
"
}