-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
130 lines (108 loc) · 2.96 KB
/
main.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"errors"
"os"
"time"
"github.com/alexferl/golib/config"
"github.com/alexferl/golib/database/mongodb"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/alexferl/echo-boilerplate/data"
"github.com/alexferl/echo-boilerplate/mappers"
"github.com/alexferl/echo-boilerplate/models"
"github.com/alexferl/echo-boilerplate/services"
)
type Config struct {
Config *config.Config
MongoDB *mongodb.Config
Super *Super
}
type Super struct {
Email string
Name string
Password string
Username string
}
func New() *Config {
return &Config{
Config: config.New("APP"),
MongoDB: mongodb.DefaultConfig,
Super: &Super{
Email: "super@example.com",
Name: "Super",
Password: "",
Username: "super",
},
}
}
const (
SuperEmail = "email"
SuperName = "name"
SuperPassword = "password"
SuperUsername = "username"
)
func (c *Config) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Super.Email, SuperEmail, c.Super.Email, "Superuser email")
fs.StringVar(&c.Super.Name, SuperName, c.Super.Name, "Superuser display name")
fs.StringVar(&c.Super.Password, SuperPassword, c.Super.Password, "Superuser password")
fs.StringVar(&c.Super.Username, SuperUsername, c.Super.Username, "Superuser name")
}
func (c *Config) BindFlags() {
c.addFlags(pflag.CommandLine)
c.MongoDB.BindFlags(pflag.CommandLine)
err := c.Config.BindFlags()
if err != nil {
log.Fatal().Err(err).Msg("failed binding flags")
}
if viper.GetString(SuperPassword) == "" {
log.Fatal().Msg("password is unset!")
}
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
c := New()
c.BindFlags()
client, err := data.MewMongoClient()
if err != nil {
log.Fatal().Err(err).Msg("failed creating mongo client")
}
mapper := mappers.NewUser(client)
svc := services.NewUser(mapper)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := svc.FindOneByEmailOrUsername(ctx, viper.GetString(SuperEmail), viper.GetString(SuperUsername))
if res != nil {
log.Fatal().Msg("username or email already in-use")
}
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.NotExist {
email := viper.GetString(SuperEmail)
name := viper.GetString(SuperName)
username := viper.GetString(SuperUsername)
log.Info().
Str("name", name).
Str("username", username).
Str("email", email).
Msg("creating superuser")
user := models.NewUserWithRole(email, username, models.SuperRole)
user.Name = name
err = user.SetPassword(viper.GetString(SuperPassword))
if err != nil {
log.Fatal().Err(err).Msg("failed setting superuser password")
}
_, err = svc.Create(ctx, user)
if err != nil {
log.Fatal().Err(err).Msg("failed creating superuser")
}
log.Info().Msg("done")
return
}
}
log.Fatal().Err(err).Msg("failed getting superuser")
}
}