forked from ubccr/mokey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (116 loc) · 2.86 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
131
132
133
// Copyright 2015 mokey Authors. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/ubccr/mokey/server"
"github.com/ubccr/mokey/tools"
"github.com/urfave/cli"
)
func init() {
viper.SetConfigName("mokey")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/mokey/")
}
func main() {
app := cli.NewApp()
app.Name = "mokey"
app.Authors = []cli.Author{cli.Author{Name: "Andrew E. Bruno", Email: "aebruno2@buffalo.edu"}}
app.Usage = "mokey"
app.Version = "0.5.4"
app.Flags = []cli.Flag{
&cli.StringFlag{Name: "conf,c", Usage: "Path to conf file"},
&cli.BoolFlag{Name: "debug,d", Usage: "Print debug messages"},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
conf := c.GlobalString("conf")
if len(conf) > 0 {
viper.SetConfigFile(conf)
}
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("Failed reading config file - %s", err)
}
if !viper.IsSet("enc_key") || !viper.IsSet("auth_key") {
log.Fatal("Please ensure authentication and encryption keys are set")
}
return nil
}
app.Commands = []cli.Command{
{
Name: "server",
Usage: "Run http server",
Action: func(c *cli.Context) error {
err := server.Run()
if err != nil {
log.Fatal(err)
return cli.NewExitError(err, 1)
}
return nil
},
},
{
Name: "resetpw",
Usage: "Send reset password email",
Flags: []cli.Flag{
&cli.StringFlag{Name: "uid, u", Usage: "User id"},
},
Action: func(c *cli.Context) error {
uid := c.String("uid")
if len(uid) == 0 {
return cli.NewExitError(errors.New("Please provide a uid"), 1)
}
err := tools.SendResetPasswordEmail(uid)
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
},
{
Name: "verify-email",
Usage: "Re-send verify email",
Flags: []cli.Flag{
&cli.StringFlag{Name: "uid, u", Usage: "User id"},
},
Action: func(c *cli.Context) error {
uid := c.String("uid")
if len(uid) == 0 {
return cli.NewExitError(errors.New("Please provide a uid"), 1)
}
err := tools.SendVerifyEmail(uid)
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
},
{
Name: "status",
Usage: "Display token status for user",
Flags: []cli.Flag{
&cli.StringFlag{Name: "uid, u", Usage: "User id"},
},
Action: func(c *cli.Context) error {
uid := c.String("uid")
if len(uid) == 0 {
return cli.NewExitError(errors.New("Please provide a uid"), 1)
}
err := tools.Status(uid)
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}}
app.RunAndExitOnError()
}