create_admin.go 924 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package cli // import "miniflux.app/cli"
  5. import (
  6. "fmt"
  7. "os"
  8. "miniflux.app/logger"
  9. "miniflux.app/model"
  10. "miniflux.app/storage"
  11. )
  12. func createAdmin(store *storage.Storage) {
  13. user := model.NewUser()
  14. user.Username = os.Getenv("ADMIN_USERNAME")
  15. user.Password = os.Getenv("ADMIN_PASSWORD")
  16. user.IsAdmin = true
  17. if user.Username == "" || user.Password == "" {
  18. user.Username, user.Password = askCredentials()
  19. }
  20. if err := user.ValidateUserCreation(); err != nil {
  21. fmt.Fprintf(os.Stderr, "%v\n", err)
  22. os.Exit(1)
  23. }
  24. if store.UserExists(user.Username) {
  25. logger.Info(`User %q already exists, skipping creation`, user.Username)
  26. return
  27. }
  28. if err := store.CreateUser(user); err != nil {
  29. fmt.Fprintf(os.Stderr, "%v\n", err)
  30. os.Exit(1)
  31. }
  32. }