func TestLoad(t *testing.T) { // Create temporary data & file tempFile := "tempRCData" comment := "# Example config file" key1 := "FILE" val1 := "/home/user/myfile" key2 := "VERBOSE" val2 := "true" byteStream := []byte(comment + "\n" + key1 + " = " + val1 + "\n" + key2 + " = " + val2 + "\n") err := ioutil.WriteFile(tempFile, byteStream, 0644) if err != nil { t.Errorf("Problem with creating temporary file.") } defer os.Remove(tempFile) // Open temporary file file, err := os.Open(tempFile) if err != nil { t.Errorf(err.Error()) } defer file.Close() // Try to load properties and read the data properties := gprops.New() errLoad := properties.Load(file) if errLoad != nil { t.Errorf(errLoad.Error()) } if properties.Get(key1) != val1 || properties.Get(key2) != val2 { t.Errorf("Data loaded from file are not as expected.\n") } }
func TestGet(t *testing.T) { properties := gprops.New() properties.Set("key1", "value1") if properties.Get("key1") != "value1" { t.Errorf("Returned value is not as expected.\n") } }
func TestDelete(t *testing.T) { properties := gprops.New() properties.Set("key1", "value1") properties.Delete("key1") if properties.Contains("key1") == true { t.Errorf("The key has not been deleted.\n") } }
func TestContainsKey(t *testing.T) { properties := gprops.New() properties.Set("key", "") if properties.Contains("key") == false { t.Errorf("Returns false for existing key.\n") } if properties.Contains("anotherkey") == true { t.Errorf("Returns true for not existing key.\n") } }
func TestStore(t *testing.T) { // Prepare the properties to store propsToStore := gprops.New() key1 := "key1" val1 := "val1" key2 := "key2" val2 := "val2" propsToStore.Set(key1, val1) propsToStore.Set(key2, val2) // Store properties in file tempFile := "temporaryPropsFile" f, err := os.Create(tempFile) if err != nil { t.Errorf(err.Error()) } propsToStore.Store(f, "Test properties") f.Close() // Load the properties from the file propsLoaded := gprops.New() f2, err := os.Open(tempFile) if err != nil { t.Errorf(err.Error()) } defer f2.Close() errLoad := propsLoaded.Load(f2) if errLoad != nil { t.Errorf(errLoad.Error()) } // Compare the properties if propsLoaded.Get(key1) != propsToStore.Get(key1) || propsLoaded.Get(key2) != propsToStore.Get(key2) { t.Errorf("Data stored in a file are not as expected.\n") } os.Remove(tempFile) }
// GetConfigSettings returns contents of settings file (~/.blrc) func getConfigSettings() (dataFile string, err error) { // Read config file configSettings := gprops.New() configFile, err := os.Open(path.Join(os.Getenv("HOME"), ".blrc")) if err == nil { err = configSettings.Load(configFile) if err != nil { return NotSetStringValue, err } } configFile.Close() dataFile = configSettings.GetOrDefault(confDataFile, NotSetStringValue) return dataFile, nil }
func main() { dataFile := "" verbose := false movingAverage := 7 cli.CommandHelpTemplate = `NAME: {{.HelpName}} - {{.Usage}} USAGE: {{.HelpName}}{{if .Subcommands}} [subcommand]{{end}}{{if .Flags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} OPTIONS: {{range .Flags}}{{.}} {{end}}{{ end }}{{if .Subcommands}} SUBCOMMANDS: {{range .Subcommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} {{end}}{{ end }} ` // Loading properties from config file if exists configSettings := gprops.New() configFile, err := os.Open(path.Join(os.Getenv("HOME"), ".wwrc")) if err == nil { err = configSettings.Load(configFile) if err != nil { fmt.Fprintf(os.Stderr, "weightWatcher: syntax error in %s. Exit.\n", configFile.Name()) return } } configFile.Close() if configSettings.Contains(CONF_DATAFILE) { dataFile = configSettings.Get(CONF_DATAFILE) } if configSettings.Contains(CONF_VERBOSE) { verbose, err = strconv.ParseBool(configSettings.Get(CONF_VERBOSE)) if err != nil { verbose = false } } if configSettings.Contains(CONF_MOVINGAVERAGE) { movingAverage, err = strconv.Atoi(configSettings.Get(CONF_MOVINGAVERAGE)) if err != nil { fmt.Fprintf(os.Stderr, "weightWatcher: syntax error in %s. Exit.\n", configFile.Name()) return } } // Commandline arguments app := cli.NewApp() app.Name = "weightWatcher" app.Usage = "keeps track of your weight" app.Version = "0.1" app.Authors = []cli.Author{ cli.Author{"Marcin 'Zbroju' Zbroinski", "*****@*****.**"}, } // Flags definitions flagDate := cli.StringFlag{ Name: "date, d", Value: today(), Usage: "date of measurement (format: YYYY-MM-DD)", } flagVerbose := cli.BoolFlag{ Name: "verbose, b", Usage: "show more output", Destination: &verbose, } flagWeight := cli.Float64Flag{ Name: "weight, w", // Value: 0, Usage: "measured weight", } flagFile := cli.StringFlag{ Name: "file, f", Value: dataFile, Usage: "data file", } flagId := cli.IntFlag{ Name: "id, i", Value: -1, Usage: "id of edited or removed object", } flagMovAv := cli.IntFlag{ Name: "average, a", Value: movingAverage, Usage: "measurement subset size for calculating moving average", } // Commands app.Commands = []cli.Command{ { Name: "init", Aliases: []string{"I"}, Flags: []cli.Flag{flagVerbose, flagFile}, Usage: "init a new data file specified by the user", Action: cmdInit, }, { Name: "add", Aliases: []string{"A"}, Flags: []cli.Flag{flagVerbose, flagDate, flagWeight, flagFile}, Usage: "add a new measurement", Action: cmdAddMeasurement, }, { Name: "edit", Aliases: []string{"E"}, Flags: []cli.Flag{flagVerbose, flagDate, flagWeight, flagFile, flagId}, Usage: "edit a measurement", Action: cmdEditMeasurement, }, { Name: "remove", Aliases: []string{"R"}, Flags: []cli.Flag{flagVerbose, flagFile, flagId}, Usage: "remove a measurement", Action: cmdRemoveMeasurement, }, { Name: "list", Aliases: []string{"L"}, Flags: []cli.Flag{flagVerbose, flagFile}, Usage: "lists all measurements", Action: cmdListMeasurements, }, { Name: "show", Aliases: []string{"S"}, Usage: "show report", // Reports Subcommands: []cli.Command{ { Name: "history", Aliases: []string{"a"}, Flags: []cli.Flag{flagFile, flagMovAv}, Usage: "historical data with moving average (<x> periods)", Action: reportHistory, }, }, }, } app.Run(os.Args) }
func TestNew(t *testing.T) { properties := gprops.New() properties.Set("key1", "value1") }