Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrzna committed Jun 24, 2020
1 parent 1c6cde4 commit 5c0c66f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
9 changes: 2 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func loadConfig() *config {

if fileExists(pathConfigFile) {
err := readProperties(pathConfigFile, func(key string, value string) {
switch strings.ToUpper(key) {
switch key {
case confTTYnumber:
c.tty = parseTTY(value, "0")
case confSwitchTTY:
c.switchTTY = parseBool(value, "true")
case confPrintIssue:
c.printIssue = parseBool(value, "true")
case confDefaultUser:
c.defaultUser = parseDefaultUser(value, "")
c.defaultUser = sanitizeValue(value, "")
case confAutologin:
c.autologin = parseBool(value, "false")
case confLang:
Expand Down Expand Up @@ -83,11 +83,6 @@ func parseBool(strBool string, defaultValue string) bool {
return val
}

// Parse default user.
func parseDefaultUser(defaultUser string, defaultValue string) string {
return sanitizeValue(defaultUser, defaultValue)
}

// Sanitize value.
func sanitizeValue(value string, defaultValue string) string {
if value == "" {
Expand Down
7 changes: 3 additions & 4 deletions desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -62,7 +61,7 @@ type lastSession struct {
func selectDesktop(uid int) *desktop {
desktops := listAllDesktops()
if len(desktops) == 0 {
handleErr(errors.New("Not found any installed desktop."))
handleStrErr("Not found any installed desktop.")
}

lastSessions := loadLastSessions()
Expand Down Expand Up @@ -148,7 +147,7 @@ func getDesktop(path string, env enEnvironment) *desktop {
}

readProperties(path, func(key string, value string) {
switch strings.ToUpper(key) {
switch key {
case desktopName:
d.name = value
case desktopExec:
Expand All @@ -169,7 +168,7 @@ func loadUserDesktop(homeDir string) (*desktop, string) {
d := desktop{isUser: true, path: confFile, env: Xorg}

err := readProperties(confFile, func(key string, value string) {
switch strings.ToUpper(key) {
switch key {
case confCommand:
d.exec = sanitizeValue(value, "")
case confEnvironment:
Expand Down
2 changes: 1 addition & 1 deletion login.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func xorg(usr *sysuser, d *desktop, conf *config) {
xorg.Env = append(os.Environ())
xorg.Start()
if xorg.Process == nil {
handleErr(errors.New("Xorg is not running"))
handleStrErr("Xorg is not running")
}
log.Print("Started Xorg")

Expand Down
9 changes: 8 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func readProperties(filePath string, method propertyFunc) error {
if strings.Index(value, "#") >= 0 {
value = value[:strings.Index(value, "#")]
}
key = strings.TrimSpace(key)
key = strings.ToUpper(strings.TrimSpace(key))
value = strings.TrimSpace(value)
method(key, value)
}
Expand All @@ -50,6 +50,13 @@ func fileExists(path string) bool {
return err == nil
}

// Handles error passed as string and calls handleErr function.
func handleStrErr(err string) {
if err != "" {
handleErr(errors.New(err))
}
}

// If error is not nil, otherwise it prints error, waits for user input and then exits the program.
func handleErr(err error) {
if err != nil {
Expand Down

0 comments on commit 5c0c66f

Please sign in to comment.