From 96f250fc4757550503f69c91901bc6ee8884a4d2 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Fri, 7 Jun 2024 23:36:45 -0400 Subject: [PATCH] move config loader to its own package --- config/loader.go | 89 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 93 ++++-------------------------------------------- 2 files changed, 96 insertions(+), 86 deletions(-) create mode 100644 config/loader.go diff --git a/config/loader.go b/config/loader.go new file mode 100644 index 0000000..a0d244e --- /dev/null +++ b/config/loader.go @@ -0,0 +1,89 @@ +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: "", + 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 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 +} diff --git a/main.go b/main.go index 41c74c6..6cb9626 100644 --- a/main.go +++ b/main.go @@ -5,93 +5,14 @@ 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)") @@ -105,7 +26,7 @@ func main() { } flag.Parse() - config, err := loadConfig(*configPath) + loadedConfig, err := config.LoadConfig(*configPath) if err != nil { fmt.Println(err) os.Exit(1) @@ -115,7 +36,7 @@ func main() { logger := slog.Default() // Configure mountpoint - mountpoint := config.FS.Mountpoint + mountpoint := loadedConfig.FS.Mountpoint if flag.NArg() == 1 { mountpoint = flag.Arg(0) } @@ -126,7 +47,7 @@ func main() { } // Configure mountoptions - mountoptions := config.FS.MountOptions + mountoptions := loadedConfig.FS.MountOptions if *mountoptionsFlag != "" { mountoptions = *mountoptionsFlag } @@ -136,7 +57,7 @@ func main() { } // Create the git client - gitClientParam, err := makeGitConfig(config) + gitClientParam, err := config.MakeGitConfig(loadedConfig) if err != nil { fmt.Println(err) os.Exit(1) @@ -144,12 +65,12 @@ func main() { gitClient, _ := git.NewClient(logger, *gitClientParam) // Create the gitlab client - GitlabClientConfig, err := makeGitlabConfig(config) + GitlabClientConfig, err := config.MakeGitlabConfig(loadedConfig) if err != nil { fmt.Println(err) os.Exit(1) } - gitlabClient, _ := gitlab.NewClient(logger, config.Gitlab.URL, config.Gitlab.Token, *GitlabClientConfig) + gitlabClient, _ := gitlab.NewClient(logger, loadedConfig.Gitlab.URL, loadedConfig.Gitlab.Token, *GitlabClientConfig) // Start the filesystem err = fstree.Start(