luet/cmd/root.go

136 lines
3.7 KiB
Go
Raw Normal View History

2019-10-29 16:36:48 +00:00
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
//
// 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 2 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 cmd
import (
"os"
"path/filepath"
2019-12-27 19:12:08 +00:00
"strings"
2019-10-29 16:36:48 +00:00
"github.com/marcsauter/single"
2019-12-27 19:12:08 +00:00
config "github.com/mudler/luet/pkg/config"
. "github.com/mudler/luet/pkg/logger"
2019-12-27 19:12:08 +00:00
repo "github.com/mudler/luet/pkg/repository"
2019-10-29 16:36:48 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var Verbose bool
2019-12-27 19:12:08 +00:00
const (
LuetCLIVersion = "0.4-dev"
LuetEnvPrefix = "LUET"
)
2019-10-29 16:36:48 +00:00
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "luet",
Short: "Package manager for the XXth century!",
2019-11-17 18:45:20 +00:00
Long: `Package manager which uses containers to build packages`,
Version: LuetCLIVersion,
2019-12-27 19:12:08 +00:00
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := LoadConfig(config.LuetCfg)
if err != nil {
Fatal("failed to load configuration:", err.Error())
}
},
}
func LoadConfig(c *config.LuetConfig) error {
// If a config file is found, read it in.
if err := c.Viper.ReadInConfig(); err == nil {
Info("Using config file:", c.Viper.ConfigFileUsed())
} else {
Warning(err)
}
err := c.Viper.Unmarshal(&config.LuetCfg)
if err != nil {
return err
}
NewSpinner()
2019-12-27 19:12:08 +00:00
if c.GetLogging().Path != "" {
// TODO: Init logrus, etc.
}
// Load repositories
err = repo.LoadRepositories(c)
if err != nil {
return err
}
return nil
2019-10-29 16:36:48 +00:00
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
2019-11-26 17:05:13 +00:00
// XXX: This is mostly from scratch images.
if os.Getenv("LUET_NOLOCK") != "true" {
s := single.New("luet")
if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
Fatal("another instance of the app is already running, exiting")
} else if err != nil {
// Another error occurred, might be worth handling it as well
Fatal("failed to acquire exclusive app lock:", err.Error())
}
defer s.TryUnlock()
}
2019-10-29 16:36:48 +00:00
if err := RootCmd.Execute(); err != nil {
Error(err)
2019-10-29 16:36:48 +00:00
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luet.yaml)")
RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
2019-10-29 16:36:48 +00:00
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
Error(err)
os.Exit(1)
2019-10-29 16:36:48 +00:00
}
2019-12-27 19:12:08 +00:00
viper.SetEnvPrefix(LuetEnvPrefix)
viper.SetConfigType("yaml")
2019-10-29 16:36:48 +00:00
viper.SetConfigName(".luet") // name of config file (without extension)
if cfgFile != "" { // enable ability to specify config file via flag
Info(">>> cfgFile: ", cfgFile)
2019-10-29 16:36:48 +00:00
viper.SetConfigFile(cfgFile)
2019-12-27 19:12:08 +00:00
} else {
viper.AddConfigPath(dir)
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("/etc/luet")
2019-10-29 16:36:48 +00:00
}
viper.AutomaticEnv() // read in environment variables that match
2019-12-27 19:12:08 +00:00
// Create EnvKey Replacer for handle complex structure
replacer := strings.NewReplacer(".", "__")
viper.SetEnvKeyReplacer(replacer)
viper.SetTypeByDefaultValue(true)
2019-10-29 16:36:48 +00:00
}