From 320d1b016747ba4501da9417d9ce5f99368a5768 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 2 Jan 2018 22:04:48 -0800 Subject: Refactor packages to have more idiomatic code base --- cli/cli.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ cli/create_admin.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ cli/flush_sessions.go | 20 ++++++++++++++++++ cli/info.go | 18 ++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 cli/cli.go create mode 100644 cli/create_admin.go create mode 100644 cli/flush_sessions.go create mode 100644 cli/info.go (limited to 'cli') 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()) +} -- cgit v1.2.3