Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Add version field and debug mode flag #11

Merged
merged 2 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"
"os/signal"
"strings"
Expand All @@ -15,35 +14,48 @@ import (
"github.com/beyondstorage/beyond-ftp/client"
"github.com/beyondstorage/beyond-ftp/config"
"github.com/beyondstorage/beyond-ftp/constants"
"github.com/beyondstorage/beyond-ftp/pprof"
"github.com/beyondstorage/beyond-ftp/server"
"github.com/beyondstorage/beyond-ftp/utils"
)

var (
versionFlag bool
cfgFileFlag string
cfgFileFlag string
cfgDebugFlag bool

clientCount int32
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: constants.Name,
Short: "A FTP server that persists all data to Beyond Storage.",
Long: "A FTP server that persists all data to Beyond Storage.",
Version: constants.Version,
Use: constants.Name,
Short: "A FTP server that persists all data to Beyond Storage.",
Long: "A FTP server that persists all data to Beyond Storage.",
Run: func(cmd *cobra.Command, args []string) {
if versionFlag {
fmt.Fprintf(os.Stdout, "BeyondFTP version %s\n", constants.Version)
return
if cfgDebugFlag {
pprof.StartPP()
}

c := config.LoadConfigFromFilepath(cfgFileFlag)
s, err := server.NewFTPServer(c)
check.ErrorForExit("server init error", err)
StartServer(s)
},
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
check.ErrorForExit(constants.Name, err)
}
xiongjiwei marked this conversation as resolved.
Show resolved Hide resolved
}

func init() {
RootCmd.PersistentFlags().BoolVarP(&cfgDebugFlag, "debug", "d", false, "Enter debug mode")
RootCmd.PersistentFlags().StringVarP(&cfgFileFlag, "config", "c", "./config/config.example.toml", "Specify config file")
}

func StartServer(s server.Server) {
s.Start()
go signalHandler(s)
Expand Down Expand Up @@ -76,20 +88,6 @@ func serveClient(s server.Server, id, addr string, connection utils.Conn) {
utils.Logger.Infof("FTP Client disconnected: ftp.disconnected, id: %s, RemoteAddr: %v, Total: %d", id, addr, clientCount)
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
check.ErrorForExit(constants.Name, err)
os.Exit(-1)
}
}

func init() {
RootCmd.PersistentFlags().BoolVarP(&versionFlag, "version", "v", false, "Show version")
RootCmd.PersistentFlags().StringVarP(&cfgFileFlag, "config", "c", "./config/config.example.toml", "Specify config file")
}

func signalHandler(s server.Server) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
Expand Down
2 changes: 1 addition & 1 deletion constants/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants

// Version number string.
const Version = "0.2.0"
const Version = "0.0.1"
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package main

import (
"github.com/beyondstorage/beyond-ftp/cmd"
"github.com/beyondstorage/beyond-ftp/pprof"
)

func main() {
pprof.StartPP()
cmd.Execute()
}