From f62f3b8a021f646c95960f1b7288f733fb98fe58 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 20 Oct 2020 11:46:44 +0800 Subject: [PATCH] define HomeDir and SourceDir helper functions --- cmd/migrate.go | 11 ++++------- pkg/bbgo/home.go | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 pkg/bbgo/home.go diff --git a/cmd/migrate.go b/cmd/migrate.go index 41a55724d..dbb4924c6 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -11,6 +11,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + + "github.com/c9s/bbgo/pkg/bbgo" ) func init() { @@ -38,13 +40,8 @@ var MigrateCmd = &cobra.Command{ return err } - homeDir, err := os.UserHomeDir() - if err != nil { - return err - } - - dotDir := path.Join(homeDir, ".bbgo") - sourceDir := path.Join(dotDir, "source") + dotDir := bbgo.HomeDir() + sourceDir := bbgo.SourceDir() migrationDir := path.Join(sourceDir, "migrations") log.Infof("creating dir: %s", dotDir) diff --git a/pkg/bbgo/home.go b/pkg/bbgo/home.go new file mode 100644 index 000000000..a8a1a3a5a --- /dev/null +++ b/pkg/bbgo/home.go @@ -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 +}