Skip to content

Commit

Permalink
Use include statements instead of writing whole file contents
Browse files Browse the repository at this point in the history
  • Loading branch information
psidex committed Apr 10, 2020
1 parent 3a657c9 commit caeb7b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ Inspired by [Peace](https://sourceforge.net/projects/peace-equalizer-apo-extensi

## Install

- Download the latest `E-APO-Config-Switcher.zip` from [releases](https://github.com/psidex/EACS/releases/latest)
- Download the latest `EACS.zip` from [releases](https://github.com/psidex/EACS/releases/latest)
- Extract it to `<Equalizer APO install location>\EqualizerAPO\config`
- Run `E_APO_Config_Switcher.exe` that is now inside `config\E-APO-Config-Switcher`
- Run `EACS.exe` that is now inside `config\EACS`
- You should now have the icon in your system tray, you can now left/right click on it to switch configurations
- If you want it to run on system startup, create a shortcut to the exe and move it to the Windows startup directory

## Edit / Add / Remove Configurations

The folder that contains all the configuration files will be located at `EqualizerAPO\config\E-APO-Config-Switcher\config-files`
The folder that contains all the configuration files will be located at `EqualizerAPO\config\EACS\config-files`

See [the user guide](./GUIDE.md) for more detailed information
45 changes: 11 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,58 @@
package config

import (
"fmt"
"github.com/getlantern/systray"
"io/ioutil"
"os"
"strings"
"tawesoft.co.uk/go/dialog"
)

const configFileDir = "./config-files/"
const configFileMaster = "../config.txt"
const includeText = "Include: EACS\\config-files\\%s"

type EApoConfig struct {
Name string
Data []byte
FileName string
MenuItem *systray.MenuItem
}

// Takes a FileInfo interface. Assuming that file is in configFileDir, load the contents of that file into an
// EApoConfig struct and return it.
func configStructFromFile(file os.FileInfo) (EApoConfig, error) {
fileName := file.Name()

dat, err := ioutil.ReadFile(configFileDir + fileName)
if err != nil {
return EApoConfig{}, err
}

configName := strings.Replace(fileName, ".txt", "", 1)

return EApoConfig{
Name: configName,
Data: dat,
}, nil
}

// Creates a slice of EApoConfigs which contain the data for all config files in configFileDir.
func CreateConfigSlice() []*EApoConfig {
var configSlice []*EApoConfig

files, err := ioutil.ReadDir(configFileDir)
if err != nil {
dialog.Alert("EACS Config Error", "Cannot read config file directory: "+configFileDir)
// Can't do anything else.
errMsg := fmt.Sprintf("Cannot read config file directory: %s", configFileDir)
dialog.Alert("EACS Config Error", errMsg)
os.Exit(1)
}

for _, file := range files {
if !file.IsDir() {
configStruct, err := configStructFromFile(file)
// Ignore files that cause err.
if err == nil {
configSlice = append(configSlice, &configStruct)
}
configStruct := EApoConfig{FileName: file.Name()}
configSlice = append(configSlice, &configStruct)
}
}

return configSlice
}

// Write all items in configSlice to configFileMaster if their associated menu item is checked.
func WriteConfigToMaster(configSlice []*EApoConfig) {
var completeData []byte
newline := []byte("\n")

for _, config := range configSlice {
if config.MenuItem.Checked() {
// "..." -> https://stackoverflow.com/a/16248257/6396652
includeStatement := fmt.Sprintf(includeText, config.FileName)
completeData = append(completeData, []byte(includeStatement)...)
completeData = append(completeData, newline...)
completeData = append(completeData, config.Data...)
}
}

err := ioutil.WriteFile(configFileMaster, completeData, 0644)
if err != nil {
dialog.Alert("EACS Config Error", "Cannot write to master config file: "+configFileMaster)
errMsg := fmt.Sprintf("Cannot write to master config file: %s", configFileMaster)
dialog.Alert("EACS Config Error", errMsg)
os.Exit(1)
return
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/getlantern/systray"
"github.com/psidex/EACS/config"
"github.com/psidex/EACS/icon"
"strings"
)

func main() {
Expand All @@ -18,7 +19,8 @@ func onReady() {
configSlice := config.CreateConfigSlice()

for _, configStruct := range configSlice {
btn := systray.AddMenuItem(configStruct.Name, "Activate / Deactivate this config")
configName := strings.Replace(configStruct.FileName, ".txt", "", 1)
btn := systray.AddMenuItem(configName, "Activate / Deactivate this config")
configStruct.MenuItem = btn

go func() {
Expand Down

0 comments on commit caeb7b9

Please sign in to comment.