Config fixes

* Fix empty config configuration
  * Alter menus when no remotes
  * Save config file after delete remote
This commit is contained in:
Nick Craig-Wood 2014-03-15 16:52:51 +00:00
parent 0a108832e2
commit b3f1a45bbf

View file

@ -72,6 +72,10 @@ func LoadConfig() {
ConfigFile, err = goconfig.LoadConfigFile(ConfigPath) ConfigFile, err = goconfig.LoadConfigFile(ConfigPath)
if err != nil { if err != nil {
log.Printf("Failed to load config file %v - using defaults", ConfigPath) log.Printf("Failed to load config file %v - using defaults", ConfigPath)
ConfigFile, err = goconfig.LoadConfigFile(os.DevNull)
if err != nil {
log.Fatalf("Failed to read null config file: %v", err)
}
} }
} }
@ -84,8 +88,11 @@ func SaveConfig() {
} }
// Show an overview of the config file // Show an overview of the config file
func ShowConfig() { func ShowRemotes() {
remotes := ConfigFile.GetSectionList() remotes := ConfigFile.GetSectionList()
if len(remotes) == 0 {
return
}
sort.Strings(remotes) sort.Strings(remotes)
fmt.Printf("%-20s %s\n", "Name", "Type") fmt.Printf("%-20s %s\n", "Name", "Type")
fmt.Printf("%-20s %s\n", "====", "====") fmt.Printf("%-20s %s\n", "====", "====")
@ -112,7 +119,7 @@ func ReadLine() string {
} }
// Command - choose one // Command - choose one
func Command(commands []string) int { func Command(commands []string) byte {
opts := []string{} opts := []string{}
for _, text := range commands { for _, text := range commands {
fmt.Printf("%c) %s\n", text[0], text[1:]) fmt.Printf("%c) %s\n", text[0], text[1:])
@ -128,7 +135,7 @@ func Command(commands []string) int {
} }
i := strings.IndexByte(optString, result[0]) i := strings.IndexByte(optString, result[0])
if i >= 0 { if i >= 0 {
return i return result[0]
} }
} }
} }
@ -179,11 +186,11 @@ func ShowRemote(name string) {
func OkRemote(name string) bool { func OkRemote(name string) bool {
ShowRemote(name) ShowRemote(name)
switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i { switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i {
case 0: case 'y':
return true return true
case 1: case 'e':
return false return false
case 2: case 'd':
ConfigFile.DeleteSection(name) ConfigFile.DeleteSection(name)
return true return true
default: default:
@ -236,24 +243,37 @@ func EditRemote(name string) {
SaveConfig() SaveConfig()
} }
// Delete a remote
func DeleteRemote(name string) {
ConfigFile.DeleteSection(name)
SaveConfig()
}
// Edit the config file interactively // Edit the config file interactively
func EditConfig() { func EditConfig() {
for { for {
haveRemotes := len(ConfigFile.GetSectionList()) != 0
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "qQuit config"}
if haveRemotes {
fmt.Printf("Current remotes:\n\n") fmt.Printf("Current remotes:\n\n")
ShowConfig() ShowRemotes()
fmt.Printf("\n") fmt.Printf("\n")
switch i := Command([]string{"eEdit existing remote", "nNew remote", "dDelete remote", "qQuit config"}); i { } else {
case 0: fmt.Printf("No remotes found - make a new one\n")
what = append(what[1:2], what[3])
}
switch i := Command(what); i {
case 'e':
name := ChooseRemote() name := ChooseRemote()
EditRemote(name) EditRemote(name)
case 1: case 'n':
fmt.Printf("name> ") fmt.Printf("name> ")
name := ReadLine() name := ReadLine()
NewRemote(name) NewRemote(name)
case 2: case 'd':
name := ChooseRemote() name := ChooseRemote()
ConfigFile.DeleteSection(name) DeleteRemote(name)
case 3: case 'q':
return return
} }
} }