diff --git a/cli/root.go b/cli/root.go index f9a87c3..cd8a86e 100644 --- a/cli/root.go +++ b/cli/root.go @@ -76,14 +76,21 @@ func preRunError(cmd *cobra.Command, args []string) error { } postgresTimeStamp = time.Date(postgresTimeStamp.Year(), postgresTimeStamp.Month(), postgresTimeStamp.Day(), postgresTimeStamp.Hour(), postgresTimeStamp.Minute(), postgresTimeStamp.Second(), int(math.Round(float64(postgresTimeStamp.Nanosecond())/1000000)*1000000), location) + // default cache directory + defaultCacheDir, err := getDefaultCacheDir() + if err != nil { + return err + } + // Default configuration + dsn := fmt.Sprintf("sqlite3://%v/sqlite.db?cache=shared&mode=memory&foreign_keys=on", defaultCacheDir) cnf := config.Config{ Device: &types.Device{ ID: uuid.NewV4().String(), Name: hostname, CreationDate: postgresTimeStamp, }, - DSN: "sqlite3:///var/log/flucky/sqlite.db?cache=shared&mode=memory&foreign_keys=on", + DSN: dsn, } err = config.Write(&cnf, configFile) @@ -95,8 +102,8 @@ func preRunError(cmd *cobra.Command, args []string) error { return nil } -// GetDefaultConfigPath returns the default path to the configuration of -// rpm-distributor +// getDefaultConfigFile returns the default path to the configuration file of +// flucky func getDefaultConfigFile() (string, error) { u, err := user.Current() if err != nil { @@ -110,3 +117,19 @@ func getDefaultConfigFile() (string, error) { return filepath.Join(u.HomeDir, ".config/flucky/config.json"), nil } } + +// getDefaultCacheDir returns the default path to the cache directory where +// flucky stores his measured values. +func getDefaultCacheDir() (string, error) { + u, err := user.Current() + if err != nil { + return "", fmt.Errorf("Can not read current user: %v", err) + } + + switch u.Uid { + case "0": + return "/var/cache/flucky", nil + default: + return filepath.Join(u.HomeDir, ".cache/flucky"), nil + } +}