summaryrefslogtreecommitdiff
path: root/pkg/app
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/app')
-rw-r--r--pkg/app/app.go25
-rw-r--r--pkg/app/config.go96
-rw-r--r--pkg/app/files.go2
3 files changed, 119 insertions, 4 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go
index 624b048..e281abc 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -1,6 +1,8 @@
package app
import (
+ "os"
+
"github.com/isacikgoz/gitbatch/pkg/gui"
log "github.com/sirupsen/logrus"
)
@@ -14,13 +16,30 @@ type App struct {
// Setup will handle pre-required operations. It is designed to be a wrapper for
// main method right now.
-func Setup(directory, repoPattern, logLevel string) (*App, error) {
+func Setup(directory, repoPattern, logLevel string, ignoreConfig bool) (*App, error) {
// initiate the app and give it initial values
app := &App{}
- app.Config, _ = LoadConfiguration()
setLogLevel(logLevel)
var err error
- directories := generateDirectories(directory, repoPattern)
+ app.Config, err = LoadConfiguration()
+ if err != nil {
+ // the error types and handling is not considered yer
+ log.Error(err)
+ return app, err
+ }
+ workingDirectory, _ := os.Getwd()
+
+ directories := make([]string, 0)
+ if len(app.Config.Directories) <= 0 || ignoreConfig ||
+ (workingDirectory != directory && len(app.Config.Directories) > 0 ){
+ directories = generateDirectories(directory, repoPattern)
+ } else {
+ for _, dir := range app.Config.Directories {
+ for _, d := range generateDirectories(dir, repoPattern) {
+ directories = append(directories, d)
+ }
+ }
+ }
// create a gui.Gui struct and set it as App's gui
app.Gui, err = gui.NewGui(app.Config.Mode, directories)
diff --git a/pkg/app/config.go b/pkg/app/config.go
index d34e647..815b5b0 100644
--- a/pkg/app/config.go
+++ b/pkg/app/config.go
@@ -1,16 +1,112 @@
package app
import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "github.com/spf13/viper"
+ log "github.com/sirupsen/logrus"
)
+// Config type is the configuration entity of the application
type Config struct {
Mode string
Directories []string
}
+// config file stuff
+var (
+ configFileName = "config"
+ configFileExt = ".yml"
+ configType = "yaml"
+ appName = "gitbatch"
+
+ configurationDirectory = filepath.Join(osConfigDirectory(), appName)
+ configFileAbsPath = filepath.Join(configurationDirectory, configFileName)
+)
+
+// configuration items
+var (
+ modeKey = "mode"
+ modeKeyDefault = "fetch"
+ pathsKey = "paths"
+ pathsKeyDefault = []string{"."}
+)
+
+// LoadConfiguration returns a Config struct is filled
func LoadConfiguration() (*Config, error) {
+ if err := initializeConfigurationManager(); err != nil {
+ return nil, err
+ }
+ if err := setDefaults(); err != nil {
+ return nil, err
+ }
+ if err := readConfiguration(); err != nil {
+ return nil, err
+ }
config := &Config{
+ Mode: viper.GetString(modeKey),
+ Directories: viper.GetStringSlice(pathsKey),
}
return config, nil
+}
+
+// set default configuration parameters
+func setDefaults() error {
+ viper.SetDefault(modeKey, modeKeyDefault)
+ // viper.SetDefault(pathsKey, pathsKeyDefault)
+ return nil
+}
+
+// read configuration from file
+func readConfiguration() error{
+ err := viper.ReadInConfig() // Find and read the config file
+ if err != nil { // Handle errors reading the config file
+ // if file does not exist, simply create one
+ if _, err := os.Stat(configFileAbsPath+configFileExt); os.IsNotExist(err) {
+ os.MkdirAll(configurationDirectory, 0755)
+ os.Create(configFileAbsPath+configFileExt)
+ } else {
+ return err
+ }
+ // let's write defaults
+ if err := viper.WriteConfig(); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// write configuration to a file
+func writeConfiguration() error{
+ if err := viper.WriteConfig(); err != nil {
+ return err
+ }
+ return nil
+}
+
+// initialize the configuration manager
+func initializeConfigurationManager() error {
+ // config viper
+ viper.AddConfigPath(configurationDirectory)
+ viper.SetConfigName(configFileName)
+ viper.SetConfigType(configType)
+
+ return nil
+}
+
+// returns OS dependent config directory
+func osConfigDirectory() (osConfigDirectory string) {
+ switch osname := runtime.GOOS; osname {
+ case "windows":
+ osConfigDirectory = os.Getenv("APPDATA")
+ case "darwin":
+ osConfigDirectory = os.Getenv("HOME") + "/Library/Application Support"
+ case "linux":
+ osConfigDirectory = os.Getenv("HOME") + "/.config"
+ default:
+ log.Warn("Operating system couldn't be recognized")
+ }
+ return osConfigDirectory
} \ No newline at end of file
diff --git a/pkg/app/files.go b/pkg/app/files.go
index f6ae947..b210c66 100644
--- a/pkg/app/files.go
+++ b/pkg/app/files.go
@@ -52,7 +52,7 @@ func filterDirectories(files []os.FileInfo, repoPattern string) []os.FileInfo {
var filteredRepos []os.FileInfo
for _, f := range files {
// it is just a simple filter
- if strings.Contains(f.Name(), repoPattern) {
+ if strings.Contains(f.Name(), repoPattern) && f.Name() != ".git" {
filteredRepos = append(filteredRepos, f)
} else {
continue