This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
bigfile.go
131 lines (121 loc) · 3.36 KB
/
bigfile.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
// Copyright 2019 The bigfile Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package main is the entry for the whole program
package main
import (
"fmt"
"os"
"sort"
"time"
cmdApp "github.com/bigfile/bigfile/artisan/app"
"github.com/bigfile/bigfile/artisan/ftp"
"github.com/bigfile/bigfile/artisan/http"
"github.com/bigfile/bigfile/artisan/migrate"
"github.com/bigfile/bigfile/artisan/multi"
"github.com/bigfile/bigfile/artisan/rpc"
"github.com/bigfile/bigfile/config"
"github.com/bigfile/bigfile/log"
"github.com/gookit/color"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"gopkg.in/urfave/cli.v2"
)
var (
app = cli.App{
Name: "bigfile",
Version: "1.0.10",
Compiled: time.Now(),
Authors: []*cli.Author{
{
Name: "bigfile team",
Email: "bigfilefu@gmail.com",
},
},
Copyright: "copyright 2019 The bigfile authors",
Usage: "develop toolkit and program entry",
UsageText: "bigfile [global options] command [command options] [arguments...]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "system config file,search path: .bigfile.yaml, $HOME/.bigfile.yaml, /etc/bigfile/.bigfile.yaml",
Aliases: []string{"c"},
EnvVars: []string{"BIGFILE_CONFIG"},
},
&cli.StringFlag{
Name: "db-host",
Usage: "set the database host",
Value: "127.0.0.1",
},
&cli.UintFlag{
Name: "db-port",
Usage: "set the database port",
Value: 3306,
},
&cli.StringFlag{
Name: "db-user",
Usage: "set the database user",
Value: "root",
},
&cli.StringFlag{
Name: "db-pass",
Usage: "set the database password",
Value: "root",
},
&cli.StringFlag{
Name: "db-name",
Usage: "set the database name",
Value: "bigfile",
},
},
Before: func(ctx *cli.Context) error {
var (
err error
cfgFile = ctx.String("config")
userHome string
)
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName(".bigfile")
viper.AddConfigPath(".")
if userHome, err = homedir.Dir(); err != nil {
return err
}
viper.AddConfigPath(userHome)
viper.AddConfigPath("/etc/bigfile")
}
if err = viper.ReadInConfig(); err == nil {
if err = viper.Unmarshal(config.DefaultConfig); err != nil {
return err
}
if _, err = log.NewLogger(&config.DefaultConfig.Log); err != nil {
return err
}
} else {
color.Warn.Println(err.Error())
config.DefaultConfig.Database.Host = ctx.String("db-host")
config.DefaultConfig.Database.Port = uint32(ctx.Uint("db-port"))
config.DefaultConfig.Database.User = ctx.String("db-user")
config.DefaultConfig.Database.Password = ctx.String("db-pass")
config.DefaultConfig.Database.DBName = ctx.String("db-name")
}
return nil
},
}
)
func main() {
var commands []*cli.Command
commands = append(commands, migrate.Commands...)
commands = append(commands, cmdApp.Commands...)
commands = append(commands, http.Commands...)
commands = append(commands, rpc.Commands...)
commands = append(commands, ftp.Commands...)
commands = append(commands, multi.Commands...)
app.Commands = commands
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}