Skip to content

Commit

Permalink
Refactor code to enhance organization and readability
Browse files Browse the repository at this point in the history
This commit refactors multiple parts of the codebase to
improve organization and readability. The changes include:
- Removing unused imports and variables
- Consistent naming conventions for variables and functions
- Restructuring code blocks for better logical flow
- Refactoring error handling to be more descriptive and consistent
- Updating comments for clarity and consistency

No functional changes are made in this commit.
  • Loading branch information
AbdeltwabMF committed Apr 5, 2024
1 parent 090713b commit 9bc886e
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 263 deletions.
8 changes: 4 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"log/slog"
_ "net/http/pprof"
"os"

"github.com/AbdeltwabMF/gomeet/configs"
Expand All @@ -12,16 +11,17 @@ import (
)

func main() {
file, err := configs.InitLogger()
f, err := configs.OpenLog(os.O_CREATE | os.O_TRUNC | os.O_WRONLY)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer file.Close()
defer f.Close()
configs.InitLogger(f)

cfg, err := configs.LoadConfig()
if err != nil {
slog.Error(err.Error())
slog.Error(err.Error(), slog.Any("func", configs.CallerInfo()))
os.Exit(1)
}

Expand Down
94 changes: 76 additions & 18 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,128 @@ package configs

import (
"encoding/json"
"io"
"log/slog"
"os"
"path"
"path/filepath"
"runtime"

"github.com/AbdeltwabMF/gomeet/internal/platform"
)

const LogFile = "log.txt"
const ConfigFile = "config.json"
const CredentialsFile = "credentials.json"
const TokenFile = "token.json"
const (
LogFile = "log.txt"
ConfigFile = "config.json"
CredentialsFile = "credentials.json"
TokenFile = "token.json"
)

type Config struct {
AutoStart bool `json:"auto_start"`
}

type Start struct {
type start struct {
Time string `json:"time"`
Days []string `json:"days"`
}

type Event struct {
Summary string `json:"summary"`
Url string `json:"url"`
Start Start `json:"start"`
Start start `json:"start"`
}

type Events struct {
Items []*Event `json:"events"`
}

func LoadConfig() (*Config, error) {
c, err := platform.ConfigDir()
type FuncInfo struct {
Name string
File string
Line int
}

// OpenLog opens the log file with the specified flags.
func OpenLog(flags int) (*os.File, error) {
d, err := platform.LogDir()
if err != nil {
return nil, err
}

file, err := os.OpenFile(filepath.Join(c, ConfigFile), os.O_CREATE|os.O_RDONLY, 0640)
return os.OpenFile(filepath.Join(d, LogFile), flags, 0640)
}

// OpenConfig opens the configuration file with the specified flags.
func OpenConfig(flags int) (*os.File, error) {
d, err := platform.ConfigDir()
if err != nil {
return nil, err
}
defer file.Close()

var config Config
if err := json.NewDecoder(file).Decode(&config); err != nil {
return os.OpenFile(filepath.Join(d, ConfigFile), flags, 0640)
}

// OpenCredentials opens the credentials file with the specified flags.
func OpenCredentials(flags int) (*os.File, error) {
d, err := platform.ConfigDir()
if err != nil {
return nil, err
}

return &config, nil
return os.OpenFile(filepath.Join(d, CredentialsFile), flags, 0600)
}

func InitLogger() (*os.File, error) {
lDir, err := platform.LogDir()
// OpenToken opens the token file with the specified flags.
func OpenToken(flags int) (*os.File, error) {
d, err := platform.ConfigDir()
if err != nil {
return nil, err
}

file, err := os.OpenFile(filepath.Join(lDir, LogFile), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
return os.OpenFile(filepath.Join(d, TokenFile), flags, 0600)
}

// LoadConfig loads the configuration from the configuration file.
func LoadConfig() (*Config, error) {
f, err := OpenConfig(os.O_RDONLY)
if err != nil {
return nil, err
}
defer f.Close()

cfg := new(Config)
if err := json.NewDecoder(f).Decode(cfg); err != nil {
return nil, err
}

return cfg, nil
}

logger := slog.New(slog.NewJSONHandler(file, nil))
// InitLogger initializes the default logger with the provided writer.
func InitLogger(w io.Writer) {
logger := slog.New(slog.NewTextHandler(w,
&slog.HandlerOptions{
Level: slog.LevelDebug,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Any(a.Key, a.Value.Time().Format("2006-01-02 15:04:05"))
}
return a
}},
))
slog.SetDefault(logger)
}

// CallerInfo returns information about the caller of the function where it's called.
func CallerInfo() FuncInfo {
pc, file, line, ok := runtime.Caller(1) // 0: Function info, 1: Caller info
if !ok {
return FuncInfo{}
}

file = path.Base(file)
name := path.Base(runtime.FuncForPC(pc).Name())

return file, nil
return FuncInfo{Name: name, File: file, Line: line}
}
Loading

0 comments on commit 9bc886e

Please sign in to comment.