create_admin.go 947 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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/config"
  9. "miniflux.app/logger"
  10. "miniflux.app/model"
  11. "miniflux.app/storage"
  12. )
  13. func createAdmin(store *storage.Storage) {
  14. user := model.NewUser()
  15. user.Username = config.Opts.AdminUsername()
  16. user.Password = config.Opts.AdminPassword()
  17. user.IsAdmin = true
  18. if user.Username == "" || user.Password == "" {
  19. user.Username, user.Password = askCredentials()
  20. }
  21. if err := user.ValidateUserCreation(); err != nil {
  22. fmt.Fprintf(os.Stderr, "%v\n", err)
  23. os.Exit(1)
  24. }
  25. if store.UserExists(user.Username) {
  26. logger.Info(`User %q already exists, skipping creation`, user.Username)
  27. return
  28. }
  29. if err := store.CreateUser(user); err != nil {
  30. fmt.Fprintf(os.Stderr, "%v\n", err)
  31. os.Exit(1)
  32. }
  33. }