aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/create_admin.go
blob: 9af8df3383f3bdafe1422760cb14b0abb2bb54dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)
	}
}