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
This commit is contained in:
Markus Pesch 2020-07-31 00:02:29 +02:00
parent 8c1bd57ad5
commit 6c03bc078b
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
1 changed files with 26 additions and 3 deletions

View File

@ -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
}
}