-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (92 loc) · 2.5 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
package main
import (
"os"
"strings"
"github.com/gdatasoftwareag/tftp/v2/pkg/logging"
"github.com/gdatasoftwareag/tftp/v2/pkg/tftp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
var (
rootCmd *cobra.Command
logger logging.Logger
cfg config
logLevel string
configFilePath string
developmentLogs bool
)
type config struct {
FSHandlerBaseDir string
TFTP tftp.Config
}
func main() {
rootCmd = &cobra.Command{
Use: "tftp",
Short: "Read only tftp server",
}
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "logging level to use")
rootCmd.PersistentFlags().BoolVarP(&developmentLogs, "development-logs", "d", false, "Enable development mode logs")
rootCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", "", "path to the config file to load")
rootCmd.PersistentPreRunE = rootCmdPersistentPreRunE
rootCmd.AddCommand(versionCmd, serveCmd)
if err := rootCmd.Execute(); err != nil {
panic(err.Error())
}
}
func rootCmdPersistentPreRunE(cmd *cobra.Command, args []string) (err error) {
if cmd.Name() == versionCmd.Name() {
return
}
if cfg, err = parseConfig(); err != nil {
return
}
logging.ConfigureLogging(
logging.ParseLevel(logLevel),
developmentLogs,
map[string]interface{}{
"cmd": cmd.Name(),
"args": args,
},
)
logger = logging.MustGetLogger()
logger.Info(
"Initiating TFTP server",
zap.String("git_tag", tftp.GitTag),
zap.String("git_commit", tftp.GitCommit),
zap.String("build_time", tftp.BuildTime),
)
return
}
func parseConfig() (cfg config, err error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if configFilePath == "" {
if dir, err := os.Getwd(); err == nil {
viper.AddConfigPath(dir)
}
viper.AddConfigPath("/etc/tftp")
} else {
viper.SetConfigFile(configFilePath)
}
viper.SetDefault("FSHandlerBaseDir", "/var/lib/tftpboot")
viper.SetDefault("tftp.ip", "0.0.0.0")
viper.SetDefault("tftp.port", 69)
viper.SetDefault("tftp.retransmissions", 3)
viper.SetDefault("tftp.maxparallelconnections", 10)
viper.SetDefault("tftp.filetransfertimeout", "0s")
viper.SetDefault("tftp.writetimeout", "5s")
viper.SetDefault("tftp.readtimeout", "5s")
viper.SetDefault("tftp.metrics.enabled", true)
viper.SetDefault("tftp.metrics.port", 9100)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("TFTP")
viper.AutomaticEnv()
if err = viper.ReadInConfig(); err != nil {
return
}
if err = viper.Unmarshal(&cfg); err != nil {
return
}
return
}