refactor: perform more clean up in codebase
Some checks failed
Go CI Pipeline / ci (push) Has been cancelled
Some checks failed
Go CI Pipeline / ci (push) Has been cancelled
This commit is contained in:
parent
20b4b7ba2d
commit
127018b565
4 changed files with 34 additions and 19 deletions
4
cmd.go
4
cmd.go
|
|
@ -79,10 +79,10 @@ Use --background (-b) to keep tunnels running in the background without auto-con
|
|||
fmt.Println("INFO: Background shutdown complete.")
|
||||
|
||||
} else {
|
||||
fmt.Println("INFO: Automatically connecting to workstation via SSH tunnel...")
|
||||
fmt.Println("Automatically connecting to workstation via SSH tunnel...")
|
||||
a.connectToWorkstation()
|
||||
|
||||
fmt.Println("INFO: Workstation SSH session finished.")
|
||||
fmt.Println("Workstation SSH session finished.")
|
||||
slog.Info("Foreground session ended, cleanup via defer sshCon.Close() will run.")
|
||||
if err := a.timeStore.StopTracking(); err != nil {
|
||||
slog.Warn(fmt.Sprintf("Failed to stop time tracking: %v", err))
|
||||
|
|
|
|||
22
forwarder.go
22
forwarder.go
|
|
@ -31,7 +31,7 @@ func (pf *PortForwarder) forward() error {
|
|||
localAddr := "127.0.0.1:" + pf.localPort
|
||||
remoteAddr := net.JoinHostPort(pf.remoteHost, pf.remotePort)
|
||||
|
||||
pf.logf("INFO", "Starting port forwarder: local %s -> remote %s (via SSH)", localAddr, remoteAddr)
|
||||
pf.logf("INFO", "Starting port forwarder: local -> remote (via SSH)", "Local Address", localAddr, "Remote Address", remoteAddr)
|
||||
|
||||
listener, err := net.Listen("tcp", localAddr)
|
||||
if err != nil {
|
||||
|
|
@ -39,20 +39,20 @@ func (pf *PortForwarder) forward() error {
|
|||
return fmt.Errorf("failed to listen on %s: %w", localAddr, err)
|
||||
}
|
||||
defer listener.Close()
|
||||
pf.logf("INFO", fmt.Sprintf("Listener active on %s", localAddr))
|
||||
pf.logf("INFO", "Listener active", "Local Address", localAddr)
|
||||
|
||||
for {
|
||||
localConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "use of closed network connection" {
|
||||
pf.logf("INFO", "Listener on %s closed, stopping forwarder.", localAddr)
|
||||
pf.logf("INFO", "Listener closed, stopping forwarder.", "Local Address", localAddr)
|
||||
return nil
|
||||
}
|
||||
pf.logf("ERROR", "Failed to accept incoming connection on %s: %v", localAddr, err)
|
||||
pf.logf("ERROR", "Failed to accept incoming connection:", "Local Address", localAddr, "Error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pf.logf("INFO", fmt.Sprintf("Accepted connection from %s on %s", localConn.RemoteAddr(), localAddr))
|
||||
pf.logf("INFO", "Accepted connection:", "Remote Address", localConn.RemoteAddr(), "Local Address", localAddr)
|
||||
go pf.handleConnection(localConn, remoteAddr)
|
||||
}
|
||||
}
|
||||
|
|
@ -60,14 +60,14 @@ func (pf *PortForwarder) forward() error {
|
|||
func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string) {
|
||||
defer localConn.Close()
|
||||
|
||||
pf.logf("INFO", "Dialing remote host %s via SSH tunnel for %s", remoteAddr, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Dialing remote host via SSH tunnel", "Local Address", remoteAddr, "Remote Address", localConn.RemoteAddr())
|
||||
remoteConn, err := pf.sshCon.Dial("tcp", remoteAddr)
|
||||
if err != nil {
|
||||
pf.logf("ERROR", "Failed to dial remote host %s via SSH: %v", remoteAddr, err)
|
||||
pf.logf("ERROR", "Failed to dial remote host via SSH:", "Remote Address", remoteAddr, "Error:", err)
|
||||
return
|
||||
}
|
||||
defer remoteConn.Close()
|
||||
pf.logf("INFO", "Connection to %s established. Starting data copy.", remoteAddr)
|
||||
pf.logf("INFO", "Connection established. Starting data copy.", "Remote Address", remoteAddr)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
|
@ -78,7 +78,7 @@ func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string)
|
|||
bytesCopied, err := io.Copy(localConn, remoteConn)
|
||||
if err != nil {
|
||||
}
|
||||
pf.logf("INFO", "Finished copying remote->local (%d bytes) for %s", bytesCopied, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Finished copying remote->local", "Bytes copied", bytesCopied, "Remote Address", localConn.RemoteAddr())
|
||||
}()
|
||||
|
||||
go func() {
|
||||
|
|
@ -87,11 +87,11 @@ func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string)
|
|||
bytesCopied, err := io.Copy(remoteConn, localConn)
|
||||
if err != nil {
|
||||
}
|
||||
pf.logf("INFO", "Finished copying local->remote (%d bytes) for %s", bytesCopied, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Finished copying local->remote", "Bytes copied", bytesCopied, "Remote Address", localConn.RemoteAddr())
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
pf.logf("INFO", "Closing forwarded connection for %s", localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Closing forwarded connection", "Remote Address", localConn.RemoteAddr())
|
||||
}
|
||||
|
||||
func (pf *PortForwarder) logf(level, format string, v ...any) {
|
||||
|
|
|
|||
21
main.go
21
main.go
|
|
@ -1,20 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
slog.Error("Cant get user config dir")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filepath.Join(configDir, "work", "workctl.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(file, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
app, err := NewApp()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Unable to setup application: %v", err))
|
||||
slog.Error("Unable to setup application", "Error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() {
|
||||
if err := app.Close(); err != nil {
|
||||
slog.Error(fmt.Sprintf("Failed to close application resources: %v", err))
|
||||
slog.Error("Failed to close application resources", "Error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
|||
6
store.go
6
store.go
|
|
@ -37,7 +37,7 @@ func NewTimeStore(cfg Config) (*TimeStore, error) {
|
|||
return nil, fmt.Errorf("could not determine database path: %w", err)
|
||||
}
|
||||
|
||||
slog.Info(fmt.Sprintf("Using database at: %s", dbPath))
|
||||
slog.Info("Using database at:", "Database Path", dbPath)
|
||||
|
||||
db, err := sql.Open("sqlite", fmt.Sprintf("%s?_pragma=journal_mode(WAL)", dbPath))
|
||||
if err != nil {
|
||||
|
|
@ -66,7 +66,7 @@ func NewTimeStore(cfg Config) (*TimeStore, error) {
|
|||
|
||||
createIndexSQL := `CREATE INDEX IF NOT EXISTS idx_time_entries_start_time ON time_entries (start_time);`
|
||||
if _, err = db.Exec(createIndexSQL); err != nil {
|
||||
slog.Warn(fmt.Sprintf("Failed to create index on start_time: %v", err))
|
||||
slog.Warn("Failed to create index on start_time:", "Error:", err)
|
||||
}
|
||||
|
||||
return &TimeStore{db: db, dbPath: dbPath}, nil
|
||||
|
|
@ -89,7 +89,7 @@ func ensureDatabasePath(_ Config) (string, error) {
|
|||
|
||||
func (ts *TimeStore) Close() error {
|
||||
if ts.db != nil {
|
||||
slog.Info(fmt.Sprintf("Closing database connection to %s", ts.dbPath))
|
||||
slog.Info("Closing database connection", "Database Path", ts.dbPath)
|
||||
return ts.db.Close()
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue