aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-01-02 22:04:48 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-01-02 22:04:48 -0800
commit320d1b016747ba4501da9417d9ce5f99368a5768 (patch)
tree1054d96afde6022951b76cc4a09b78e1e3f05058 /cli
parentc39f2e1a8d2de6d412bcc673d29eb0f7a2d1f5f7 (diff)
Refactor packages to have more idiomatic code base
Diffstat (limited to 'cli')
-rw-r--r--cli/cli.go58
-rw-r--r--cli/create_admin.go52
-rw-r--r--cli/flush_sessions.go20
-rw-r--r--cli/info.go18
4 files changed, 148 insertions, 0 deletions
diff --git a/cli/cli.go b/cli/cli.go
new file mode 100644
index 0000000..67131e6
--- /dev/null
+++ b/cli/cli.go
@@ -0,0 +1,58 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package cli
+
+import (
+ "flag"
+ "fmt"
+
+ "github.com/miniflux/miniflux/config"
+ "github.com/miniflux/miniflux/daemon"
+ "github.com/miniflux/miniflux/storage"
+ "github.com/miniflux/miniflux/version"
+)
+
+// Parse parses command line arguments.
+func Parse() {
+ flagInfo := flag.Bool("info", false, "Show application information")
+ flagVersion := flag.Bool("version", false, "Show application version")
+ flagMigrate := flag.Bool("migrate", false, "Migrate database schema")
+ flagFlushSessions := flag.Bool("flush-sessions", false, "Flush all sessions (disconnect users)")
+ flagCreateAdmin := flag.Bool("create-admin", false, "Create admin user")
+ flag.Parse()
+
+ cfg := config.NewConfig()
+ store := storage.NewStorage(
+ cfg.Get("DATABASE_URL", config.DefaultDatabaseURL),
+ cfg.GetInt("DATABASE_MAX_CONNS", config.DefaultDatabaseMaxConns),
+ )
+
+ if *flagInfo {
+ info()
+ return
+ }
+
+ if *flagVersion {
+ fmt.Println(version.Version)
+ return
+ }
+
+ if *flagMigrate {
+ store.Migrate()
+ return
+ }
+
+ if *flagFlushSessions {
+ flushSessions(store)
+ return
+ }
+
+ if *flagCreateAdmin {
+ createAdmin(store)
+ return
+ }
+
+ daemon.Run(cfg, store)
+}
diff --git a/cli/create_admin.go b/cli/create_admin.go
new file mode 100644
index 0000000..9af8df3
--- /dev/null
+++ b/cli/create_admin.go
@@ -0,0 +1,52 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package cli
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/miniflux/miniflux/model"
+ "github.com/miniflux/miniflux/storage"
+
+ "golang.org/x/crypto/ssh/terminal"
+)
+
+func askCredentials() (string, string) {
+ reader := bufio.NewReader(os.Stdin)
+
+ fmt.Print("Enter Username: ")
+ username, _ := reader.ReadString('\n')
+
+ fmt.Print("Enter Password: ")
+ bytePassword, _ := terminal.ReadPassword(0)
+
+ fmt.Printf("\n")
+ return strings.TrimSpace(username), strings.TrimSpace(string(bytePassword))
+}
+
+func createAdmin(store *storage.Storage) {
+ user := &model.User{
+ Username: os.Getenv("ADMIN_USERNAME"),
+ Password: os.Getenv("ADMIN_PASSWORD"),
+ IsAdmin: true,
+ }
+
+ if user.Username == "" || user.Password == "" {
+ user.Username, user.Password = askCredentials()
+ }
+
+ if err := user.ValidateUserCreation(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ if err := store.CreateUser(user); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}
diff --git a/cli/flush_sessions.go b/cli/flush_sessions.go
new file mode 100644
index 0000000..06a56d0
--- /dev/null
+++ b/cli/flush_sessions.go
@@ -0,0 +1,20 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package cli
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/miniflux/miniflux/storage"
+)
+
+func flushSessions(store *storage.Storage) {
+ fmt.Println("Flushing all sessions (disconnect users)")
+ if err := store.FlushAllSessions(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}
diff --git a/cli/info.go b/cli/info.go
new file mode 100644
index 0000000..2ae1cef
--- /dev/null
+++ b/cli/info.go
@@ -0,0 +1,18 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package cli
+
+import (
+ "fmt"
+ "runtime"
+
+ "github.com/miniflux/miniflux/version"
+)
+
+func info() {
+ fmt.Println("Version:", version.Version)
+ fmt.Println("Build Date:", version.BuildDate)
+ fmt.Println("Go Version:", runtime.Version())
+}