Compare commits

..

No commits in common. "0a501582394c63581d52dacb19777e3bf3aeaf89" and "e26f0ae86566c8404fe497b42891950e9026cdc2" have entirely different histories.

5 changed files with 92 additions and 288 deletions

View File

@ -14,12 +14,6 @@ gitlab:
# Default to anonymous (only public projects will be visible).
#token:
# Must be set to either "http" or "ssh".
# The protocol to configure the git remote on.
# "http" may not work on private repos unless a credential manager is configured
# If possible, prefer "ssh" over "http"
pull_method: http
# A list of the group ids to expose their projects in the filesystem.
group_ids:
- 9970 # gitlab-org
@ -38,6 +32,12 @@ git:
# The name of the remote in the local clone.
remote: origin
# Must be set to either "http" or "ssh".
# The protocol to configure the git remote on.
# "http" may not work on private repos unless a credential manager is configured
# If possible, prefer "ssh" over "http"
pull_method: http
# Must be set to either "init", or "clone".
# If set to "init", the local copy will be initialized with `git init` and the remote is configured manually. The git server is nerver queried. (fast)
# If set to "clone", the local copy will be initialized with `git clone`. (slow)

View File

@ -1,22 +0,0 @@
fs:
mountpoint: /tmp/gitlabfs/test/mnt
mountoptions: nodev
gitlab:
url: https://example.com
token: "12345"
group_ids:
- 123
user_ids:
- 456
include_current_user: true
pull_method: ssh
git:
clone_location: /tmp/gitlabfs/test/clone
remote: origin
on_clone: clone
auto_pull: false
depth: 0
queue_size: 100
worker_count: 1

View File

@ -1,89 +0,0 @@
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/platforms/gitlab"
"gopkg.in/yaml.v2"
)
type (
Config struct {
FS FSConfig `yaml:"fs,omitempty"`
Gitlab gitlab.GitlabClientConfig `yaml:"gitlab,omitempty"`
Git git.GitClientParam `yaml:"git,omitempty"`
}
FSConfig struct {
Mountpoint string `yaml:"mountpoint,omitempty"`
MountOptions string `yaml:"mountoptions,omitempty"`
}
)
func LoadConfig(configPath string) (*Config, error) {
// defaults
dataHome := os.Getenv("XDG_DATA_HOME")
if dataHome == "" {
dataHome = filepath.Join(os.Getenv("HOME"), ".local/share")
}
defaultCloneLocation := filepath.Join(dataHome, "gitlabfs")
config := &Config{
FS: FSConfig{
Mountpoint: "",
MountOptions: "nodev,nosuid",
},
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
PullMethod: "http",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
},
Git: git.GitClientParam{
CloneLocation: defaultCloneLocation,
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
}
if configPath != "" {
f, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %v", err)
}
defer f.Close()
d := yaml.NewDecoder(f)
if err := d.Decode(config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %v", err)
}
}
return config, nil
}
func MakeGitConfig(config *Config) (*git.GitClientParam, error) {
// parse on_clone
if config.Git.OnClone != "init" && config.Git.OnClone != "clone" {
return nil, fmt.Errorf("on_clone must be either \"init\" or \"clone\"")
}
return &config.Git, nil
}
func MakeGitlabConfig(config *Config) (*gitlab.GitlabClientConfig, error) {
// parse pull_method
if config.Gitlab.PullMethod != gitlab.PullMethodHTTP && config.Gitlab.PullMethod != gitlab.PullMethodSSH {
return nil, fmt.Errorf("pull_method must be either \"%v\" or \"%v\"", gitlab.PullMethodHTTP, gitlab.PullMethodSSH)
}
return &config.Gitlab, nil
}

View File

@ -1,164 +0,0 @@
package config_test
import (
"reflect"
"testing"
"github.com/badjware/gitlabfs/config"
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/platforms/gitlab"
)
func TestLoadConfig(t *testing.T) {
tests := map[string]struct {
input string
expected *config.Config
}{
"LoadConfig": {
input: "config.test.yaml",
expected: &config.Config{
FS: config.FSConfig{
Mountpoint: "/tmp/gitlabfs/test/mnt",
MountOptions: "nodev",
},
Gitlab: gitlab.GitlabClientConfig{
URL: "https://example.com",
Token: "12345",
PullMethod: "ssh",
GroupIDs: []int{123},
UserIDs: []int{456},
IncludeCurrentUser: true,
},
Git: git.GitClientParam{
CloneLocation: "/tmp/gitlabfs/test/clone",
Remote: "origin",
OnClone: "clone",
AutoPull: false,
Depth: 0,
QueueSize: 100,
QueueWorkerCount: 1,
}},
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.LoadConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("LoadConfig(%v) returned %v; expected %v; error: %v", test.input, got, expected, err)
}
})
}
}
func TestMakeGitConfig(t *testing.T) {
tests := map[string]struct {
input *config.Config
expected *git.GitClientParam
}{
"ValidConfig": {
input: &config.Config{
Git: git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
expected: &git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
"InvalidOnClone": {
input: &config.Config{
Git: git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "invalid",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
expected: nil,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.MakeGitConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("MakeGitConfig(%v) returned %v; expected %v; error %v", test.input, got, expected, err)
}
})
}
}
func TestMakeGitlabConfig(t *testing.T) {
tests := map[string]struct {
input *config.Config
expected *gitlab.GitlabClientConfig
}{
"ValidConfig": {
input: &config.Config{
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "http",
},
},
expected: &gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "http",
},
},
"InvalidPullMethod": {
input: &config.Config{
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "invalid",
},
},
expected: nil,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.MakeGitlabConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("MakeGitlabConfig(%v) returned %v; expected %v; error: %v", test.input, got, expected, err)
}
})
}
}

93
main.go
View File

@ -5,14 +5,93 @@ import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/badjware/gitlabfs/config"
"github.com/badjware/gitlabfs/fstree"
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/platforms/gitlab"
"gopkg.in/yaml.v2"
)
type (
Config struct {
FS FSConfig `yaml:"fs,omitempty"`
Gitlab gitlab.GitlabClientConfig `yaml:"gitlab,omitempty"`
Git git.GitClientParam `yaml:"git,omitempty"`
}
FSConfig struct {
Mountpoint string `yaml:"mountpoint,omitempty"`
MountOptions string `yaml:"mountoptions,omitempty"`
}
)
func loadConfig(configPath string) (*Config, error) {
// defaults
dataHome := os.Getenv("XDG_DATA_HOME")
if dataHome == "" {
dataHome = filepath.Join(os.Getenv("HOME"), ".local/share")
}
defaultCloneLocation := filepath.Join(dataHome, "gitlabfs")
config := &Config{
FS: FSConfig{
Mountpoint: "",
MountOptions: "nodev,nosuid",
},
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "http",
},
Git: git.GitClientParam{
CloneLocation: defaultCloneLocation,
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
}
if configPath != "" {
f, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %v", err)
}
defer f.Close()
d := yaml.NewDecoder(f)
if err := d.Decode(config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %v", err)
}
}
return config, nil
}
func makeGitlabConfig(config *Config) (*gitlab.GitlabClientConfig, error) {
// parse pull_method
if config.Gitlab.PullMethod != gitlab.PullMethodHTTP && config.Gitlab.PullMethod != gitlab.PullMethodSSH {
return nil, fmt.Errorf("pull_method must be either \"%v\" or \"%v\"", gitlab.PullMethodHTTP, gitlab.PullMethodSSH)
}
return &config.Gitlab, nil
}
func makeGitConfig(config *Config) (*git.GitClientParam, error) {
// parse on_clone
if config.Git.OnClone != "init" && config.Git.OnClone != "clone" {
return nil, fmt.Errorf("on_clone must be either \"init\" or \"clone\"")
}
return &config.Git, nil
}
func main() {
configPath := flag.String("config", "", "The config file")
mountoptionsFlag := flag.String("o", "", "Filesystem mount options. See mount.fuse(8)")
@ -26,7 +105,7 @@ func main() {
}
flag.Parse()
loadedConfig, err := config.LoadConfig(*configPath)
config, err := loadConfig(*configPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
@ -36,7 +115,7 @@ func main() {
logger := slog.Default()
// Configure mountpoint
mountpoint := loadedConfig.FS.Mountpoint
mountpoint := config.FS.Mountpoint
if flag.NArg() == 1 {
mountpoint = flag.Arg(0)
}
@ -47,7 +126,7 @@ func main() {
}
// Configure mountoptions
mountoptions := loadedConfig.FS.MountOptions
mountoptions := config.FS.MountOptions
if *mountoptionsFlag != "" {
mountoptions = *mountoptionsFlag
}
@ -57,7 +136,7 @@ func main() {
}
// Create the git client
gitClientParam, err := config.MakeGitConfig(loadedConfig)
gitClientParam, err := makeGitConfig(config)
if err != nil {
fmt.Println(err)
os.Exit(1)
@ -65,12 +144,12 @@ func main() {
gitClient, _ := git.NewClient(logger, *gitClientParam)
// Create the gitlab client
GitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
GitlabClientConfig, err := makeGitlabConfig(config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gitlabClient, _ := gitlab.NewClient(logger, loadedConfig.Gitlab.URL, loadedConfig.Gitlab.Token, *GitlabClientConfig)
gitlabClient, _ := gitlab.NewClient(logger, config.Gitlab.URL, config.Gitlab.Token, *GitlabClientConfig)
// Start the filesystem
err = fstree.Start(