-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use include statements instead of writing whole file contents
- Loading branch information
Showing
3 changed files
with
17 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters