Skip to content

Commit

Permalink
fix: yaml not working
Browse files Browse the repository at this point in the history
  • Loading branch information
pluveto committed Jan 24, 2024
1 parent e64965f commit d2e4950
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Run with given config file:

```shell
APP=flydav ARGS="-c /etc/flydav/flydav.toml" make run
```
```
7 changes: 7 additions & 0 deletions cmd/flydav/app/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (
)

func InitLogger(cnf conf.Log, verbose bool) {
if(verbose){
println("verbose mode enabled")
}
newLoggerCount := len(cnf.Stdout) + len(cnf.File)
if newLoggerCount != 0 {
for i := 0; i < newLoggerCount; i++ {
logger.AddLogger(logrus.New())
}
} else {
// if no logger configured, use default logger
if(verbose){
println("no logger configured, use default logger")
}
logger.SetOutput(os.Stdout)
return
}
Expand All @@ -28,6 +34,7 @@ func InitLogger(cnf conf.Log, verbose bool) {
// enable source code line numbers
logger.SetReportCaller(true)
} else {
println("Verbose mode disabled")
logger.SetLevel(levelToLogrusLevel(cnf.Level))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/flydav/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GetDefaultConf() Conf {
{
Username: "flydav",
PasswordHash: (func() string {
b, _ := bcrypt.GenerateFromPassword([]byte("flydav"), bcrypt.DefaultCost)
b, _ := bcrypt.GenerateFromPassword([]byte("flydavdefaultpassword"), bcrypt.DefaultCost)
return string(b)
})(),
PasswordCrypt: BcryptHash,
Expand Down
40 changes: 32 additions & 8 deletions cmd/flydav/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func main() {
var defaultConf = conf.GetDefaultConf()

args = loadArgsValid()
cnf = loadConfValid(args.Config, defaultConf, "config.toml")
if args.Verbose {
fmt.Printf("args: %+v\n", args)
}

cnf = loadConfValid(args.Verbose, args.Config, defaultConf, "config.toml")
if args.Verbose {
fmt.Printf("conf: %+v\n", cnf)
}
overrideConf(&cnf, args)
validateConf(&cnf)
app.InitLogger(cnf.Log, args.Verbose)
Expand Down Expand Up @@ -103,20 +110,29 @@ func getAppDir() string {
return filepath.Dir(dir)
}

func loadConfValid(path string, defaultConf conf.Conf, defaultConfPath string) conf.Conf {
func loadConfValid(verbose bool, path string, defaultConf conf.Conf, defaultConfPath string) conf.Conf {
if path == "" {
path = defaultConfPath
if verbose {
fmt.Println("no config file specified, using default config file: ", path)
}
}
// app executable dir + config.toml has the highest priority
preferredPath := filepath.Join(getAppDir(), path)
if _, err := os.Stat(preferredPath); err == nil {
path = preferredPath
if verbose {
fmt.Println("using config file: ", path)
}
}

err := decode(path, &defaultConf)
if err != nil {
logger.Warn("failed to load config file: ", err, " using default config")
if err != nil && verbose {
os.Stderr.WriteString(fmt.Sprintf("Failed to load config file: %s\n", err))
}else
{
logger.WithField("conf", &defaultConf).Debug("configuration loaded")
}
logger.WithField("conf", &defaultConf).Debug("configuration loaded")
return defaultConf
}

Expand All @@ -127,10 +143,18 @@ func decode(path string, conf *conf.Conf) (error) {
}

switch ext {
case ".toml":
case "toml":
_, err = toml.DecodeFile(path, conf)
case ".yaml", ".yml":
err = yaml.Unmarshal([]byte(path), conf)
case "yaml", "yml":
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read config file: %s", err)
}

err = yaml.Unmarshal([]byte(content), conf)
if err != nil {
return fmt.Errorf("failed to parse config file: %s", err)
}
default:
err = fmt.Errorf("unsupported config file extension: %s", ext)
}
Expand Down
2 changes: 1 addition & 1 deletion conf/config.default.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
server:
host: 0.0.0.0
port: 7086
port: 7000
path: /webdav
fs_dir: /tmp/flydav
ui:
Expand Down
2 changes: 0 additions & 2 deletions scripts/installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,3 @@ echo "Starting systemd service"
must_run systemctl start flydav.service

echo "Installation complete"


2 changes: 1 addition & 1 deletion scripts/ui_installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ must_run unzip /tmp/flydav_install/flydav-ui-dist.zip -d $UI_INSTALL_DIR
echo "Flydav UI is installed to $UI_INSTALL_DIR"
echo "Edit your flydav config file and restart to enable UI"

clean_up
clean_up

0 comments on commit d2e4950

Please sign in to comment.