forked from bmatsuo/gonew
/
config.go
187 lines (182 loc) · 5.04 KB
/
config.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2011, Bryan Matsuo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
/*
* File: config.go
* Author: Bryan Matsuo [bmatsuo@soe.ucsc.edu]
* Created: Sat Jul 2 23:09:50 PDT 2011
*/
import (
"os"
"fmt"
"log"
"bytes"
"bufio"
"path/filepath"
//"goconf.googlecode.com/hg" // This does not work for some reason.
//"conf"
"github.com/kless/goconfig/config"
)
var ConfigFilename = filepath.Join(os.Getenv("HOME"), ".gonewrc")
type GonewConfig struct {
MakeTest bool
Name string
Email string
HostUser string
AltRoot string
License LicenseType
Repo RepoType
Host RepoHost
}
var AppConfig GonewConfig = GonewConfig{
true, "", "", "", "", NilLicenseType, NilRepoType, NilRepoHost}
func ReadConfig() os.Error {
conf, err := config.ReadDefault(ConfigFilename)
if err != nil {
return err
}
var (
repostr string
hoststr string
license string
)
AppConfig.Name, err = conf.String("variables", "name")
AppConfig.Email, err = conf.String("variables", "email")
AppConfig.HostUser, err = conf.String("general", "hostuser")
AppConfig.AltRoot, err = conf.String("general", "templates")
license, err = conf.String("general", "license")
switch license {
case "":
AppConfig.License = NilLicenseType
case "newbsd":
AppConfig.License = NewBSD
}
repostr, err = conf.String("general", "repo")
switch repostr {
case "":
AppConfig.Repo = NilRepoType
case "git":
AppConfig.Repo = GitType
//case "mercurial":
//...
default:
AppConfig.Repo = NilRepoType
}
hoststr, err = conf.String("general", "host")
switch hoststr {
case "":
AppConfig.Host = NilRepoHost
case "github":
AppConfig.Host = GitHubHost
AppConfig.Repo = GitType
//case "googlecode":
//...
default:
AppConfig.Host = NilRepoHost
}
return nil
}
func TouchConfig() os.Error {
stat, err := os.Stat(ConfigFilename)
var patherr *os.PathError
switch err.(type) {
case nil:
patherr = nil
case *os.PathError:
patherr = err.(*os.PathError)
}
if patherr != nil && patherr.Error != os.ENOENT {
fmt.Fprintf(os.Stderr, "Error stat'ing ~/.gonewrc. %v", patherr)
return patherr
} else if stat == nil || (patherr != nil && patherr.Error == os.ENOENT) {
if DEBUG || VERBOSE {
log.Print("Config not found. Prompting user for info.")
}
return MakeConfig()
} else {
if DEBUG {
log.Print("~/.gonewrc found.")
}
}
return nil
}
func MakeConfig() os.Error {
var (
c = config.NewDefault()
scanner = bufio.NewReader(os.Stdin)
errScan os.Error
buff []byte
)
fmt.Printf("Enter your name: ")
buff, _, errScan = scanner.ReadLine()
if errScan != nil {
return errScan
}
c.AddOption("variables", "name", string(bytes.TrimRight(buff, "\n")))
fmt.Printf("Enter your email address: ")
buff, _, errScan = scanner.ReadLine()
if errScan != nil {
return errScan
}
c.AddOption("variables", "email", string(bytes.TrimRight(buff, "\n")))
var (
repoName string
repoOk bool
)
for !repoOk {
fmt.Printf("Enter a repository type ('git', or none): ")
buff, _, errScan = scanner.ReadLine()
if errScan != nil {
return errScan
}
repoName = string(bytes.TrimRight(buff, "\n"))
switch repoName {
case "":
fallthrough
case "git":
repoOk = true
default:
fmt.Printf("I didn't understand repo type %s\n", repoName)
}
}
c.AddOption("general", "repo", repoName)
var (
hostName string
hostOk bool
)
for !hostOk {
fmt.Printf("Enter a repo host ('github', or none): ")
buff, _, errScan = scanner.ReadLine()
if errScan != nil {
return errScan
}
hostName = string(bytes.TrimRight(buff, "\n"))
switch hostName {
case "":
fallthrough
case "github":
hostOk = true
default:
fmt.Printf("I didn't understand repo host %s\n", hostName)
}
}
c.AddOption("general", "host", hostName)
var hostuser = ""
var hostuserOk = hostName != "github"
for !hostuserOk {
fmt.Printf("Enter a %s username: ", hostName)
buff, _, errScan = scanner.ReadLine()
if errScan != nil {
return errScan
}
hostuser = string(bytes.TrimRight(buff, "\n"))
if hostuser == "" {
fmt.Printf("Invalid %s username '%s'\n", hostName, hostuser)
} else {
hostuserOk = true
}
}
c.AddOption("general", "hostuser", hostuser)
return c.WriteFile(ConfigFilename, FilePermissions, "Generated configuration for gonew.")
}