Skip to content

Commit e123ab7

Browse files
committed
ux: log username, os and date
1 parent 89768ca commit e123ab7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"fmt"
1010
"log"
1111
"os"
12+
"os/user"
1213
"path/filepath"
14+
"runtime"
15+
"strings"
1316
"syscall"
1417
"time"
1518

@@ -47,6 +50,8 @@ func main() {
4750
logFile := filepath.Join(rootDir, "ctRestClient.log")
4851

4952
appLogger := logger.NewLogger(logFile)
53+
logGeneralInfo(appLogger, getCurrentUserName(), getCurrentOSName(), getDate())
54+
5055
keepassCli, err := app.NewKeepassCli(keepassDbFilePath, keepassDbPassword, appLogger)
5156
if err != nil {
5257
log.Fatalf("Failed to initialize Keepass CLI: %v", err)
@@ -100,3 +105,49 @@ func getPasswordFromUser() (string, error) {
100105

101106
return string(password), nil
102107
}
108+
109+
func getCurrentUserName() string {
110+
currentUser, err := user.Current()
111+
if err != nil {
112+
return "unknown"
113+
}
114+
return currentUser.Username
115+
}
116+
117+
func getCurrentOSName() string {
118+
switch runtime.GOOS {
119+
case "darwin":
120+
return "macOS"
121+
case "linux":
122+
return "Linux"
123+
case "windows":
124+
return "Windows"
125+
default:
126+
return fmt.Sprintf("Unknown (%s)", runtime.GOOS)
127+
}
128+
}
129+
130+
func getDate() string {
131+
return time.Now().Format("2006-01-02 15:04:05")
132+
}
133+
134+
func logGeneralInfo(logger logger.Logger, user string, os string, date string) {
135+
boxLength := 70
136+
userInfo := fmt.Sprintf("User: '%s'", user)
137+
userInfoLength := len(userInfo)
138+
139+
osInfo := fmt.Sprintf("OS: '%s'", os)
140+
osInfoLength := len(osInfo)
141+
142+
dateInfo := fmt.Sprintf("Date: '%s'", date)
143+
dateInfoLength := len(dateInfo)
144+
145+
border := strings.Repeat("-", boxLength)
146+
147+
logger.Info("")
148+
logger.Info(fmt.Sprintf("+%s+", border))
149+
logger.Info(fmt.Sprintf("| %s "+strings.Repeat(" ", boxLength-userInfoLength-2)+"|", userInfo))
150+
logger.Info(fmt.Sprintf("| %s "+strings.Repeat(" ", boxLength-osInfoLength-2)+"|", osInfo))
151+
logger.Info(fmt.Sprintf("| %s "+strings.Repeat(" ", boxLength-dateInfoLength-2)+"|", dateInfo))
152+
logger.Info(fmt.Sprintf("+%s+", border))
153+
}

0 commit comments

Comments
 (0)