Skip to content

Commit

Permalink
Merge pull request #5 from merschformann/merschformann/less-aggressiv…
Browse files Browse the repository at this point in the history
…e-config-management

Offer reset config and fix darwin/arm64 build
  • Loading branch information
merschformann committed May 19, 2022
2 parents e1260f1 + 7d195c1 commit 994f870
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
21 changes: 21 additions & 0 deletions core/auxiliary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -39,3 +42,21 @@ func convertHexToRgb(hex string) (r, g, b uint8, err error) {
b = uint8(rgb & 0x0000ff)
return r, g, b, nil
}

// AskUser asks the user a yes/no question and returns true if the user answers
// yes.
func AskUser(question string) (bool, error) {
// Ask the user
fmt.Printf("%s (y/N): ", question)
// Read user input
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return false, err
}
// Normalize input
input = strings.ToLower(input)
input = strings.TrimSpace(input)
// Check input
return input == "y" || input == "yes", nil
}
16 changes: 7 additions & 9 deletions core/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -150,37 +151,34 @@ func defaultConfigFile() string {
func Load() (Config, error) {
// If no configuration file exists, create one
if _, err := os.Stat(defaultConfigFile()); os.IsNotExist(err) {
return saveDefault()
return SaveDefault()
}
// Read configuration file
var config Config
data, err := ioutil.ReadFile(defaultConfigFile())
if err != nil {
fmt.Println("Error reading config file (replacing with default config):", err)
return saveDefault()
return config, errors.New("Error reading config file: " + err.Error())
}
// Unmarshal
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Println("Error unmarshaling config file (replacing with default config):", err)
return saveDefault()
return config, errors.New("Error unmarshaling config file: " + err.Error())
}
// Check version
if config.ConfigVersion != ConfigVersion {
version := config.ConfigVersion
if version == "" {
version = "unknown"
}
fmt.Println("Configuration file version mismatch (replacing with default config), version found:", version)
return saveDefault()
return config, errors.New("Config file version " + version + " is not supported")
}
// Validate (replace invalid values with defaults)
config = config.validate()
return config, nil
}

// saveDefault creates a default config and immediately saves it.
func saveDefault() (Config, error) {
// SaveDefault creates a default config and immediately saves it.
func SaveDefault() (Config, error) {
c := DefaultConfig()
return c, c.Save()
}
Expand Down
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@ import (
func main() {
// Get configuration
config, err := core.Load()
// If there was an error loading the config, offer the user the option to
// reset it (or simply exit).
if err != nil {
fmt.Println("error handling configuration:", err)
os.Exit(1)
fmt.Println("error loading configuration:", err)
// Ask the user if they want to reset the config
if ok, in_err := core.AskUser("Reset configuration?"); in_err != nil {
fmt.Println("error asking user:", in_err)
os.Exit(1)
} else if ok {
// Reset config
config, in_err = core.SaveDefault()
if in_err != nil {
fmt.Println("error resetting configuration:", in_err)
os.Exit(1)
}
} else {
// Exit
os.Exit(0)
}
}
// Parse flags
config, t, changed, err := core.ParseFlags(config, Version)
Expand Down
2 changes: 1 addition & 1 deletion material/scripts/buildall.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# Specify platforms to build for
platforms=("linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64", "windows/amd64")
platforms=("linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64")

# Clean build directory
builddir="build"
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version = "v0.1.0"
const Version = "v0.1.1"

0 comments on commit 994f870

Please sign in to comment.