forked from Plemya-x/ALR
		
	Initial commit
This commit is contained in:
		
							
								
								
									
										79
									
								
								internal/config/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								internal/config/config.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
/*
 | 
			
		||||
 * LURE - Linux User REpository
 | 
			
		||||
 * Copyright (C) 2023 Elara Musayelyan
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"os"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/pelletier/go-toml/v2"
 | 
			
		||||
	"lure.sh/lure/internal/types"
 | 
			
		||||
	"lure.sh/lure/pkg/loggerctx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var defaultConfig = &types.Config{
 | 
			
		||||
	RootCmd:          "sudo",
 | 
			
		||||
	PagerStyle:       "native",
 | 
			
		||||
	IgnorePkgUpdates: []string{},
 | 
			
		||||
	Repos: []types.Repo{
 | 
			
		||||
		{
 | 
			
		||||
			Name: "default",
 | 
			
		||||
			URL:  "https://github.com/lure-sh/lure-repo.git",
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	configMtx sync.Mutex
 | 
			
		||||
	config    *types.Config
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Config returns a LURE configuration struct.
 | 
			
		||||
// The first time it's called, it'll load the config from a file.
 | 
			
		||||
// Subsequent calls will just return the same value.
 | 
			
		||||
func Config(ctx context.Context) *types.Config {
 | 
			
		||||
	configMtx.Lock()
 | 
			
		||||
	defer configMtx.Unlock()
 | 
			
		||||
	log := loggerctx.From(ctx)
 | 
			
		||||
 | 
			
		||||
	if config == nil {
 | 
			
		||||
		cfgFl, err := os.Open(GetPaths(ctx).ConfigPath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("Error opening config file, using defaults").Err(err).Send()
 | 
			
		||||
			return defaultConfig
 | 
			
		||||
		}
 | 
			
		||||
		defer cfgFl.Close()
 | 
			
		||||
 | 
			
		||||
		// Copy the default configuration into config
 | 
			
		||||
		defCopy := *defaultConfig
 | 
			
		||||
		config = &defCopy
 | 
			
		||||
		config.Repos = nil
 | 
			
		||||
 | 
			
		||||
		err = toml.NewDecoder(cfgFl).Decode(config)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("Error decoding config file, using defaults").Err(err).Send()
 | 
			
		||||
			// Set config back to nil so that we try again next time
 | 
			
		||||
			config = nil
 | 
			
		||||
			return defaultConfig
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return config
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										67
									
								
								internal/config/lang.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								internal/config/lang.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
/*
 | 
			
		||||
 * LURE - Linux User REpository
 | 
			
		||||
 * Copyright (C) 2023 Elara Musayelyan
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"lure.sh/lure/pkg/loggerctx"
 | 
			
		||||
	"golang.org/x/text/language"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	langMtx sync.Mutex
 | 
			
		||||
	lang    language.Tag
 | 
			
		||||
	langSet bool
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Language returns the system language.
 | 
			
		||||
// The first time it's called, it'll detect the langauge based on
 | 
			
		||||
// the $LANG environment variable.
 | 
			
		||||
// Subsequent calls will just return the same value.
 | 
			
		||||
func Language(ctx context.Context) language.Tag {
 | 
			
		||||
	langMtx.Lock()
 | 
			
		||||
	defer langMtx.Unlock()
 | 
			
		||||
	log := loggerctx.From(ctx)
 | 
			
		||||
	if !langSet {
 | 
			
		||||
		syslang := SystemLang()
 | 
			
		||||
		tag, err := language.Parse(syslang)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Error parsing system language").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
		base, _ := tag.Base()
 | 
			
		||||
		lang = language.Make(base.String())
 | 
			
		||||
		langSet = true
 | 
			
		||||
	}
 | 
			
		||||
	return lang
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SystemLang returns the system language based on
 | 
			
		||||
// the $LANG environment variable.
 | 
			
		||||
func SystemLang() string {
 | 
			
		||||
	lang := os.Getenv("LANG")
 | 
			
		||||
	lang, _, _ = strings.Cut(lang, ".")
 | 
			
		||||
	if lang == "" || lang == "C" {
 | 
			
		||||
		lang = "en"
 | 
			
		||||
	}
 | 
			
		||||
	return lang
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										108
									
								
								internal/config/paths.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								internal/config/paths.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
/*
 | 
			
		||||
 * LURE - Linux User REpository
 | 
			
		||||
 * Copyright (C) 2023 Elara Musayelyan
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/pelletier/go-toml/v2"
 | 
			
		||||
	"lure.sh/lure/pkg/loggerctx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Paths contains various paths used by LURE
 | 
			
		||||
type Paths struct {
 | 
			
		||||
	ConfigDir  string
 | 
			
		||||
	ConfigPath string
 | 
			
		||||
	CacheDir   string
 | 
			
		||||
	RepoDir    string
 | 
			
		||||
	PkgsDir    string
 | 
			
		||||
	DBPath     string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	pathsMtx sync.Mutex
 | 
			
		||||
	paths    *Paths
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GetPaths returns a Paths struct.
 | 
			
		||||
// The first time it's called, it'll generate the struct
 | 
			
		||||
// using information from the system.
 | 
			
		||||
// Subsequent calls will return the same value.
 | 
			
		||||
func GetPaths(ctx context.Context) *Paths {
 | 
			
		||||
	pathsMtx.Lock()
 | 
			
		||||
	defer pathsMtx.Unlock()
 | 
			
		||||
 | 
			
		||||
	log := loggerctx.From(ctx)
 | 
			
		||||
	if paths == nil {
 | 
			
		||||
		paths = &Paths{}
 | 
			
		||||
 | 
			
		||||
		cfgDir, err := os.UserConfigDir()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Unable to detect user config directory").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		paths.ConfigDir = filepath.Join(cfgDir, "lure")
 | 
			
		||||
 | 
			
		||||
		err = os.MkdirAll(paths.ConfigDir, 0o755)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Unable to create LURE config directory").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		paths.ConfigPath = filepath.Join(paths.ConfigDir, "lure.toml")
 | 
			
		||||
 | 
			
		||||
		if _, err := os.Stat(paths.ConfigPath); err != nil {
 | 
			
		||||
			cfgFl, err := os.Create(paths.ConfigPath)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Fatal("Unable to create LURE config file").Err(err).Send()
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = toml.NewEncoder(cfgFl).Encode(&defaultConfig)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Fatal("Error encoding default configuration").Err(err).Send()
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			cfgFl.Close()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		cacheDir, err := os.UserCacheDir()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Unable to detect cache directory").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		paths.CacheDir = filepath.Join(cacheDir, "lure")
 | 
			
		||||
		paths.RepoDir = filepath.Join(paths.CacheDir, "repo")
 | 
			
		||||
		paths.PkgsDir = filepath.Join(paths.CacheDir, "pkgs")
 | 
			
		||||
 | 
			
		||||
		err = os.MkdirAll(paths.RepoDir, 0o755)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Unable to create repo cache directory").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = os.MkdirAll(paths.PkgsDir, 0o755)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Unable to create package cache directory").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		paths.DBPath = filepath.Join(paths.CacheDir, "db")
 | 
			
		||||
	}
 | 
			
		||||
	return paths
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								internal/config/version.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								internal/config/version.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
// Version contains the version of LURE. If the version
 | 
			
		||||
// isn't known, it'll be set to "unknown"
 | 
			
		||||
var Version = "unknown"
 | 
			
		||||
		Reference in New Issue
	
	Block a user