gitforgefs/main.go

109 lines
2.6 KiB
Go
Raw Normal View History

2020-12-26 19:36:09 +00:00
package main
2020-12-27 03:30:19 +00:00
import (
"flag"
"fmt"
2024-06-06 05:51:34 +00:00
"log/slog"
2020-12-27 03:30:19 +00:00
"os"
"strings"
2020-12-27 03:30:19 +00:00
2024-06-08 03:36:45 +00:00
"github.com/badjware/gitlabfs/config"
2024-08-09 20:13:57 +00:00
"github.com/badjware/gitlabfs/forges/gitea"
2024-08-05 01:45:51 +00:00
"github.com/badjware/gitlabfs/forges/github"
"github.com/badjware/gitlabfs/forges/gitlab"
"github.com/badjware/gitlabfs/fstree"
2020-12-29 02:37:18 +00:00
"github.com/badjware/gitlabfs/git"
2020-12-27 03:30:19 +00:00
)
2020-12-26 19:36:09 +00:00
func main() {
configPath := flag.String("config", "", "The config file")
mountoptionsFlag := flag.String("o", "", "Filesystem mount options. See mount.fuse(8)")
debug := flag.Bool("debug", false, "Enable debug logging")
flag.Usage = func() {
fmt.Println("USAGE:")
fmt.Printf(" %s MOUNTPOINT\n\n", os.Args[0])
fmt.Println("OPTIONS:")
flag.PrintDefaults()
}
2020-12-27 03:30:19 +00:00
flag.Parse()
2020-12-30 23:00:37 +00:00
2024-06-08 03:36:45 +00:00
loadedConfig, err := config.LoadConfig(*configPath)
2020-12-30 23:00:37 +00:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2024-06-06 05:51:34 +00:00
// Get logger
logger := slog.Default()
2020-12-30 23:00:37 +00:00
// Configure mountpoint
2024-06-08 03:36:45 +00:00
mountpoint := loadedConfig.FS.Mountpoint
2020-12-30 23:00:37 +00:00
if flag.NArg() == 1 {
mountpoint = flag.Arg(0)
}
if mountpoint == "" {
fmt.Println("Mountpoint is not configured in config file and missing from command-line arguments")
flag.Usage()
os.Exit(2)
2020-12-27 03:30:19 +00:00
}
2020-12-30 23:00:37 +00:00
// Configure mountoptions
2024-06-08 03:36:45 +00:00
mountoptions := loadedConfig.FS.MountOptions
if *mountoptionsFlag != "" {
mountoptions = *mountoptionsFlag
}
parsedMountoptions := make([]string, 0)
if mountoptions != "" {
parsedMountoptions = strings.Split(mountoptions, ",")
}
2020-12-30 23:00:37 +00:00
// Create the git client
2024-06-08 03:36:45 +00:00
gitClientParam, err := config.MakeGitConfig(loadedConfig)
2020-12-29 02:37:18 +00:00
if err != nil {
2020-12-30 23:00:37 +00:00
fmt.Println(err)
2020-12-29 02:37:18 +00:00
os.Exit(1)
}
2024-06-06 05:51:34 +00:00
gitClient, _ := git.NewClient(logger, *gitClientParam)
2020-12-27 03:30:19 +00:00
2024-08-05 01:45:51 +00:00
var gitForgeClient fstree.GitForge
if loadedConfig.FS.Forge == config.ForgeGitlab {
// Create the gitlab client
2024-08-09 20:13:57 +00:00
gitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2024-08-09 20:13:57 +00:00
gitForgeClient, _ = gitlab.NewClient(logger, *gitlabClientConfig)
2024-08-05 01:45:51 +00:00
} else if loadedConfig.FS.Forge == config.ForgeGithub {
// Create the github client
2024-08-09 20:13:57 +00:00
githubClientConfig, err := config.MakeGithubConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2024-08-09 20:13:57 +00:00
gitForgeClient, _ = github.NewClient(logger, *githubClientConfig)
} else if loadedConfig.FS.Forge == config.ForgeGitea {
// Create the gitea client
giteaClientConfig, err := config.MakeGiteaConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitForgeClient, _ = gitea.NewClient(logger, *giteaClientConfig)
2020-12-29 02:37:18 +00:00
}
2020-12-29 02:37:18 +00:00
// Start the filesystem
err = fstree.Start(
2024-06-06 05:51:34 +00:00
logger,
2020-12-30 23:00:37 +00:00
mountpoint,
parsedMountoptions,
2024-08-05 01:45:51 +00:00
&fstree.FSParam{GitClient: gitClient, GitForge: gitForgeClient},
2020-12-30 23:00:37 +00:00
*debug,
)
2021-03-03 05:25:49 +00:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2020-12-26 19:36:09 +00:00
}