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