/
main.go
111 lines (101 loc) · 2.41 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
package main
import (
"context"
"fmt"
"os"
"errors"
githubl "github.com/google/go-github/github"
"github.com/saromanov/born/provider"
"github.com/saromanov/born/provider/github"
"github.com/saromanov/born/server"
"github.com/saromanov/born/store"
"github.com/saromanov/born/store/postgresql"
"github.com/urfave/cli"
"golang.org/x/oauth2"
)
var (
errDBCredsIsNotDefined = errors.New("db: username and password is not defined")
errNoGithubToken = errors.New("github token is not defined")
)
var flags = []cli.Flag{
cli.StringFlag{
EnvVar: "GITHUB_TOKEN",
Name: "github-token",
Usage: "using of github token for test",
},
cli.StringFlag{
EnvVar: "BORN_SERVER",
Name: "born-server",
Usage: "server of the born",
},
cli.StringFlag{
EnvVar: "DB_USERNAME",
Name: "db-username",
Usage: "databse username",
},
cli.StringFlag{
EnvVar: "DB_PASSWORD",
Name: "db-password",
Usage: "databse password",
},
}
// setupProvider provides initialization of provider
// at this moment, its support only Github
func setupProvider(c *cli.Context) (provider.Provider, error) {
token := c.String("github-token")
if token == "" {
return nil, errNoGithubToken
}
ctx := context.Background()
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, sts)
client := githubl.NewClient(tc)
return github.New(github.Options{
Client: client,
})
}
// setupStore provides initialization of db store
// at this moment its support Postgesql
func setupStore(c *cli.Context) (store.Store, error) {
username := c.String("db-username")
password := c.String("db-password")
if username == "" && password == "" {
return nil, errDBCredsIsNotDefined
}
client, err := postgresql.New(&store.Options{
Username: c.String("db-username"),
Password: c.String("db-password"),
})
if err != nil {
return nil, err
}
return client, nil
}
//setupServer provides create of the new server
func setupServer(p provider.Provider, s store.Store, c *cli.Context) {
server.Setup(p, s, c.String("born-server"))
}
func run(c *cli.Context) {
provider, err := setupProvider(c)
if err != nil {
panic(err)
}
fmt.Println(provider)
store, err := setupStore(c)
if err != nil {
panic(err)
}
fmt.Println(store)
setupServer(provider, store, c)
}
func main() {
app := cli.NewApp()
app.Name = "born"
app.Flags = flags
app.Action = run
if err := app.Run(os.Args); err != nil {
panic(err)
}
}