Skip to content

Commit

Permalink
Merge pull request #34 from Ryooooooga/log-env-var
Browse files Browse the repository at this point in the history
  • Loading branch information
relastle authored Dec 23, 2020
2 parents d6b7ccb + 507cb15 commit cb0dae2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/aop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
// MeasureElapsedTime measures elapsed time given
// a started time.
func MeasureElapsedTime(start time.Time, name string) {
logPath := os.ExpandEnv(defaultLogPath)
err := os.MkdirAll(path.Dir(logPath), os.ModePerm)
err := os.MkdirAll(path.Dir(LogPath), os.ModePerm)
if err != nil {
return
}
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
f, err := os.OpenFile(LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
Expand Down
12 changes: 9 additions & 3 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
defaultLogPath string = "${HOME}/.pmy/log.txt"
rulesPathVarName string = "PMY_RULE_PATH"
snippetPathVarName string = "PMY_SNIPPET_PATH"
logPathVarName string = "PMY_LOG_PATH"
tagDelimiterVarName string = "PMY_TAG_DELIMITER"
)

Expand All @@ -18,6 +19,8 @@ var (
RulePath string
// SnippetPath defines snippet root directry path
SnippetPath string
// LogPath is a path of the log file
LogPath string
// TagDelimiter defines delimiter string
// that divide `tag` and one line of source
TagDelimiter = "\\t"
Expand All @@ -27,9 +30,11 @@ var (
func setConfig(
target *string,
varName string,
defaultValue string,
) {
envVar, ok := os.LookupEnv(varName)
if !ok {
*target = defaultValue
return
}
*target = envVar
Expand All @@ -38,7 +43,8 @@ func setConfig(
// SetConfigs set all Pmy config variable from shell's
// environment variables.
func SetConfigs() {
setConfig(&RulePath, rulesPathVarName)
setConfig(&SnippetPath, snippetPathVarName)
setConfig(&TagDelimiter, tagDelimiterVarName)
setConfig(&RulePath, rulesPathVarName, "")
setConfig(&SnippetPath, snippetPathVarName, "")
setConfig(&LogPath, logPathVarName, os.ExpandEnv(defaultLogPath))
setConfig(&TagDelimiter, tagDelimiterVarName, "")
}

0 comments on commit cb0dae2

Please sign in to comment.