From c208894bb6e9baa60a803ced27395ae9bc337c3e Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 07:14:58 +0700 Subject: [PATCH] Move opening database to main file --- cmd/root.go | 21 +++++---------------- main.go | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9e004d6d..51121593 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,19 +2,19 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" - db "github.com/RadhiFadlillah/shiori/database" - _ "github.com/mattn/go-sqlite3" + + "github.com/RadhiFadlillah/shiori/database" + "github.com/spf13/cobra" ) var ( // DB is database that used by this cli - DB db.Database + DB database.Database rootCmd = &cobra.Command{ Use: "shiori", - Short: "Simple command-line bookmark manager built with Go.", + Short: "Simple command-line bookmark manager built with Go", } ) @@ -26,14 +26,3 @@ func Execute() { os.Exit(1) } } - -func init () { - databasePath := "shiori.db" - if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { - databasePath = value - } - sqliteDB, err := db.OpenSQLiteDatabase(databasePath) - checkError(err) - - DB = sqliteDB -} \ No newline at end of file diff --git a/main.go b/main.go index 54181ae3..cdff3834 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,28 @@ package main import ( + "os" + "github.com/RadhiFadlillah/shiori/cmd" + db "github.com/RadhiFadlillah/shiori/database" + _ "github.com/mattn/go-sqlite3" ) func main() { + databasePath := "shiori.db" + if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { + databasePath = value + } + + sqliteDB, err := db.OpenSQLiteDatabase(databasePath) + checkError(err) + + cmd.DB = sqliteDB cmd.Execute() -} \ No newline at end of file +} + +func checkError(err error) { + if err != nil { + panic(err) + } +}