shiori/internal/cmd/root.go

133 lines
2.7 KiB
Go
Raw Normal View History

2019-05-21 11:31:40 +08:00
package cmd
import (
2019-06-09 15:54:07 +08:00
"crypto/tls"
2019-05-22 00:24:11 +08:00
"net/http"
2019-06-09 15:54:07 +08:00
"net/http/cookiejar"
2019-08-09 11:19:43 +08:00
"os"
fp "path/filepath"
2019-05-22 00:24:11 +08:00
"time"
"github.com/go-shiori/shiori/internal/database"
2019-08-09 11:19:43 +08:00
apppaths "github.com/muesli/go-app-paths"
2019-05-21 11:31:40 +08:00
"github.com/spf13/cobra"
)
2019-05-22 00:24:11 +08:00
var (
2019-08-09 11:19:43 +08:00
db database.DB
dataDir string
httpClient *http.Client
developmentMode bool
2019-05-22 00:24:11 +08:00
)
2019-06-09 15:54:07 +08:00
func init() {
jar, _ := cookiejar.New(nil)
httpClient = &http.Client{
Timeout: time.Minute,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Jar: jar,
}
}
2019-05-21 11:31:40 +08:00
// ShioriCmd returns the root command for shiori
func ShioriCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "shiori",
Short: "Simple command-line bookmark manager built with Go",
}
2019-08-09 11:19:43 +08:00
rootCmd.PersistentPreRun = preRunRootHandler
rootCmd.PersistentFlags().Bool("portable", false, "run shiori in portable mode")
2019-05-21 11:31:40 +08:00
rootCmd.AddCommand(
addCmd(),
printCmd(),
updateCmd(),
deleteCmd(),
openCmd(),
importCmd(),
exportCmd(),
pocketCmd(),
serveCmd(),
)
return rootCmd
}
2019-08-09 11:19:43 +08:00
func preRunRootHandler(cmd *cobra.Command, args []string) {
// Read flag
var err error
portableMode, _ := cmd.Flags().GetBool("portable")
// Get and create data dir
dataDir, err = getDataDir(portableMode)
if err != nil {
cError.Printf("Failed to get data dir: %v\n", err)
os.Exit(1)
}
err = os.MkdirAll(dataDir, os.ModePerm)
if err != nil {
cError.Printf("Failed to create data dir: %v\n", err)
os.Exit(1)
}
// Open database
2019-08-10 09:13:13 +08:00
db, err = openDatabase()
2019-08-09 11:19:43 +08:00
if err != nil {
cError.Printf("Failed to open database: %v\n", err)
os.Exit(1)
}
}
func getDataDir(portableMode bool) (string, error) {
// If in portable mode, uses directory of executable
if portableMode {
exePath, err := os.Executable()
if err != nil {
return "", err
}
exeDir := fp.Dir(exePath)
return fp.Join(exeDir, "shiori-data"), nil
}
if developmentMode {
return "dev-data", nil
}
// Try to look at environment variables
dataDir, found := os.LookupEnv("SHIORI_DIR")
if found {
return dataDir, nil
}
// Try to use platform specific app path
userScope := apppaths.NewScope(apppaths.User, "shiori", "shiori")
dataDir, err := userScope.DataDir()
if err == nil {
return dataDir, nil
}
// When all fail, use current working directory
return ".", nil
}
2019-08-10 09:13:13 +08:00
func openDatabase() (database.DB, error) {
// Check if it uses MySQL
if dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms == "mysql" {
user, _ := os.LookupEnv("SHIORI_MYSQL_USER")
password, _ := os.LookupEnv("SHIORI_MYSQL_PASS")
dbName, _ := os.LookupEnv("SHIORI_MYSQL_NAME")
return database.OpenMySQLDatabase(user, password, dbName)
}
// If not, just uses SQLite
dbPath := fp.Join(dataDir, "shiori.db")
return database.OpenSQLiteDatabase(dbPath)
}