Skip to content

Commit

Permalink
chore: switch to charmbracelet/log
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 21, 2023
1 parent 9d5e00f commit d644400
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/soft/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"log"
"runtime/debug"

"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,6 +49,6 @@ func init() {

func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
}
8 changes: 5 additions & 3 deletions cmd/soft/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"context"
"log"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/charmbracelet/log"

"github.com/charmbracelet/soft-serve/server"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/spf13/cobra"
Expand All @@ -23,7 +25,7 @@ var (
cfg := config.DefaultConfig()
s := server.NewServer(cfg)

log.Printf("Starting SSH server on %s:%d", cfg.BindAddr, cfg.Port)
log.Print("Starting SSH server", "addr", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port))

done := make(chan os.Signal, 1)
lch := make(chan error, 1)
Expand All @@ -36,7 +38,7 @@ var (
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done

log.Printf("Stopping SSH server on %s:%d", cfg.BindAddr, cfg.Port)
log.Print("Stopping SSH server", "addr", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions config/auth.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
"log"
"strings"

"github.com/charmbracelet/log"

gm "github.com/charmbracelet/wish/git"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
Expand All @@ -14,19 +15,19 @@ func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
go func() {
err := cfg.Reload()
if err != nil {
log.Printf("error reloading after push: %s", err)
log.Error("error reloading after push", "err", err)
}
if cfg.Cfg.Callbacks != nil {
cfg.Cfg.Callbacks.Push(repo)
}
r, err := cfg.Source.GetRepo(repo)
if err != nil {
log.Printf("error getting repo after push: %s", err)
log.Error("error getting repo after push", "err", err)
return
}
err = r.UpdateServerInfo()
if err != nil {
log.Printf("error updating server info after push: %s", err)
log.Error("error updating server info after push", "err", err)
}
}()
}
Expand Down Expand Up @@ -88,7 +89,7 @@ func (cfg *Config) accessForKey(repo string, pk ssh.PublicKey) gm.AccessLevel {
for _, k := range user.PublicKeys {
apk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(strings.TrimSpace(k)))
if err != nil {
log.Printf("error: malformed authorized key: '%s'", k)
log.Error("malformed authorized key", "key", k)
return gm.NoAccess
}
if ssh.KeysEqual(pk, apk) {
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"encoding/json"
"errors"
"io/fs"
"log"
"path/filepath"
"strings"
"sync"
"text/template"
"time"

"github.com/charmbracelet/log"

"golang.org/x/crypto/ssh"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -192,7 +193,7 @@ func (cfg *Config) Reload() error {
}
if err := cfg.readConfig(repo, &rc); err != nil {
if !errors.Is(err, ErrNoConfig) {
log.Printf("error reading config: %v", err)
log.Error("error reading config", "err", err)
}
continue
}
Expand All @@ -208,7 +209,7 @@ func (cfg *Config) Reload() error {
repo := r.Repo()
err = r.UpdateServerInfo()
if err != nil {
log.Printf("error updating server info for %s: %s", repo, err)
log.Error("error updating server info", "repo", repo, "err", err)
}
pat := "README*"
rp := ""
Expand Down
7 changes: 4 additions & 3 deletions config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package config

import (
"errors"
"log"
"os"
"path/filepath"
"sync"

"github.com/charmbracelet/log"

"github.com/charmbracelet/soft-serve/git"
"github.com/gobwas/glob"
"github.com/golang/groupcache/lru"
Expand Down Expand Up @@ -239,7 +240,7 @@ func (rs *RepoSource) LoadRepo(name string) error {
rp := filepath.Join(rs.Path, name)
r, err := rs.open(rp)
if err != nil {
log.Printf("error opening repository %q: %s", rp, err)
log.Error("error opening repository", "path", rp, "err", err)
return err
}
rs.repos[name] = r
Expand All @@ -254,7 +255,7 @@ func (rs *RepoSource) LoadRepos() error {
}
for _, de := range rd {
if !de.IsDir() {
log.Printf("warning: %q is not a directory", filepath.Join(rs.Path, de.Name()))
log.Warn("not a directory", "path", filepath.Join(rs.Path, de.Name()))
continue
}
err = rs.LoadRepo(de.Name())
Expand Down
17 changes: 9 additions & 8 deletions examples/setuid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"

"github.com/charmbracelet/log"

"github.com/charmbracelet/soft-serve/server"
"github.com/charmbracelet/soft-serve/server/config"
)
Expand All @@ -34,14 +35,14 @@ func main() {
// To listen on port 22 we need root privileges
ls, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Can't listen: %s", err)
log.Fatal("Can't listen", "err", err)
}
// We don't need root privileges any more
if err := syscall.Setgid(*gid); err != nil {
log.Fatalf("Setgid error: %s", err)
log.Fatal("Setgid error", "err", err)
}
if err := syscall.Setuid(*uid); err != nil {
log.Fatalf("Setuid error: %s", err)
log.Fatal("Setuid error", "err", err)
}
cfg := config.DefaultConfig()
cfg.Port = *port
Expand All @@ -50,19 +51,19 @@ func main() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

log.Printf("Starting SSH server on %s:%d", cfg.BindAddr, cfg.Port)
log.Print("Starting SSH server", "addr", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port))
go func() {
if err := s.Serve(ls); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
}()

<-done

log.Printf("Stopping SSH server on %s:%d", cfg.BindAddr, cfg.Port)
log.Print("Stopping SSH server", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
}
11 changes: 6 additions & 5 deletions server/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package config

import (
"log"
glog "log"
"path/filepath"

"github.com/caarlos0/env/v6"
"github.com/charmbracelet/log"
)

// Callbacks provides an interface that can be used to run callbacks on different events.
Expand All @@ -23,15 +24,15 @@ type Config struct {
RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
Callbacks Callbacks
ErrorLog *log.Logger
ErrorLog *glog.Logger
}

// DefaultConfig returns a Config with the values populated with the defaults
// or specified environment variables.
func DefaultConfig() *Config {
cfg := &Config{ErrorLog: log.Default()}
cfg := &Config{ErrorLog: log.StandardLog(log.StandardLogOption{ForceLevel: log.ErrorLevel})}
if err := env.Parse(cfg); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
if cfg.KeyPath == "" {
// NB: cross-platform-compatible path
Expand All @@ -47,7 +48,7 @@ func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
}

// WithErrorLogger sets the error logger for the configuration.
func (c *Config) WithErrorLogger(logger *log.Logger) *Config {
func (c *Config) WithErrorLogger(logger *glog.Logger) *Config {
c.ErrorLog = logger
return c
}
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package server
import (
"context"
"fmt"
"log"
"net"
"path/filepath"
"strings"

"github.com/charmbracelet/log"

appCfg "github.com/charmbracelet/soft-serve/config"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/charmbracelet/wish"
Expand Down Expand Up @@ -63,7 +64,7 @@ func NewServer(cfg *config.Config) *Server {
sh(s)
}
},
lm.Middleware(),
lm.MiddlewareWithLogger(log.StandardLog(log.StandardLogOption{ForceLevel: log.DebugLevel})),
),
}
s, err := wish.NewServer(
Expand All @@ -74,7 +75,7 @@ func NewServer(cfg *config.Config) *Server {
wish.WithMiddleware(mw...),
)
if err != nil {
log.Fatalln(err)
log.Fatal(err)
}
return &Server{
SSHServer: s,
Expand Down

0 comments on commit d644400

Please sign in to comment.