Skip to content

Commit

Permalink
Normalise paths
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 3, 2024
1 parent 96c29f3 commit 20ee952
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/bahner/go-ma"
Expand All @@ -15,7 +16,7 @@ import (

const (
NAME string = "go-ma-actor"
VERSION string = "v0.2.2"
VERSION string = "v0.2.3"
ENV_PREFIX string = "GO_MA_ACTOR"
fakeActorIdentity string = "NO_DEFAULT_ACTOR_IDENITY"
fakeNodeIdentity string = "NO_DEFAULT_NODE_IDENITY"
Expand Down Expand Up @@ -164,7 +165,7 @@ func configFile() string {
filename = configHome + configName() + ".yaml"
}

return filename
return filepath.FromSlash(filepath.Clean(filename))

}

Expand Down
3 changes: 2 additions & 1 deletion config/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"fmt"
"path/filepath"
"sync"

"github.com/bahner/go-ma-actor/config"
Expand Down Expand Up @@ -82,7 +83,7 @@ func dbfile() string {
return ""
}

return path
return filepath.FromSlash(filepath.Clean(path))

}

Expand Down
38 changes: 25 additions & 13 deletions config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/spf13/pflag"
Expand All @@ -12,7 +13,8 @@ import (
)

const (
defaultLogLevel string = "info"
defaultLogLevel string = "info"
logFilePerm os.FileMode = 0640
)

var defaultLogfile string = dataHome + defaultActor + ".log"
Expand All @@ -37,18 +39,17 @@ func InitLogging() {
os.Exit(64) // EX_USAGE
}
log.SetLevel(ll)
logfile := viper.GetString("log.file")
logfile, err := getLogFile()
if err != nil {
fmt.Println(err)
os.Exit(73) // EX_CANTCREAT
}
if logfile == "STDERR" {
log.SetOutput(os.Stderr)
} else if logfile == "STDOUT" {
log.SetOutput(os.Stdout)
} else {
logfile, err = homedir.Expand(logfile)
if err != nil {
fmt.Println(err)
os.Exit(73) // EX_CANTCREAT
}
file, err := os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
file, err := os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, logFilePerm)
if err != nil {
fmt.Println(err)
os.Exit(73) // EX_CANTCREAT
Expand All @@ -61,10 +62,21 @@ func InitLogging() {

}

func GetLogLevel() string {
return viper.GetString("log.level")
}
func getLogFile() (string, error) {
lf := viper.GetString("log.file")

func GetLogFile() string {
return viper.GetString("log.file")
if lf == "STDERR" || lf == "STDOUT" {
return lf, nil
}

lf, err := homedir.Expand(lf)
if err != nil {
return "", err
}

lf, err = filepath.Abs(lf)
if err != nil {
return "", err
}
return filepath.FromSlash(filepath.Clean(lf)), nil
}

0 comments on commit 20ee952

Please sign in to comment.