From 6c03bc078b241eb174447b67544a254d767b1aff Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Fri, 31 Jul 2020 00:02:29 +0200 Subject: [PATCH] fix: default directory for the sqlite db The default directory of the sqlite db has now been changed depending of the user who executes flucky. The place where the sqlite db will be stored is unchanged for the root user, but for all other users the sqlite db will now be created to ~/.cache/flucky --- cli/root.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) 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 + } +}