2024-06-08 03:36:45 +00:00
package config
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
2024-08-04 19:44:50 +00:00
const (
2024-08-05 01:45:51 +00:00
ForgeGitlab = "gitlab"
ForgeGithub = "github"
2024-08-09 20:13:57 +00:00
ForgeGitea = "gitea"
2024-08-04 19:44:50 +00:00
PullMethodHTTP = "http"
PullMethodSSH = "ssh"
ArchivedProjectShow = "show"
ArchivedProjectHide = "hide"
ArchivedProjectIgnore = "ignore"
)
2024-06-08 03:36:45 +00:00
type (
Config struct {
2024-08-04 19:44:50 +00:00
FS FSConfig ` yaml:"fs,omitempty" `
Gitlab GitlabClientConfig ` yaml:"gitlab,omitempty" `
Github GithubClientConfig ` yaml:"github,omitempty" `
2024-08-09 20:13:57 +00:00
Gitea GiteaClientConfig ` yaml:"gitea,omitempty" `
2024-08-04 19:44:50 +00:00
Git GitClientConfig ` yaml:"git,omitempty" `
2024-06-08 03:36:45 +00:00
}
FSConfig struct {
Mountpoint string ` yaml:"mountpoint,omitempty" `
MountOptions string ` yaml:"mountoptions,omitempty" `
2024-08-05 01:45:51 +00:00
Forge string ` yaml:"forge,omitempty" `
2024-08-04 19:44:50 +00:00
}
GitlabClientConfig struct {
URL string ` yaml:"url,omitempty" `
Token string ` yaml:"token,omitempty" `
2024-08-14 04:01:47 +00:00
GroupIDs [ ] int ` yaml:"group_ids,omitempty" `
UserNames [ ] string ` yaml:"user_names,omitempty" `
2024-08-04 19:44:50 +00:00
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" `
}
2024-08-09 20:13:57 +00:00
GiteaClientConfig struct {
URL string ` yaml:"url,omitempty" `
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" `
}
2024-08-04 19:44:50 +00:00
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" `
2024-06-08 03:36:45 +00:00
}
)
func LoadConfig ( configPath string ) ( * Config , error ) {
// defaults
dataHome := os . Getenv ( "XDG_DATA_HOME" )
if dataHome == "" {
dataHome = filepath . Join ( os . Getenv ( "HOME" ) , ".local/share" )
}
2024-08-14 02:55:18 +00:00
defaultCloneLocation := filepath . Join ( dataHome , "gitforgefs" )
2024-06-08 03:36:45 +00:00
config := & Config {
FS : FSConfig {
Mountpoint : "" ,
MountOptions : "nodev,nosuid" ,
2024-08-05 01:45:51 +00:00
Forge : "" ,
2024-06-08 03:36:45 +00:00
} ,
2024-08-04 19:44:50 +00:00
Gitlab : GitlabClientConfig {
2024-07-18 04:25:13 +00:00
URL : "https://gitlab.com" ,
Token : "" ,
PullMethod : "http" ,
GroupIDs : [ ] int { 9970 } ,
2024-08-14 04:01:47 +00:00
UserNames : [ ] string { } ,
2024-07-18 04:25:13 +00:00
ArchivedProjectHandling : "hide" ,
IncludeCurrentUser : true ,
2024-06-08 03:36:45 +00:00
} ,
2024-08-04 19:44:50 +00:00
Github : GithubClientConfig {
Token : "" ,
PullMethod : "http" ,
OrgNames : [ ] string { } ,
UserNames : [ ] string { } ,
ArchivedRepoHandling : "hide" ,
IncludeCurrentUser : true ,
} ,
Git : GitClientConfig {
2024-06-08 03:36:45 +00:00
CloneLocation : defaultCloneLocation ,
Remote : "origin" ,
OnClone : "init" ,
AutoPull : false ,
Depth : 0 ,
QueueSize : 200 ,
QueueWorkerCount : 5 ,
} ,
}
2024-08-14 02:22:50 +00:00
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 )
2024-06-08 03:36:45 +00:00
}
2024-08-05 01:45:51 +00:00
// validate forge is set
2024-08-09 20:13:57 +00:00
if config . FS . Forge != ForgeGithub && config . FS . Forge != ForgeGitlab && config . FS . Forge != ForgeGitea {
return nil , fmt . Errorf ( "fs.forge must be either \"%v\", \"%v\", or \"%v\"" , ForgeGitlab , ForgeGithub , ForgeGitea )
2024-08-04 19:44:50 +00:00
}
2024-06-08 03:36:45 +00:00
return config , nil
}
2024-08-04 19:44:50 +00:00
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 )
2024-06-08 03:36:45 +00:00
}
2024-08-04 19:44:50 +00:00
// 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
2024-06-08 03:36:45 +00:00
}
2024-08-04 19:44:50 +00:00
func MakeGithubConfig ( config * Config ) ( * GithubClientConfig , error ) {
2024-06-08 03:36:45 +00:00
// parse pull_method
2024-08-09 20:13:57 +00:00
if config . Github . PullMethod != PullMethodHTTP && config . Github . PullMethod != PullMethodSSH {
2024-08-04 19:44:50 +00:00
return nil , fmt . Errorf ( "github.pull_method must be either \"%v\" or \"%v\"" , PullMethodHTTP , PullMethodSSH )
2024-06-08 03:36:45 +00:00
}
2024-07-18 04:25:13 +00:00
// parse archive_handing
2024-08-09 20:13:57 +00:00
if config . Github . ArchivedRepoHandling != ArchivedProjectShow && config . Github . ArchivedRepoHandling != ArchivedProjectHide && config . Github . ArchivedRepoHandling != ArchivedProjectIgnore {
2024-08-04 19:44:50 +00:00
return nil , fmt . Errorf ( "github.archived_repo_handling must be either \"%v\", \"%v\" or \"%v\"" , ArchivedProjectShow , ArchivedProjectHide , ArchivedProjectIgnore )
2024-07-18 04:25:13 +00:00
}
2024-08-04 19:44:50 +00:00
return & config . Github , nil
}
2024-08-09 20:13:57 +00:00
func MakeGiteaConfig ( config * Config ) ( * GiteaClientConfig , error ) {
// parse pull_method
if config . Gitea . PullMethod != PullMethodHTTP && config . Gitea . PullMethod != PullMethodSSH {
return nil , fmt . Errorf ( "gitea.pull_method must be either \"%v\" or \"%v\"" , PullMethodHTTP , PullMethodSSH )
}
// parse archive_handing
if config . Gitea . ArchivedRepoHandling != ArchivedProjectShow && config . Gitea . ArchivedRepoHandling != ArchivedProjectHide && config . Gitea . ArchivedRepoHandling != ArchivedProjectIgnore {
return nil , fmt . Errorf ( "gitea.archived_repo_handling must be either \"%v\", \"%v\" or \"%v\"" , ArchivedProjectShow , ArchivedProjectHide , ArchivedProjectIgnore )
}
return & config . Gitea , nil
}
2024-08-04 19:44:50 +00:00
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
2024-06-08 03:36:45 +00:00
}