/
rootcmd.go
63 lines (50 loc) · 1.42 KB
/
rootcmd.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
package cmd
import (
"log"
"github.com/Duncodes/ecom/config"
"github.com/Duncodes/ecom/database"
"github.com/Duncodes/ecom/server"
"github.com/spf13/cobra"
)
var port string
var configfilepath string
func loadconfig() {
err := config.LoadConfig(configfilepath)
if err != nil {
log.Println(err)
log.Println("Falling back to default configuration")
}
}
func init() {
serveCmd.PersistentFlags().StringVarP(&config.Config.Port, "port", "p", "9200", "Port for the server")
RootCmd.PersistentFlags().StringVarP(&configfilepath, "config", "f", "config.json", "Ecom Config json file")
RootCmd.AddCommand(serveCmd)
RootCmd.AddCommand(version)
RootCmd.AddCommand(dbcmd)
}
// RootCmd base commnad for ecom
var RootCmd = &cobra.Command{
Use: "ecom",
Short: "Ecom is a ecommerce api ",
Long: `Ecom a less usefull ecommerce api server for anyone who loves to code`,
Run: func(cmd *cobra.Command, args []string) {
loadconfig()
database.InitDB()
/* get the port to use . if flag is provide use flag else use config
file
*/
server.StartServer(config.Config.Port)
},
}
// serveCmd used to start server
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Starts ecom server",
Long: `serve starts the ecom http serve ,
by default is uses port :9200 port can be provides via a flag,`,
Run: func(cmd *cobra.Command, args []string) {
loadconfig()
database.InitDB()
server.StartServer(config.Config.Port)
},
}