refactor config loader and add github config
This commit is contained in:
parent
dca46e8c69
commit
abf8507673
|
@ -6,6 +6,10 @@ fs:
|
||||||
# See mount.fuse(8) for the full list of options.
|
# See mount.fuse(8) for the full list of options.
|
||||||
#mountoptions: nodev,nosuid
|
#mountoptions: nodev,nosuid
|
||||||
|
|
||||||
|
# The git platform to use as the backend.
|
||||||
|
# Must be one of "gitlab", or "github"
|
||||||
|
platform: gitlab
|
||||||
|
|
||||||
gitlab:
|
gitlab:
|
||||||
# The gitlab url.
|
# The gitlab url.
|
||||||
url: https://gitlab.com
|
url: https://gitlab.com
|
||||||
|
@ -16,7 +20,7 @@ gitlab:
|
||||||
|
|
||||||
# Must be set to either "http" or "ssh".
|
# Must be set to either "http" or "ssh".
|
||||||
# The protocol to configure the git remote on.
|
# The protocol to configure the git remote on.
|
||||||
# "http" may not work on private repos unless a credential manager is configured
|
# "http" may not work on private projects unless a credential manager is configured
|
||||||
# If possible, prefer "ssh" over "http"
|
# If possible, prefer "ssh" over "http"
|
||||||
pull_method: http
|
pull_method: http
|
||||||
|
|
||||||
|
@ -28,7 +32,7 @@ gitlab:
|
||||||
user_ids: []
|
user_ids: []
|
||||||
|
|
||||||
# Set how archived projects are handled.
|
# Set how archived projects are handled.
|
||||||
# If set to "show", it will add them to the filesystem and treat them like any other repository
|
# If set to "show", it will add them to the filesystem and treat them like any other project
|
||||||
# If set to "hide", it will add them to the filesystem, but prefix the symlink with a "."
|
# If set to "hide", it will add them to the filesystem, but prefix the symlink with a "."
|
||||||
# If set to "ignore", it will make them absent from the filesystem
|
# If set to "ignore", it will make them absent from the filesystem
|
||||||
# Default to "hide"
|
# Default to "hide"
|
||||||
|
@ -37,6 +41,35 @@ gitlab:
|
||||||
# If set to true, the user the api token belongs to will automatically be added to the list of users exposed by the filesystem.
|
# If set to true, the user the api token belongs to will automatically be added to the list of users exposed by the filesystem.
|
||||||
include_current_user: true
|
include_current_user: true
|
||||||
|
|
||||||
|
github:
|
||||||
|
# The github api token
|
||||||
|
# Default to anonymous (only public repositories 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 repositories unless a credential manager is configured
|
||||||
|
# If possible, prefer "ssh" over "http"
|
||||||
|
pull_method: http
|
||||||
|
|
||||||
|
# A list of the name of the organizations to expose in the filesystem
|
||||||
|
org_names: []
|
||||||
|
|
||||||
|
# A list of the name of the user to expose their repositories un the filesystem
|
||||||
|
user_names: []
|
||||||
|
|
||||||
|
# Set how archived repositories are handled.
|
||||||
|
# If set to "show", it will add them to the filesystem and treat them like any other repository
|
||||||
|
# If set to "hide", it will add them to the filesystem, but prefix the symlink with a "."
|
||||||
|
# If set to "ignore", it will make them absent from the filesystem
|
||||||
|
# Default to "hide"
|
||||||
|
archived_repo_handling: hide
|
||||||
|
|
||||||
|
# If set to true, the personal repositories and the repositories of the organizations the user the api token belongs to
|
||||||
|
# will be automatically be added to the list of users exposed by the filesystem.
|
||||||
|
include_current_user: true
|
||||||
|
|
||||||
|
|
||||||
git:
|
git:
|
||||||
# Path to the local repository cache. Repositories in the filesystem will symlink to a folder in this path.
|
# Path to the local repository cache. Repositories in the filesystem will symlink to a folder in this path.
|
||||||
# Default to $XDG_DATA_HOME/gitlabfs, or $HOME/.local/share/gitlabfs if the environment variable $XDG_DATA_HOME is unset.
|
# Default to $XDG_DATA_HOME/gitlabfs, or $HOME/.local/share/gitlabfs if the environment variable $XDG_DATA_HOME is unset.
|
||||||
|
|
|
@ -1,19 +1,31 @@
|
||||||
fs:
|
fs:
|
||||||
mountpoint: /tmp/gitlabfs/test/mnt
|
mountpoint: /tmp/gitlabfs/test/mnt/gitlab
|
||||||
mountoptions: nodev
|
mountoptions: nodev
|
||||||
|
platform: gitlab
|
||||||
|
|
||||||
gitlab:
|
gitlab:
|
||||||
url: https://example.com
|
url: https://example.com
|
||||||
token: "12345"
|
token: "12345"
|
||||||
|
pull_method: ssh
|
||||||
group_ids:
|
group_ids:
|
||||||
- 123
|
- 123
|
||||||
user_ids:
|
user_ids:
|
||||||
- 456
|
- 456
|
||||||
|
archived_project_handling: hide
|
||||||
|
include_current_user: true
|
||||||
|
|
||||||
|
github:
|
||||||
|
token: "12345"
|
||||||
|
pull_method: http
|
||||||
|
org_names:
|
||||||
|
- test-org
|
||||||
|
user_names:
|
||||||
|
- test-user
|
||||||
|
archived_repo_handling: hide
|
||||||
include_current_user: true
|
include_current_user: true
|
||||||
pull_method: ssh
|
|
||||||
|
|
||||||
git:
|
git:
|
||||||
clone_location: /tmp/gitlabfs/test/clone
|
clone_location: /tmp/gitlabfs/test/cache/gitlab
|
||||||
remote: origin
|
remote: origin
|
||||||
on_clone: clone
|
on_clone: clone
|
||||||
auto_pull: false
|
auto_pull: false
|
||||||
|
|
112
config/loader.go
112
config/loader.go
|
@ -5,20 +5,62 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/badjware/gitlabfs/git"
|
|
||||||
"github.com/badjware/gitlabfs/platforms/gitlab"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PlatformGitlab = "gitlab"
|
||||||
|
PlatformGithub = "github"
|
||||||
|
|
||||||
|
PullMethodHTTP = "http"
|
||||||
|
PullMethodSSH = "ssh"
|
||||||
|
|
||||||
|
ArchivedProjectShow = "show"
|
||||||
|
ArchivedProjectHide = "hide"
|
||||||
|
ArchivedProjectIgnore = "ignore"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Config struct {
|
Config struct {
|
||||||
FS FSConfig `yaml:"fs,omitempty"`
|
FS FSConfig `yaml:"fs,omitempty"`
|
||||||
Gitlab gitlab.GitlabClientConfig `yaml:"gitlab,omitempty"`
|
Gitlab GitlabClientConfig `yaml:"gitlab,omitempty"`
|
||||||
Git git.GitClientParam `yaml:"git,omitempty"`
|
Github GithubClientConfig `yaml:"github,omitempty"`
|
||||||
|
Git GitClientConfig `yaml:"git,omitempty"`
|
||||||
}
|
}
|
||||||
FSConfig struct {
|
FSConfig struct {
|
||||||
Mountpoint string `yaml:"mountpoint,omitempty"`
|
Mountpoint string `yaml:"mountpoint,omitempty"`
|
||||||
MountOptions string `yaml:"mountoptions,omitempty"`
|
MountOptions string `yaml:"mountoptions,omitempty"`
|
||||||
|
Platform string `yaml:"platform,omitempty"`
|
||||||
|
}
|
||||||
|
GitlabClientConfig struct {
|
||||||
|
URL string `yaml:"url,omitempty"`
|
||||||
|
Token string `yaml:"token,omitempty"`
|
||||||
|
|
||||||
|
GroupIDs []int `yaml:"group_ids,omitempty"`
|
||||||
|
UserIDs []int `yaml:"user_ids,omitempty"`
|
||||||
|
|
||||||
|
ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"`
|
||||||
|
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
|
||||||
|
PullMethod string `yaml:"pull_method,omitempty"`
|
||||||
|
}
|
||||||
|
GithubClientConfig struct {
|
||||||
|
Token string `yaml:"token,omitempty"`
|
||||||
|
|
||||||
|
OrgNames []string `yaml:"org_names,omitempty"`
|
||||||
|
UserNames []string `yaml:"user_names,omitempty"`
|
||||||
|
|
||||||
|
ArchivedRepoHandling string `yaml:"archived_repo_handling,omitempty"`
|
||||||
|
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
|
||||||
|
PullMethod string `yaml:"pull_method,omitempty"`
|
||||||
|
}
|
||||||
|
GitClientConfig struct {
|
||||||
|
CloneLocation string `yaml:"clone_location,omitempty"`
|
||||||
|
Remote string `yaml:"remote,omitempty"`
|
||||||
|
OnClone string `yaml:"on_clone,omitempty"`
|
||||||
|
AutoPull bool `yaml:"auto_pull,omitempty"`
|
||||||
|
Depth int `yaml:"depth,omitempty"`
|
||||||
|
QueueSize int `yaml:"queue_size,omitempty"`
|
||||||
|
QueueWorkerCount int `yaml:"worker_count,omitempty"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,8 +76,9 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||||
FS: FSConfig{
|
FS: FSConfig{
|
||||||
Mountpoint: "",
|
Mountpoint: "",
|
||||||
MountOptions: "nodev,nosuid",
|
MountOptions: "nodev,nosuid",
|
||||||
|
Platform: "",
|
||||||
},
|
},
|
||||||
Gitlab: gitlab.GitlabClientConfig{
|
Gitlab: GitlabClientConfig{
|
||||||
URL: "https://gitlab.com",
|
URL: "https://gitlab.com",
|
||||||
Token: "",
|
Token: "",
|
||||||
PullMethod: "http",
|
PullMethod: "http",
|
||||||
|
@ -44,7 +87,15 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||||
ArchivedProjectHandling: "hide",
|
ArchivedProjectHandling: "hide",
|
||||||
IncludeCurrentUser: true,
|
IncludeCurrentUser: true,
|
||||||
},
|
},
|
||||||
Git: git.GitClientParam{
|
Github: GithubClientConfig{
|
||||||
|
Token: "",
|
||||||
|
PullMethod: "http",
|
||||||
|
OrgNames: []string{},
|
||||||
|
UserNames: []string{},
|
||||||
|
ArchivedRepoHandling: "hide",
|
||||||
|
IncludeCurrentUser: true,
|
||||||
|
},
|
||||||
|
Git: GitClientConfig{
|
||||||
CloneLocation: defaultCloneLocation,
|
CloneLocation: defaultCloneLocation,
|
||||||
Remote: "origin",
|
Remote: "origin",
|
||||||
OnClone: "init",
|
OnClone: "init",
|
||||||
|
@ -68,28 +119,47 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validate platform is set
|
||||||
|
if config.FS.Platform != PlatformGithub && config.FS.Platform != PlatformGitlab {
|
||||||
|
return nil, fmt.Errorf("fs.platform must be either \"%v\", or \"%v\"", PlatformGitlab, PlatformGithub)
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeGitConfig(config *Config) (*git.GitClientParam, error) {
|
func MakeGitlabConfig(config *Config) (*GitlabClientConfig, 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
|
// parse pull_method
|
||||||
if config.Gitlab.PullMethod != gitlab.PullMethodHTTP && config.Gitlab.PullMethod != gitlab.PullMethodSSH {
|
if config.Gitlab.PullMethod != PullMethodHTTP && config.Gitlab.PullMethod != PullMethodSSH {
|
||||||
return nil, fmt.Errorf("pull_method must be either \"%v\" or \"%v\"", gitlab.PullMethodHTTP, gitlab.PullMethodSSH)
|
return nil, fmt.Errorf("gitlab.pull_method must be either \"%v\" or \"%v\"", PullMethodHTTP, PullMethodSSH)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse archive_handing
|
// parse archive_handing
|
||||||
if config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectShow && config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectHide && config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectIgnore {
|
if config.Gitlab.ArchivedProjectHandling != ArchivedProjectShow && config.Gitlab.ArchivedProjectHandling != ArchivedProjectHide && config.Gitlab.ArchivedProjectHandling != ArchivedProjectIgnore {
|
||||||
return nil, fmt.Errorf("pull_method must be either \"%v\", \"%v\" or \"%v\"", gitlab.ArchivedProjectShow, gitlab.ArchivedProjectHide, gitlab.ArchivedProjectIgnore)
|
return nil, fmt.Errorf("gitlab.archived_project_handling must be either \"%v\", \"%v\" or \"%v\"", ArchivedProjectShow, ArchivedProjectHide, ArchivedProjectIgnore)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &config.Gitlab, nil
|
return &config.Gitlab, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeGithubConfig(config *Config) (*GithubClientConfig, error) {
|
||||||
|
// parse pull_method
|
||||||
|
if config.Gitlab.PullMethod != PullMethodHTTP && config.Gitlab.PullMethod != PullMethodSSH {
|
||||||
|
return nil, fmt.Errorf("github.pull_method must be either \"%v\" or \"%v\"", PullMethodHTTP, PullMethodSSH)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse archive_handing
|
||||||
|
if config.Gitlab.ArchivedProjectHandling != ArchivedProjectShow && config.Gitlab.ArchivedProjectHandling != ArchivedProjectHide && config.Gitlab.ArchivedProjectHandling != ArchivedProjectIgnore {
|
||||||
|
return nil, fmt.Errorf("github.archived_repo_handling must be either \"%v\", \"%v\" or \"%v\"", ArchivedProjectShow, ArchivedProjectHide, ArchivedProjectIgnore)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config.Github, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeGitConfig(config *Config) (*GitClientConfig, error) {
|
||||||
|
// parse on_clone
|
||||||
|
if config.Git.OnClone != "init" && config.Git.OnClone != "clone" {
|
||||||
|
return nil, fmt.Errorf("git.on_clone must be either \"init\" or \"clone\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config.Git, nil
|
||||||
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/badjware/gitlabfs/config"
|
"github.com/badjware/gitlabfs/config"
|
||||||
"github.com/badjware/gitlabfs/git"
|
|
||||||
"github.com/badjware/gitlabfs/platforms/gitlab"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadConfig(t *testing.T) {
|
func TestLoadConfig(t *testing.T) {
|
||||||
|
@ -18,10 +16,11 @@ func TestLoadConfig(t *testing.T) {
|
||||||
input: "config.test.yaml",
|
input: "config.test.yaml",
|
||||||
expected: &config.Config{
|
expected: &config.Config{
|
||||||
FS: config.FSConfig{
|
FS: config.FSConfig{
|
||||||
Mountpoint: "/tmp/gitlabfs/test/mnt",
|
Mountpoint: "/tmp/gitlabfs/test/mnt/gitlab",
|
||||||
MountOptions: "nodev",
|
MountOptions: "nodev",
|
||||||
|
Platform: "gitlab",
|
||||||
},
|
},
|
||||||
Gitlab: gitlab.GitlabClientConfig{
|
Gitlab: config.GitlabClientConfig{
|
||||||
URL: "https://example.com",
|
URL: "https://example.com",
|
||||||
Token: "12345",
|
Token: "12345",
|
||||||
PullMethod: "ssh",
|
PullMethod: "ssh",
|
||||||
|
@ -30,8 +29,16 @@ func TestLoadConfig(t *testing.T) {
|
||||||
ArchivedProjectHandling: "hide",
|
ArchivedProjectHandling: "hide",
|
||||||
IncludeCurrentUser: true,
|
IncludeCurrentUser: true,
|
||||||
},
|
},
|
||||||
Git: git.GitClientParam{
|
Github: config.GithubClientConfig{
|
||||||
CloneLocation: "/tmp/gitlabfs/test/clone",
|
Token: "12345",
|
||||||
|
PullMethod: "http",
|
||||||
|
OrgNames: []string{"test-org"},
|
||||||
|
UserNames: []string{"test-user"},
|
||||||
|
ArchivedRepoHandling: "hide",
|
||||||
|
IncludeCurrentUser: true,
|
||||||
|
},
|
||||||
|
Git: config.GitClientConfig{
|
||||||
|
CloneLocation: "/tmp/gitlabfs/test/cache/gitlab",
|
||||||
Remote: "origin",
|
Remote: "origin",
|
||||||
OnClone: "clone",
|
OnClone: "clone",
|
||||||
AutoPull: false,
|
AutoPull: false,
|
||||||
|
@ -58,11 +65,14 @@ func TestLoadConfig(t *testing.T) {
|
||||||
func TestMakeGitConfig(t *testing.T) {
|
func TestMakeGitConfig(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
input *config.Config
|
input *config.Config
|
||||||
expected *git.GitClientParam
|
expected *config.GitClientConfig
|
||||||
}{
|
}{
|
||||||
"ValidConfig": {
|
"ValidConfig": {
|
||||||
input: &config.Config{
|
input: &config.Config{
|
||||||
Git: git.GitClientParam{
|
FS: config.FSConfig{
|
||||||
|
Platform: "gitlab",
|
||||||
|
},
|
||||||
|
Git: config.GitClientConfig{
|
||||||
CloneLocation: "/tmp",
|
CloneLocation: "/tmp",
|
||||||
Remote: "origin",
|
Remote: "origin",
|
||||||
OnClone: "init",
|
OnClone: "init",
|
||||||
|
@ -72,7 +82,7 @@ func TestMakeGitConfig(t *testing.T) {
|
||||||
QueueWorkerCount: 5,
|
QueueWorkerCount: 5,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expected: &git.GitClientParam{
|
expected: &config.GitClientConfig{
|
||||||
CloneLocation: "/tmp",
|
CloneLocation: "/tmp",
|
||||||
Remote: "origin",
|
Remote: "origin",
|
||||||
OnClone: "init",
|
OnClone: "init",
|
||||||
|
@ -84,7 +94,10 @@ func TestMakeGitConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
"InvalidOnClone": {
|
"InvalidOnClone": {
|
||||||
input: &config.Config{
|
input: &config.Config{
|
||||||
Git: git.GitClientParam{
|
FS: config.FSConfig{
|
||||||
|
Platform: "gitlab",
|
||||||
|
},
|
||||||
|
Git: config.GitClientConfig{
|
||||||
CloneLocation: "/tmp",
|
CloneLocation: "/tmp",
|
||||||
Remote: "origin",
|
Remote: "origin",
|
||||||
OnClone: "invalid",
|
OnClone: "invalid",
|
||||||
|
@ -114,11 +127,14 @@ func TestMakeGitConfig(t *testing.T) {
|
||||||
func TestMakeGitlabConfig(t *testing.T) {
|
func TestMakeGitlabConfig(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
input *config.Config
|
input *config.Config
|
||||||
expected *gitlab.GitlabClientConfig
|
expected *config.GitlabClientConfig
|
||||||
}{
|
}{
|
||||||
"ValidConfig": {
|
"ValidConfig": {
|
||||||
input: &config.Config{
|
input: &config.Config{
|
||||||
Gitlab: gitlab.GitlabClientConfig{
|
FS: config.FSConfig{
|
||||||
|
Platform: "gitlab",
|
||||||
|
},
|
||||||
|
Gitlab: config.GitlabClientConfig{
|
||||||
URL: "https://gitlab.com",
|
URL: "https://gitlab.com",
|
||||||
PullMethod: "http",
|
PullMethod: "http",
|
||||||
Token: "",
|
Token: "",
|
||||||
|
@ -128,7 +144,7 @@ func TestMakeGitlabConfig(t *testing.T) {
|
||||||
IncludeCurrentUser: true,
|
IncludeCurrentUser: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expected: &gitlab.GitlabClientConfig{
|
expected: &config.GitlabClientConfig{
|
||||||
URL: "https://gitlab.com",
|
URL: "https://gitlab.com",
|
||||||
PullMethod: "http",
|
PullMethod: "http",
|
||||||
Token: "",
|
Token: "",
|
||||||
|
@ -140,7 +156,10 @@ func TestMakeGitlabConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
"InvalidPullMethod": {
|
"InvalidPullMethod": {
|
||||||
input: &config.Config{
|
input: &config.Config{
|
||||||
Gitlab: gitlab.GitlabClientConfig{
|
FS: config.FSConfig{
|
||||||
|
Platform: "gitlab",
|
||||||
|
},
|
||||||
|
Gitlab: config.GitlabClientConfig{
|
||||||
URL: "https://gitlab.com",
|
URL: "https://gitlab.com",
|
||||||
PullMethod: "invalid",
|
PullMethod: "invalid",
|
||||||
Token: "",
|
Token: "",
|
||||||
|
@ -154,7 +173,7 @@ func TestMakeGitlabConfig(t *testing.T) {
|
||||||
},
|
},
|
||||||
"InvalidArchiveHandling": {
|
"InvalidArchiveHandling": {
|
||||||
input: &config.Config{
|
input: &config.Config{
|
||||||
Gitlab: gitlab.GitlabClientConfig{
|
Gitlab: config.GitlabClientConfig{
|
||||||
URL: "https://gitlab.com",
|
URL: "https://gitlab.com",
|
||||||
PullMethod: "http",
|
PullMethod: "http",
|
||||||
Token: "",
|
Token: "",
|
||||||
|
|
|
@ -10,24 +10,15 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/badjware/gitlabfs/config"
|
||||||
"github.com/badjware/gitlabfs/fstree"
|
"github.com/badjware/gitlabfs/fstree"
|
||||||
"github.com/badjware/gitlabfs/utils"
|
"github.com/badjware/gitlabfs/utils"
|
||||||
"github.com/vmihailenco/taskq/v3"
|
"github.com/vmihailenco/taskq/v3"
|
||||||
"github.com/vmihailenco/taskq/v3/memqueue"
|
"github.com/vmihailenco/taskq/v3/memqueue"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitClientParam struct {
|
|
||||||
CloneLocation string `yaml:"clone_location,omitempty"`
|
|
||||||
Remote string `yaml:"remote,omitempty"`
|
|
||||||
OnClone string `yaml:"on_clone,omitempty"`
|
|
||||||
AutoPull bool `yaml:"auto_pull,omitempty"`
|
|
||||||
Depth int `yaml:"depth,omitempty"`
|
|
||||||
QueueSize int `yaml:"queue_size,omitempty"`
|
|
||||||
QueueWorkerCount int `yaml:"worker_count,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gitClient struct {
|
type gitClient struct {
|
||||||
GitClientParam
|
config.GitClientConfig
|
||||||
|
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
|
|
||||||
|
@ -42,11 +33,11 @@ type gitClient struct {
|
||||||
pullTask *taskq.Task
|
pullTask *taskq.Task
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(logger *slog.Logger, p GitClientParam) (*gitClient, error) {
|
func NewClient(logger *slog.Logger, p config.GitClientConfig) (*gitClient, error) {
|
||||||
queueFactory := memqueue.NewFactory()
|
queueFactory := memqueue.NewFactory()
|
||||||
// Create the client
|
// Create the client
|
||||||
c := &gitClient{
|
c := &gitClient{
|
||||||
GitClientParam: p,
|
GitClientConfig: p,
|
||||||
|
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|
||||||
|
|
14
git/clone.go
14
git/clone.go
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
||||||
if c.GitClientParam.OnClone == "init" {
|
if c.GitClientConfig.OnClone == "init" {
|
||||||
// "Fake" cloning the repo by never actually talking to the git server
|
// "Fake" cloning the repo by never actually talking to the git server
|
||||||
// This skip a fetch operation that we would do if we where to do a proper clone
|
// This skip a fetch operation that we would do if we where to do a proper clone
|
||||||
// We can save a lot of time and network i/o doing it this way, at the cost of
|
// We can save a lot of time and network i/o doing it this way, at the cost of
|
||||||
|
@ -40,8 +40,8 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
||||||
"git", "remote", "add",
|
"git", "remote", "add",
|
||||||
"-m", defaultBranch,
|
"-m", defaultBranch,
|
||||||
"--",
|
"--",
|
||||||
c.GitClientParam.Remote, // name
|
c.GitClientConfig.Remote, // name
|
||||||
url, // url
|
url, // url
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", url, dst, err)
|
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", url, dst, err)
|
||||||
|
@ -54,7 +54,7 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
||||||
"git", "config", "--local",
|
"git", "config", "--local",
|
||||||
"--",
|
"--",
|
||||||
fmt.Sprintf("branch.%s.remote", defaultBranch), // key
|
fmt.Sprintf("branch.%s.remote", defaultBranch), // key
|
||||||
c.GitClientParam.Remote, // value
|
c.GitClientConfig.Remote, // value
|
||||||
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -77,10 +77,10 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
||||||
c.logger.Info("Cloning git repository", "directory", dst, "repository", url)
|
c.logger.Info("Cloning git repository", "directory", dst, "repository", url)
|
||||||
args := []string{
|
args := []string{
|
||||||
"clone",
|
"clone",
|
||||||
"--origin", c.GitClientParam.Remote,
|
"--origin", c.GitClientConfig.Remote,
|
||||||
}
|
}
|
||||||
if c.GitClientParam.Depth != 0 {
|
if c.GitClientConfig.Depth != 0 {
|
||||||
args = append(args, "--depth", strconv.Itoa(c.GitClientParam.Depth))
|
args = append(args, "--depth", strconv.Itoa(c.GitClientConfig.Depth))
|
||||||
}
|
}
|
||||||
args = append(args,
|
args = append(args,
|
||||||
"--",
|
"--",
|
||||||
|
|
|
@ -24,13 +24,13 @@ func (c *gitClient) pull(repoPath string, defaultBranch string) error {
|
||||||
args := []string{
|
args := []string{
|
||||||
"pull",
|
"pull",
|
||||||
}
|
}
|
||||||
if c.GitClientParam.Depth != 0 {
|
if c.GitClientConfig.Depth != 0 {
|
||||||
args = append(args, "--depth", strconv.Itoa(c.GitClientParam.Depth))
|
args = append(args, "--depth", strconv.Itoa(c.GitClientConfig.Depth))
|
||||||
}
|
}
|
||||||
args = append(args,
|
args = append(args,
|
||||||
"--",
|
"--",
|
||||||
c.GitClientParam.Remote, // repository
|
c.GitClientConfig.Remote, // repository
|
||||||
defaultBranch, // refspec
|
defaultBranch, // refspec
|
||||||
)
|
)
|
||||||
|
|
||||||
_, err = utils.ExecProcessInDir(c.logger, repoPath, "git", args...)
|
_, err = utils.ExecProcessInDir(c.logger, repoPath, "git", args...)
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -18,6 +18,7 @@ require (
|
||||||
github.com/go-redis/redis/v8 v8.11.4 // indirect
|
github.com/go-redis/redis/v8 v8.11.4 // indirect
|
||||||
github.com/go-redis/redis_rate/v9 v9.1.2 // indirect
|
github.com/go-redis/redis_rate/v9 v9.1.2 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
|
github.com/google/go-github/v63 v63.0.0 // indirect
|
||||||
github.com/google/go-querystring v1.1.0 // indirect
|
github.com/google/go-querystring v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||||
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
|
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -114,6 +114,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
|
||||||
|
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
|
||||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||||
|
|
26
main.go
26
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/badjware/gitlabfs/config"
|
"github.com/badjware/gitlabfs/config"
|
||||||
"github.com/badjware/gitlabfs/fstree"
|
"github.com/badjware/gitlabfs/fstree"
|
||||||
"github.com/badjware/gitlabfs/git"
|
"github.com/badjware/gitlabfs/git"
|
||||||
|
"github.com/badjware/gitlabfs/platforms/github"
|
||||||
"github.com/badjware/gitlabfs/platforms/gitlab"
|
"github.com/badjware/gitlabfs/platforms/gitlab"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -64,20 +65,31 @@ func main() {
|
||||||
}
|
}
|
||||||
gitClient, _ := git.NewClient(logger, *gitClientParam)
|
gitClient, _ := git.NewClient(logger, *gitClientParam)
|
||||||
|
|
||||||
// Create the gitlab client
|
var gitPlatformClient fstree.GitPlatform
|
||||||
GitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
|
if loadedConfig.FS.Platform == config.PlatformGitlab {
|
||||||
if err != nil {
|
// Create the gitlab client
|
||||||
fmt.Println(err)
|
GitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig)
|
||||||
os.Exit(1)
|
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)
|
||||||
}
|
}
|
||||||
gitlabClient, _ := gitlab.NewClient(logger, loadedConfig.Gitlab.URL, loadedConfig.Gitlab.Token, *GitlabClientConfig)
|
|
||||||
|
|
||||||
// Start the filesystem
|
// Start the filesystem
|
||||||
err = fstree.Start(
|
err = fstree.Start(
|
||||||
logger,
|
logger,
|
||||||
mountpoint,
|
mountpoint,
|
||||||
parsedMountoptions,
|
parsedMountoptions,
|
||||||
&fstree.FSParam{GitClient: gitClient, GitPlatform: gitlabClient},
|
&fstree.FSParam{GitClient: gitClient, GitPlatform: gitPlatformClient},
|
||||||
*debug,
|
*debug,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package github
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/badjware/gitlabfs/config"
|
||||||
|
"github.com/badjware/gitlabfs/fstree"
|
||||||
|
"github.com/google/go-github/v63/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
type githubClient struct {
|
||||||
|
config.GithubClientConfig
|
||||||
|
client *github.Client
|
||||||
|
|
||||||
|
logger *slog.Logger
|
||||||
|
|
||||||
|
rootContent map[string]fstree.GroupSource
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(logger *slog.Logger, config config.GithubClientConfig) (*githubClient, error) {
|
||||||
|
client := github.NewClient(nil)
|
||||||
|
if config.Token != "" {
|
||||||
|
client = client.WithAuthToken(config.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
gitHubClient := &githubClient{
|
||||||
|
GithubClientConfig: config,
|
||||||
|
client: client,
|
||||||
|
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
return gitHubClient, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *githubClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *githubClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package github
|
|
@ -0,0 +1 @@
|
||||||
|
package github
|
|
@ -6,31 +6,13 @@ import (
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/badjware/gitlabfs/config"
|
||||||
"github.com/badjware/gitlabfs/fstree"
|
"github.com/badjware/gitlabfs/fstree"
|
||||||
"github.com/xanzy/go-gitlab"
|
"github.com/xanzy/go-gitlab"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
PullMethodHTTP = "http"
|
|
||||||
PullMethodSSH = "ssh"
|
|
||||||
|
|
||||||
ArchivedProjectShow = "show"
|
|
||||||
ArchivedProjectHide = "hide"
|
|
||||||
ArchivedProjectIgnore = "ignore"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GitlabClientConfig struct {
|
|
||||||
URL string `yaml:"url,omitempty"`
|
|
||||||
Token string `yaml:"token,omitempty"`
|
|
||||||
GroupIDs []int `yaml:"group_ids,omitempty"`
|
|
||||||
UserIDs []int `yaml:"user_ids,omitempty"`
|
|
||||||
ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"`
|
|
||||||
IncludeCurrentUser bool `yaml:"include_current_user,omitempty"`
|
|
||||||
PullMethod string `yaml:"pull_method,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gitlabClient struct {
|
type gitlabClient struct {
|
||||||
GitlabClientConfig
|
config.GitlabClientConfig
|
||||||
client *gitlab.Client
|
client *gitlab.Client
|
||||||
|
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
|
@ -46,7 +28,7 @@ type gitlabClient struct {
|
||||||
userCache map[int]*User
|
userCache map[int]*User
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(logger *slog.Logger, gitlabUrl string, gitlabToken string, p GitlabClientConfig) (*gitlabClient, error) {
|
func NewClient(logger *slog.Logger, gitlabUrl string, gitlabToken string, p config.GitlabClientConfig) (*gitlabClient, error) {
|
||||||
client, err := gitlab.NewClient(
|
client, err := gitlab.NewClient(
|
||||||
gitlabToken,
|
gitlabToken,
|
||||||
gitlab.WithBaseURL(gitlabUrl),
|
gitlab.WithBaseURL(gitlabUrl),
|
||||||
|
|
|
@ -3,6 +3,7 @@ package gitlab
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/badjware/gitlabfs/config"
|
||||||
"github.com/xanzy/go-gitlab"
|
"github.com/xanzy/go-gitlab"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,7 +28,7 @@ func (p *Project) GetDefaultBranch() string {
|
||||||
|
|
||||||
func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) *Project {
|
func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) *Project {
|
||||||
// https://godoc.org/github.com/xanzy/go-gitlab#Project
|
// https://godoc.org/github.com/xanzy/go-gitlab#Project
|
||||||
if c.ArchivedProjectHandling == ArchivedProjectIgnore && project.Archived {
|
if c.ArchivedProjectHandling == config.ArchivedProjectIgnore && project.Archived {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p := Project{
|
p := Project{
|
||||||
|
@ -38,12 +39,12 @@ func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) *Pro
|
||||||
if p.DefaultBranch == "" {
|
if p.DefaultBranch == "" {
|
||||||
p.DefaultBranch = "master"
|
p.DefaultBranch = "master"
|
||||||
}
|
}
|
||||||
if c.PullMethod == PullMethodSSH {
|
if c.PullMethod == config.PullMethodSSH {
|
||||||
p.CloneURL = project.SSHURLToRepo
|
p.CloneURL = project.SSHURLToRepo
|
||||||
} else {
|
} else {
|
||||||
p.CloneURL = project.HTTPURLToRepo
|
p.CloneURL = project.HTTPURLToRepo
|
||||||
}
|
}
|
||||||
if c.ArchivedProjectHandling == ArchivedProjectHide && project.Archived {
|
if c.ArchivedProjectHandling == config.ArchivedProjectHide && project.Archived {
|
||||||
p.Path = path.Join(path.Dir(p.Path), "."+path.Base(p.Path))
|
p.Path = path.Join(path.Dir(p.Path), "."+path.Base(p.Path))
|
||||||
}
|
}
|
||||||
return &p
|
return &p
|
||||||
|
|
Loading…
Reference in New Issue