166 lines
5.2 KiB
Go
166 lines
5.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
const (
|
|
PlatformGitlab = "gitlab"
|
|
PlatformGithub = "github"
|
|
|
|
PullMethodHTTP = "http"
|
|
PullMethodSSH = "ssh"
|
|
|
|
ArchivedProjectShow = "show"
|
|
ArchivedProjectHide = "hide"
|
|
ArchivedProjectIgnore = "ignore"
|
|
)
|
|
|
|
type (
|
|
Config struct {
|
|
FS FSConfig `yaml:"fs,omitempty"`
|
|
Gitlab GitlabClientConfig `yaml:"gitlab,omitempty"`
|
|
Github GithubClientConfig `yaml:"github,omitempty"`
|
|
Git GitClientConfig `yaml:"git,omitempty"`
|
|
}
|
|
FSConfig struct {
|
|
Mountpoint string `yaml:"mountpoint,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"`
|
|
}
|
|
)
|
|
|
|
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",
|
|
Platform: "",
|
|
},
|
|
Gitlab: GitlabClientConfig{
|
|
URL: "https://gitlab.com",
|
|
Token: "",
|
|
PullMethod: "http",
|
|
GroupIDs: []int{9970},
|
|
UserIDs: []int{},
|
|
ArchivedProjectHandling: "hide",
|
|
IncludeCurrentUser: true,
|
|
},
|
|
Github: GithubClientConfig{
|
|
Token: "",
|
|
PullMethod: "http",
|
|
OrgNames: []string{},
|
|
UserNames: []string{},
|
|
ArchivedRepoHandling: "hide",
|
|
IncludeCurrentUser: true,
|
|
},
|
|
Git: GitClientConfig{
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
func MakeGitlabConfig(config *Config) (*GitlabClientConfig, error) {
|
|
// parse pull_method
|
|
if config.Gitlab.PullMethod != PullMethodHTTP && config.Gitlab.PullMethod != PullMethodSSH {
|
|
return nil, fmt.Errorf("gitlab.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("gitlab.archived_project_handling must be either \"%v\", \"%v\" or \"%v\"", ArchivedProjectShow, ArchivedProjectHide, ArchivedProjectIgnore)
|
|
}
|
|
|
|
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
|
|
}
|