Skip to content

Commit

Permalink
fix: default home directory behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
terasum committed Sep 19, 2023
1 parent 2865986 commit ba0afd9
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 40 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/c0mm4nd/go-ripemd v0.0.0-20200326052756-bd1759ad7d10
github.com/creasty/go-levenshtein v0.0.0-20161128082938-38ce641d5030
github.com/gin-gonic/gin v1.9.1
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e
github.com/spf13/viper v1.10.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4P
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
Expand Down
4 changes: 1 addition & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ package config
import "github.com/spf13/viper"

type Config struct {
BaseDictDir string `toml:"baseDictDir"`
StaticServerPort int
CacheFileDir string
BaseDictDir string `toml:"BaseDictDir"`
}

func ReadConfig(configFilePath string) (*Config, error) {
Expand Down
1 change: 0 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ import (
func TestReadConfig(t *testing.T) {
cfg, err := ReadConfig("./testdata/test.toml")
assert.Nil(t, err)
assert.Equal(t, cfg.StaticServerPort, 1234)
assert.Equal(t, cfg.BaseDictDir, "testdir")
}
8 changes: 6 additions & 2 deletions internal/entry/app_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package entry

import (
"errors"
"fmt"
"os"
"path/filepath"

Expand All @@ -30,20 +31,22 @@ func defaultConfigPath() (string, error) {
if err != nil {
return "", err
}
configDir := filepath.Join(home, ".medict", "dicts")
fmt.Printf("medict app: default config dir %s\n", home)
configDir := filepath.Join(home, "dicts")
if _, err = os.Stat(configDir); errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(configDir, os.ModePerm)
if err != nil {
return "", err
}
}
configFile := filepath.Join(home, ".medict", "medict.toml")
configFile := filepath.Join(home, "medict.toml")
if _, err = os.Stat(configFile); errors.Is(err, os.ErrNotExist) {
err = os.WriteFile(configFile, []byte(configTmpl), 0644)
if err != nil {
return "", err
}
}
fmt.Printf("medict app: default config file %s\n", configFile)
return configFile, nil

}
Expand All @@ -61,5 +64,6 @@ func DefaultConfig() (*config.Config, error) {
if err != nil {
return nil, err
}
fmt.Printf("medict app: config dicts dir %s\n", cfg.BaseDictDir)
return cfg, nil
}
3 changes: 1 addition & 2 deletions internal/entry/config_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
package entry

const configTmpl = `
DictDir="$HOME/.medict/dicts"
StaticServerPort=59183
DictBaseDir="$HOME/dicts"
`
44 changes: 44 additions & 0 deletions internal/utils/pathhome_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build darwin

package utils

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/kirsle/configdir"
)

func ReplaceHome(origin string) (string, error) {
configPath, err := HomeDir()
if err != nil {
return "", err
}

origin = strings.ReplaceAll(origin, "$HOME", configPath)
fmt.Printf("medict app: ReplaceHome dictionary dir %s\n", origin)
return origin, nil
}

func HomeDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("userhomedir err: %s", err.Error())
}
configPath := filepath.Join(home, ".medict")

err = configdir.MakePath(configPath)
if err != nil {
return "", fmt.Errorf("base home dir mkall failed, %s", err.Error())
}

//configPath := configdir.LocalConfig("medict")
//fmt.Printf("medict app: HomeDir %s\n", configPath)
//err := configdir.MakePath(configPath) // Ensure it exists.
//if err != nil {
// return "", fmt.Errorf("base home dir mkall failed, %s", err.Error())
//}
return configPath, nil
}
29 changes: 29 additions & 0 deletions internal/utils/pathhome_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build linux

package utils

import (
"os"
"strings"

"github.com/kirsle/configdir"
)

func ReplaceHome(origin string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return origin, err
}

origin = strings.ReplaceAll(origin, "$HOME", home)
return origin, nil
}

func HomeDir() (string, error) {
configPath := configdir.LocalConfig("medict")
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
return "", err
}
return configPath, nil
}
30 changes: 30 additions & 0 deletions internal/utils/pathhome_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build windows
// +build windows

package utils

import (
"os"
"strings"

"github.com/kirsle/configdir"
)

func ReplaceHome(origin string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return origin, err
}

origin = strings.ReplaceAll(origin, "$HOME", home)
return origin, nil
}

func HomeDir() (string, error) {
configPath := configdir.LocalConfig("medict")
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
return "", err
}
return configPath, nil
}
14 changes: 0 additions & 14 deletions internal/utils/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,8 @@ package utils
import (
"os"
"path/filepath"
"strings"
)

func HomeDir() (string, error) {
return os.UserHomeDir()
}

func ReplaceHome(origin string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return origin, err
}
origin = strings.ReplaceAll(origin, "$HOME", home)
return origin, nil
}

func FetchBaseDirName(fpath string) string {
return filepath.Base(fpath)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backserver/back_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewStaticServer(conf *config.Config) (*BackServer, error) {
func (bs *BackServer) SetUp() error {
dictsSvc, err := service.NewDictService(bs.Config)
if err != nil {
return err
return fmt.Errorf("back_server setup failed, err: %s", err.Error())
}

dictCon := apis.NewDictsController(dictsSvc)
Expand Down
11 changes: 6 additions & 5 deletions pkg/service/dicts_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,17 @@ func (ds *DictService) Search(dictId string, keyword string) ([]*model.KeyIndex,
func (ds *DictService) walkDicts() error {
baseDir, err := utils.ReplaceHome(ds.config.BaseDictDir)
if err != nil {
return err
return fmt.Errorf("replace home failed, %s", err.Error())
}

items, err := support.WalkDir(baseDir)
if err != nil {
return err
return fmt.Errorf("walk dir failed, basedir %s, %s", baseDir, err.Error())
}
for _, dirItem := range items {
dictItem, err := NewByDirItem(dirItem)
if err != nil {
return err
dictItem, err1 := NewByDirItem(dirItem)
if err1 != nil {
return fmt.Errorf("new dir item failed, %s", err1.Error())
}
ds.dicts[dictItem.ID] = dictItem
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/service/dicts_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

func TestDictService_Dicts(t *testing.T) {
ds, err := NewDictService(&config.Config{
BaseDictDir: "./testdata/dicts",
StaticServerPort: 0,
CacheFileDir: "",
BaseDictDir: "./testdata/dicts",
})
assert.Nil(t, err)
dicts := ds.Dicts()
Expand All @@ -38,9 +36,7 @@ func TestDictService_Dicts(t *testing.T) {

func TestDictService_Dicts2(t *testing.T) {
ds, err := NewDictService(&config.Config{
BaseDictDir: "../../testdict/",
StaticServerPort: 0,
CacheFileDir: "",
BaseDictDir: "../../testdict/",
})
assert.Nil(t, err)
dicts := ds.Dicts()
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/mdict.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func NewMdict(dirItem *model.DirItem) (model.GeneralDictionary, error) {

mdx, err := gomdict.New(dirItem.MdictMdxAbsPath)
if err != nil {
return nil, err
return nil, fmt.Errorf("new mdx file failed, %s", err.Error())
}

mdds := make([]*gomdict.Mdict, 0)

for _, mddpath := range dirItem.MdictMddAbsPath {
mdd, err1 := gomdict.New(mddpath)
if err1 != nil {
return nil, err1
return nil, fmt.Errorf("new mdd file failed, %s", err1.Error())
}
mdds = append(mdds, mdd)
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/service/support/filewalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package support

import (
"fmt"
"io/fs"
"path/filepath"

Expand All @@ -28,8 +29,11 @@ import (
func WalkDir(dirpath string) ([]*model.DirItem, error) {
list := make([]*model.DirItem, 0)
err := filepath.WalkDir(dirpath, func(path string, d fs.DirEntry, err error) error {
if dirpath == "" {
return fmt.Errorf("walkdir failed, path is empty, path: [%s] %s", path, err.Error())
}
if err != nil {
return err
return fmt.Errorf("walkdir failed, path: [%s] %s", path, err.Error())
}
// skip self
if dirpath == path {
Expand All @@ -41,7 +45,7 @@ func WalkDir(dirpath string) ([]*model.DirItem, error) {
}
item, err := innerWalker(dirpath, path, err)
if err != nil {
return err
return fmt.Errorf("inner walker failed , path:[%s], %s", path, err.Error())
}
if item.MdictMdxAbsPath != "" {
list = append(list, item)
Expand All @@ -52,8 +56,11 @@ func WalkDir(dirpath string) ([]*model.DirItem, error) {
}

func innerWalker(rootpath, subpath string, err error) (*model.DirItem, error) {
if rootpath == "" {
return nil, fmt.Errorf("inner walkdir failed, path is empty, path: [%s] %s", rootpath, err.Error())
}
if err != nil {
return nil, err
return nil, fmt.Errorf("inner walk entry failed, path %s, err %s", rootpath, err.Error())
}
item := &model.DirItem{
BaseDir: rootpath,
Expand All @@ -64,7 +71,7 @@ func innerWalker(rootpath, subpath string, err error) (*model.DirItem, error) {

err = filepath.Walk(subpath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
return fmt.Errorf("inner walk dir failed, path %s, %s", path, err)
}
if info.IsDir() {
return nil
Expand Down

0 comments on commit ba0afd9

Please sign in to comment.