-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (137 loc) · 3.71 KB
/
Copy pathmain.go
File metadata and controls
151 lines (137 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"runtime/debug"
"strings"
"github.com/badjware/gitforgefs/cache"
"github.com/badjware/gitforgefs/config"
"github.com/badjware/gitforgefs/forges/gitea"
"github.com/badjware/gitforgefs/forges/github"
"github.com/badjware/gitforgefs/forges/gitlab"
"github.com/badjware/gitforgefs/fstree"
"github.com/badjware/gitforgefs/git"
"github.com/badjware/gitforgefs/types"
)
func main() {
configPath := flag.String("config", "config.yaml", "The config file")
mountoptionsFlag := flag.String("o", "", "Filesystem mount options. See mount.fuse(8)")
versionFlag := flag.Bool("version", false, "Print version information and exit")
debugFlag := flag.Bool("debug", false, "Enable debug logging")
debugPort := flag.Int("debug-port", 0, "Listen port for debug server. If 0, server is disabled")
flag.Usage = func() {
fmt.Println("USAGE:")
fmt.Printf(" %s MOUNTPOINT\n\n", os.Args[0])
fmt.Println("OPTIONS:")
flag.PrintDefaults()
}
flag.Parse()
if *versionFlag {
version := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
}
fmt.Println(version)
os.Exit(0)
}
loadedConfig, err := config.LoadConfig(*configPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get logger
var level slog.Level
if *debugFlag {
level = slog.LevelDebug
} else {
level = slog.LevelInfo
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
}))
// start pprof server if debug port is set
if *debugPort != 0 {
go func() {
logger.Info("Starting debug server", "port", *debugPort)
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", *debugPort), nil)
if err != nil {
logger.Error("debug server failed", "error", err)
}
}()
}
// Configure mountpoint
mountpoint := loadedConfig.FS.Mountpoint
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)
}
// Configure mountoptions
mountoptions := loadedConfig.FS.MountOptions
if *mountoptionsFlag != "" {
mountoptions = *mountoptionsFlag
}
parsedMountoptions := make([]string, 0)
if mountoptions != "" {
parsedMountoptions = strings.Split(mountoptions, ",")
}
// Create the git client
gitClientParam, err := config.MakeGitConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitClient, _ := git.NewClient(logger, *gitClientParam)
// setup backend
var gitForgeClient types.GitForge
if loadedConfig.FS.Forge == config.ForgeGitlab {
// Create the gitlab client
gitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitForgeClient, _ = gitlab.NewClient(logger, *gitlabClientConfig)
} else if loadedConfig.FS.Forge == config.ForgeGithub {
// Create the github client
githubClientConfig, err := config.MakeGithubConfig(loadedConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
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)
}
// setup cache
cache := cache.NewForgeCache(gitForgeClient, logger)
// Start the filesystem
err = fstree.Start(
logger,
mountpoint,
parsedMountoptions,
&fstree.FSParam{
UseSymlinks: loadedConfig.FS.UseSymlinks,
GitClient: gitClient,
Backend: cache,
},
*debugFlag,
)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}