Support customizing HISHTORY_PATH for people who want to install hishtory in an alternate location to fix #54

This commit is contained in:
David Dworken
2022-12-16 22:22:57 -08:00
parent 53e97253e5
commit f8b51e49da
8 changed files with 80 additions and 52 deletions

View File

@ -38,7 +38,7 @@ func GetLogger() *logrus.Logger {
}
lumberjackLogger := &lumberjack.Logger{
Filename: path.Join(homedir, data.HISHTORY_PATH, "hishtory.log"),
Filename: path.Join(homedir, data.GetHishtoryPath(), "hishtory.log"),
MaxSize: 1, // MB
MaxBackups: 10,
MaxAge: 30, // days
@ -61,9 +61,9 @@ func MakeHishtoryDir() error {
if err != nil {
return fmt.Errorf("failed to get user's home directory: %v", err)
}
err = os.MkdirAll(path.Join(homedir, data.HISHTORY_PATH), 0o744)
err = os.MkdirAll(path.Join(homedir, data.GetHishtoryPath()), 0o744)
if err != nil {
return fmt.Errorf("failed to create ~/.hishtory dir: %v", err)
return fmt.Errorf("failed to create ~/%s dir: %v", data.GetHishtoryPath(), err)
}
return nil
}
@ -86,7 +86,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
Colorful: false,
},
)
dbFilePath := path.Join(homedir, data.HISHTORY_PATH, data.DB_PATH)
dbFilePath := path.Join(homedir, data.GetHishtoryPath(), data.DB_PATH)
dsn := fmt.Sprintf("file:%s?mode=rwc&_journal_mode=WAL", dbFilePath)
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{SkipDefaultTransaction: true, Logger: newLogger})
if err != nil {
@ -189,9 +189,9 @@ func GetConfigContents() ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to retrieve homedir: %v", err)
}
dat, err := os.ReadFile(path.Join(homedir, data.HISHTORY_PATH, data.CONFIG_PATH))
dat, err := os.ReadFile(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH))
if err != nil {
files, err := os.ReadDir(path.Join(homedir, data.HISHTORY_PATH))
files, err := os.ReadDir(path.Join(homedir, data.GetHishtoryPath()))
if err != nil {
return nil, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
}
@ -200,7 +200,7 @@ func GetConfigContents() ([]byte, error) {
filenames += file.Name()
filenames += ", "
}
return nil, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, err)
return nil, fmt.Errorf("failed to read config file (files in HISHTORY_PATH: %s): %v", filenames, err)
}
return dat, nil
}
@ -237,7 +237,7 @@ func SetConfig(config ClientConfig) error {
if err != nil {
return err
}
configPath := path.Join(homedir, data.HISHTORY_PATH, data.CONFIG_PATH)
configPath := path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH)
stagedConfigPath := configPath + ".tmp-" + uuid.Must(uuid.NewRandom()).String()
err = os.WriteFile(stagedConfigPath, serializedConfig, 0o644)
if err != nil {
@ -255,7 +255,7 @@ func InitConfig() error {
if err != nil {
return err
}
_, err = os.Stat(path.Join(homedir, data.HISHTORY_PATH, data.CONFIG_PATH))
_, err = os.Stat(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH))
if errors.Is(err, os.ErrNotExist) {
return SetConfig(ClientConfig{})
}