switch to slog for logging
This commit is contained in:
parent
b0b7f7b36d
commit
8e350a7dac
|
@ -2,7 +2,6 @@ package fstree
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"syscall"
|
||||
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
|
@ -42,7 +41,7 @@ func newGroupNodeFromSource(source GroupSource, param *FSParam) (*groupNode, err
|
|||
func (n *groupNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
||||
groups, repositories, err := n.param.GitPlatform.FetchGroupContent(n.source.GetGroupID())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
n.param.logger.Error(err.Error())
|
||||
}
|
||||
|
||||
entries := make([]fuse.DirEntry, 0, len(groups)+len(repositories)+len(n.staticNodes))
|
||||
|
|
|
@ -2,7 +2,6 @@ package fstree
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"syscall"
|
||||
|
||||
"github.com/hanwen/go-fuse/v2/fs"
|
||||
|
@ -40,7 +39,7 @@ func (n *repositoryNode) Readlink(ctx context.Context) ([]byte, syscall.Errno) {
|
|||
// TODO: cleanup
|
||||
localRepositoryPath, err := n.param.GitClient.FetchLocalRepositoryPath(n.source)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
n.param.logger.Error(err.Error())
|
||||
}
|
||||
return []byte(localRepositoryPath), 0
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package fstree
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
@ -34,6 +35,7 @@ type FSParam struct {
|
|||
GitClient GitClient
|
||||
GitPlatform GitPlatform
|
||||
|
||||
logger *slog.Logger
|
||||
staticInoChan chan uint64
|
||||
}
|
||||
|
||||
|
@ -44,13 +46,14 @@ type rootNode struct {
|
|||
|
||||
var _ = (fs.NodeOnAdder)((*rootNode)(nil))
|
||||
|
||||
func Start(mountpoint string, mountoptions []string, param *FSParam, debug bool) error {
|
||||
fmt.Printf("Mounting in %v\n", mountpoint)
|
||||
func Start(logger *slog.Logger, mountpoint string, mountoptions []string, param *FSParam, debug bool) error {
|
||||
logger.Info("Mounting", "mountpoint", mountpoint)
|
||||
|
||||
opts := &fs.Options{}
|
||||
opts.MountOptions.Options = mountoptions
|
||||
opts.Debug = debug
|
||||
|
||||
param.logger = logger
|
||||
param.staticInoChan = make(chan uint64)
|
||||
root := &rootNode{
|
||||
param: param,
|
||||
|
@ -64,7 +67,7 @@ func Start(mountpoint string, mountoptions []string, param *FSParam, debug bool)
|
|||
}
|
||||
|
||||
signalChan := make(chan os.Signal)
|
||||
go signalHandler(signalChan, server)
|
||||
go signalHandler(logger, signalChan, server)
|
||||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// server.Serve() is already called in fs.Mount() so we shouldn't call it ourself. We wait for the server to terminate.
|
||||
|
@ -92,7 +95,7 @@ func (n *rootNode) OnAdd(ctx context.Context) {
|
|||
n.AddChild(groupName, persistentInode, false)
|
||||
}
|
||||
|
||||
fmt.Println("Mounted and ready to use")
|
||||
n.param.logger.Info("Mounted and ready to use")
|
||||
}
|
||||
|
||||
func staticInoGenerator(staticInoChan chan<- uint64) {
|
||||
|
@ -103,18 +106,18 @@ func staticInoGenerator(staticInoChan chan<- uint64) {
|
|||
}
|
||||
}
|
||||
|
||||
func signalHandler(signalChan <-chan os.Signal, server *fuse.Server) {
|
||||
func signalHandler(logger *slog.Logger, signalChan <-chan os.Signal, server *fuse.Server) {
|
||||
err := server.WaitMount()
|
||||
if err != nil {
|
||||
fmt.Printf("failed to start exit signal handler: %v\n", err)
|
||||
logger.Error("failed to start exit signal handler", "error", err)
|
||||
return
|
||||
}
|
||||
for {
|
||||
s := <-signalChan
|
||||
fmt.Printf("Caught %v: stopping\n", s)
|
||||
logger.Info("Caught signal", "signal", s)
|
||||
err := server.Unmount()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to unmount: %v\n", err)
|
||||
logger.Error("Failed to unmount", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package git
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
@ -28,6 +29,8 @@ type GitClientParam struct {
|
|||
type gitClient struct {
|
||||
GitClientParam
|
||||
|
||||
logger *slog.Logger
|
||||
|
||||
hostnameProg *regexp.Regexp
|
||||
|
||||
majorVersion int
|
||||
|
@ -39,12 +42,14 @@ type gitClient struct {
|
|||
pullTask *taskq.Task
|
||||
}
|
||||
|
||||
func NewClient(p GitClientParam) (*gitClient, error) {
|
||||
func NewClient(logger *slog.Logger, p GitClientParam) (*gitClient, error) {
|
||||
queueFactory := memqueue.NewFactory()
|
||||
// Create the client
|
||||
c := &gitClient{
|
||||
GitClientParam: p,
|
||||
|
||||
logger: logger,
|
||||
|
||||
hostnameProg: regexp.MustCompile(`([a-z0-1:\-]+\.)+[a-z0-1:\-]+`),
|
||||
|
||||
queue: queueFactory.RegisterQueue(&taskq.QueueOptions{
|
||||
|
@ -56,7 +61,7 @@ func NewClient(p GitClientParam) (*gitClient, error) {
|
|||
}
|
||||
|
||||
// Parse git version
|
||||
gitVersionOutput, err := utils.ExecProcess("git", "--version")
|
||||
gitVersionOutput, err := utils.ExecProcess(logger, "git", "--version")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to run \"git --version\": %v", err)
|
||||
}
|
||||
|
@ -71,7 +76,7 @@ func NewClient(p GitClientParam) (*gitClient, error) {
|
|||
return nil, fmt.Errorf("failed to parse git minor version \"%v\": %v", gitVersionOutput, err)
|
||||
}
|
||||
c.patchVersion = gitVersionMatches[3]
|
||||
fmt.Printf("Detected git version: major = %v minor = %v patch = %v\n", c.majorVersion, c.minorVersion, c.patchVersion)
|
||||
logger.Info("Detected git version", "major", c.majorVersion, "minor", c.minorVersion, "patch", c.patchVersion)
|
||||
|
||||
// Register tasks
|
||||
c.cloneTask = taskq.RegisterTask(&taskq.TaskOptions{
|
||||
|
|
12
git/clone.go
12
git/clone.go
|
@ -15,26 +15,27 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
|||
// resulting in a very barebone local copy
|
||||
|
||||
// Init the local repo
|
||||
fmt.Printf("Initializing %v into %v\n", url, dst)
|
||||
c.logger.Info("Initializing git repository", "directory", dst, "repository", url)
|
||||
args := []string{
|
||||
"init",
|
||||
}
|
||||
if c.majorVersion > 2 || c.majorVersion == 2 && c.minorVersion >= 28 {
|
||||
args = append(args, "--initial-branch", defaultBranch)
|
||||
} else {
|
||||
fmt.Printf("Version of git is too old to support --initial-branch. Consider upgrading git to version >= 2.28.0")
|
||||
c.logger.Warn("Version of git is too old to support --initial-branch. Consider upgrading git to version >= 2.28.0")
|
||||
}
|
||||
args = append(args,
|
||||
"--",
|
||||
dst, // directory
|
||||
)
|
||||
_, err := utils.ExecProcess("git", args...)
|
||||
_, err := utils.ExecProcess(c.logger, "git", args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to init git repo %v to %v: %v", url, dst, err)
|
||||
}
|
||||
|
||||
// Configure the remote
|
||||
_, err = utils.ExecProcessInDir(
|
||||
c.logger,
|
||||
dst, // workdir
|
||||
"git", "remote", "add",
|
||||
"-m", defaultBranch,
|
||||
|
@ -48,6 +49,7 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
|||
|
||||
// Configure the default branch
|
||||
_, err = utils.ExecProcessInDir(
|
||||
c.logger,
|
||||
dst, // workdir
|
||||
"git", "config", "--local",
|
||||
"--",
|
||||
|
@ -59,6 +61,7 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
|||
return fmt.Errorf("failed to setup default branch remote in git repo %v: %v", dst, err)
|
||||
}
|
||||
_, err = utils.ExecProcessInDir(
|
||||
c.logger,
|
||||
dst, // workdir
|
||||
"git", "config", "--local",
|
||||
"--",
|
||||
|
@ -71,6 +74,7 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
|||
}
|
||||
} else {
|
||||
// Clone the repo
|
||||
c.logger.Info("Cloning git repository", "directory", dst, "repository", url)
|
||||
args := []string{
|
||||
"clone",
|
||||
"--origin", c.GitClientParam.Remote,
|
||||
|
@ -84,7 +88,7 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
|||
dst, // directory
|
||||
)
|
||||
|
||||
_, err := utils.ExecProcess("git", args...)
|
||||
_, err := utils.ExecProcess(c.logger, "git", args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to clone git repo %v to %v: %v", url, dst, err)
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
func (c *gitClient) pull(repoPath string, defaultBranch string) error {
|
||||
// Check if the local repo is on default branch
|
||||
branchName, err := utils.ExecProcessInDir(
|
||||
c.logger,
|
||||
repoPath, // workdir
|
||||
"git", "branch",
|
||||
"--show-current",
|
||||
|
@ -32,12 +33,12 @@ func (c *gitClient) pull(repoPath string, defaultBranch string) error {
|
|||
defaultBranch, // refspec
|
||||
)
|
||||
|
||||
_, err = utils.ExecProcessInDir(repoPath, "git", args...)
|
||||
_, err = utils.ExecProcessInDir(c.logger, repoPath, "git", args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to pull git repo %v: %v", repoPath, err)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("%v != %v, skipping pull", branchName, defaultBranch)
|
||||
c.logger.Info("Skipping pull because local is not on default branch", "currentBranch", branchName, "defaultBranch", defaultBranch)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
9
main.go
9
main.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -110,6 +111,9 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Get logger
|
||||
logger := slog.Default()
|
||||
|
||||
// Configure mountpoint
|
||||
mountpoint := config.FS.Mountpoint
|
||||
if flag.NArg() == 1 {
|
||||
|
@ -137,7 +141,7 @@ func main() {
|
|||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
gitClient, _ := git.NewClient(*gitClientParam)
|
||||
gitClient, _ := git.NewClient(logger, *gitClientParam)
|
||||
|
||||
// Create the gitlab client
|
||||
GitlabClientConfig, err := makeGitlabConfig(config)
|
||||
|
@ -145,10 +149,11 @@ func main() {
|
|||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
gitlabClient, _ := gitlab.NewClient(config.Gitlab.URL, config.Gitlab.Token, *GitlabClientConfig)
|
||||
gitlabClient, _ := gitlab.NewClient(logger, config.Gitlab.URL, config.Gitlab.Token, *GitlabClientConfig)
|
||||
|
||||
// Start the filesystem
|
||||
err = fstree.Start(
|
||||
logger,
|
||||
mountpoint,
|
||||
parsedMountoptions,
|
||||
&fstree.FSParam{GitClient: gitClient, GitPlatform: gitlabClient},
|
||||
|
|
|
@ -2,6 +2,7 @@ package gitlab
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"github.com/badjware/gitlabfs/fstree"
|
||||
|
@ -26,6 +27,8 @@ type gitlabClient struct {
|
|||
GitlabClientConfig
|
||||
client *gitlab.Client
|
||||
|
||||
logger *slog.Logger
|
||||
|
||||
// root group cache
|
||||
rootGroupCache map[string]fstree.GroupSource
|
||||
currentUserCache *User
|
||||
|
@ -35,7 +38,7 @@ type gitlabClient struct {
|
|||
userCache map[int]*User
|
||||
}
|
||||
|
||||
func NewClient(gitlabUrl string, gitlabToken string, p GitlabClientConfig) (*gitlabClient, error) {
|
||||
func NewClient(logger *slog.Logger, gitlabUrl string, gitlabToken string, p GitlabClientConfig) (*gitlabClient, error) {
|
||||
client, err := gitlab.NewClient(
|
||||
gitlabToken,
|
||||
gitlab.WithBaseURL(gitlabUrl),
|
||||
|
@ -48,6 +51,8 @@ func NewClient(gitlabUrl string, gitlabToken string, p GitlabClientConfig) (*git
|
|||
GitlabClientConfig: p,
|
||||
client: client,
|
||||
|
||||
logger: logger,
|
||||
|
||||
rootGroupCache: nil,
|
||||
currentUserCache: nil,
|
||||
|
||||
|
@ -82,9 +87,10 @@ func (c *gitlabClient) FetchRootGroupContent() (map[string]fstree.GroupSource, e
|
|||
if c.IncludeCurrentUser {
|
||||
currentUser, err := c.fetchCurrentUser()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
c.logger.Warn(err.Error())
|
||||
} else {
|
||||
rootGroupCache[currentUser.Name] = currentUser
|
||||
}
|
||||
rootGroupCache[currentUser.Name] = currentUser
|
||||
}
|
||||
|
||||
c.rootGroupCache = rootGroupCache
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
@ -11,19 +11,19 @@ const (
|
|||
stderr = "stderr"
|
||||
)
|
||||
|
||||
func ExecProcessInDir(workdir string, command string, args ...string) (string, error) {
|
||||
func ExecProcessInDir(logger *slog.Logger, workdir string, command string, args ...string) (string, error) {
|
||||
cmd := exec.Command(command, args...)
|
||||
if workdir != "" {
|
||||
cmd.Dir = workdir
|
||||
}
|
||||
|
||||
// Run the command
|
||||
fmt.Printf("%v %v\n", command, strings.Join(args, " "))
|
||||
logger.Debug("Running command", "cmd", command, "args", args)
|
||||
output, err := cmd.Output()
|
||||
|
||||
return strings.TrimSpace(string(output)), err
|
||||
}
|
||||
|
||||
func ExecProcess(command string, args ...string) (string, error) {
|
||||
return ExecProcessInDir("", command, args...)
|
||||
func ExecProcess(logger *slog.Logger, command string, args ...string) (string, error) {
|
||||
return ExecProcessInDir(logger, "", command, args...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue