Skip to content

Commit

Permalink
feat: dfpath
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi committed Dec 6, 2021
1 parent cfbf145 commit b3bf9ad
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 38 deletions.
128 changes: 95 additions & 33 deletions internal/dfpath/dfpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,124 @@ import (
"d7y.io/dragonfly/v2/pkg/util/fileutils"
)

// Dfpath is the interface used for init project path
type Dfpath interface {

WorkHome() string
CacheDir() string
ConfigDir() string
LogDir() string
DataDir() string
PluginDir() string
DaemonSockPath() string
DaemonLockPath() string
DfgetLockPath() string
}

// dfpath provides init project path function
type dfpath struct {
dataDir string
pluginDir string
workHome string
cacheDir string
configDir string
homeDir string
logDir string
dataDir string
pluginDir string
daemonSockPath string
daemonLockPath string
dfgetLockPath string
}

func New(homedir string) Dfpath {
return &dfpath{
dataDir:
pluginDir
cacheDir
configDir
homeDir
logDir
daemonSockPath
daemonLockPath
dfgetLockPath
// Option is a functional option for configuring the dfpath
type Option func(d *dfpath)

// WithWorkHome set the workhome directory
func WithWorkHome(dir string) Option {
return func(d *dfpath) {
d.workHome = dir
}
}

var (
DefaultDataDir = filepath.Join(WorkHome, "data")
DaemonSockPath = filepath.Join(WorkHome, "daemon.sock")
DaemonLockPath = filepath.Join(WorkHome, "daemon.lock")
DfgetLockPath = filepath.Join(WorkHome, "dfget.lock")
PluginsDir = filepath.Join(WorkHome, "plugins")
)
// WithCacheDir set the cache directory
func WithCacheDir(dir string) Option {
return func(d *dfpath) {
d.cacheDir = dir
}
}

func init() {
if err := fileutils.MkdirAll(WorkHome); err != nil {
panic(err)
// WithConfigDir set the config directory
func WithConfigDir(dir string) Option {
return func(d *dfpath) {
d.configDir = dir
}
}

if err := fileutils.MkdirAll(DefaultConfigDir); err != nil {
panic(err)
// WithLogDir set the log directory
func WithLogDir(dir string) Option {
return func(d *dfpath) {
d.configDir = dir
}
}

if err := fileutils.MkdirAll(DefaultCacheDir); err != nil {
panic(err)
// New returns a new dfpath interface
func New(options ...Option) (Dfpath, error) {
d := &dfpath{
workHome: DefaultWorkHome,
cacheDir: DefaultCacheDir,
configDir: DefaultConfigDir,
logDir: DefaultLogDir,
}

if err := fileutils.MkdirAll(LogDir); err != nil {
panic(err)
for _, opt := range options {
opt(d)
}

if err := fileutils.MkdirAll(DefaultDataDir); err != nil {
panic(err)
d.dataDir = filepath.Join(d.workHome, "data")
d.pluginDir = filepath.Join(d.workHome, "plugins")
d.daemonSockPath = filepath.Join(d.workHome, "daemon.sock")
d.daemonLockPath = filepath.Join(d.workHome, "daemon.lock")
d.dfgetLockPath = filepath.Join(d.workHome, "dfget.lock")

// Create directories
for _, dir := range []string{d.workHome, d.cacheDir, d.configDir, d.logDir, d.dataDir, d.pluginDir} {
if err := fileutils.MkdirAll(dir); err != nil {
return nil, err
}
}

return d, nil
}

func (d *dfpath) WorkHome() string {
return d.workHome
}

func (d *dfpath) CacheDir() string {
return d.cacheDir
}

func (d *dfpath) ConfigDir() string {
return d.configDir
}

func (d *dfpath) LogDir() string {
return d.logDir
}

func (d *dfpath) DataDir() string {
return d.dataDir
}

func (d *dfpath) PluginDir() string {
return d.pluginDir
}

func (d *dfpath) DaemonSockPath() string {
return d.daemonSockPath
}

func (d *dfpath) DaemonLockPath() string {
return d.daemonLockPath
}

func (d *dfpath) DfgetLockPath() string {
return d.dfgetLockPath
}
8 changes: 4 additions & 4 deletions internal/dfpath/dfpath_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"d7y.io/dragonfly/v2/pkg/basic"
)

var DefaultHomeDir = filepath.Join(basic.HomeDir, ".dragonfly")
var DefaultCacheDir = filepath.Join(DefaultHomeDir, "cache")
var DefaultConfigDir = filepath.Join(DefaultHomeDir, "config")
var DefaultLogDir = filepath.Join(DefaultHomeDir, "logs")
var DefaultWorkHome = filepath.Join(basic.HomeDir, ".dragonfly")
var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache")
var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
2 changes: 1 addition & 1 deletion internal/dfpath/dfpath_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package dfpath

var DefaultHomeDir = "/usr/local/dragonfly"
var DefaultWorkHome = "/usr/local/dragonfly"
var DefaultCacheDir = "/var/cache/dragonfly"
var DefaultConfigDir = "/etc/dragonfly"
var DefaultLogDir = "/var/log/dragonfly"

0 comments on commit b3bf9ad

Please sign in to comment.