gitforgefs/main.go

100 lines
2.4 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"
"github.com/badjware/gitlabfs/fstree"
2020-12-29 02:37:18 +00:00
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/platforms/github"
2024-05-06 01:06:34 +00:00
"github.com/badjware/gitlabfs/platforms/gitlab"
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
var gitPlatformClient fstree.GitPlatform
if loadedConfig.FS.Platform == config.PlatformGitlab {
// Create the gitlab client
GitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitPlatformClient, _ = gitlab.NewClient(logger, loadedConfig.Gitlab.URL, loadedConfig.Gitlab.Token, *GitlabClientConfig)
} else if loadedConfig.FS.Platform == config.PlatformGithub {
// Create the github client
GithubClientConfig, err := config.MakeGithubConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitPlatformClient, _ = github.NewClient(logger, *GithubClientConfig)
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,
&fstree.FSParam{GitClient: gitClient, GitPlatform: gitPlatformClient},
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
}