define HomeDir and SourceDir helper functions

This commit is contained in:
c9s 2020-10-20 11:46:44 +08:00
parent fc687f3174
commit f62f3b8a02
2 changed files with 31 additions and 7 deletions

View File

@ -11,6 +11,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/bbgo"
) )
func init() { func init() {
@ -38,13 +40,8 @@ var MigrateCmd = &cobra.Command{
return err return err
} }
homeDir, err := os.UserHomeDir() dotDir := bbgo.HomeDir()
if err != nil { sourceDir := bbgo.SourceDir()
return err
}
dotDir := path.Join(homeDir, ".bbgo")
sourceDir := path.Join(dotDir, "source")
migrationDir := path.Join(sourceDir, "migrations") migrationDir := path.Join(sourceDir, "migrations")
log.Infof("creating dir: %s", dotDir) log.Infof("creating dir: %s", dotDir)

27
pkg/bbgo/home.go Normal file
View File

@ -0,0 +1,27 @@
package bbgo
import (
"os"
"path"
)
func SourceDir() string {
home := HomeDir()
return path.Join(home, "source")
}
func HomeDir() string {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
dotDir := path.Join(homeDir, ".bbgo")
_, err = os.Stat(dotDir)
if err != nil {
_ = os.Mkdir(dotDir, 0777)
}
return dotDir
}