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 (
|
2020-02-18 22:08:14 +00:00
|
|
|
"fmt"
|
2019-10-29 16:36:48 +00:00
|
|
|
"os"
|
2020-02-01 16:58:23 +00:00
|
|
|
"os/user"
|
2019-10-29 16:36:48 +00:00
|
|
|
"path/filepath"
|
2019-12-29 15:33:10 +00:00
|
|
|
"runtime"
|
2019-12-27 19:12:08 +00:00
|
|
|
"strings"
|
2019-10-29 16:36:48 +00:00
|
|
|
|
2019-11-25 19:28:08 +00:00
|
|
|
"github.com/marcsauter/single"
|
2020-11-13 17:25:44 +00:00
|
|
|
bus "github.com/mudler/luet/pkg/bus"
|
|
|
|
|
2020-06-12 15:54:57 +00:00
|
|
|
extensions "github.com/mudler/cobra-extensions"
|
2019-12-27 19:12:08 +00:00
|
|
|
config "github.com/mudler/luet/pkg/config"
|
2020-02-18 22:08:14 +00:00
|
|
|
helpers "github.com/mudler/luet/pkg/helpers"
|
2019-11-02 09:26:28 +00:00
|
|
|
. "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
|
2020-03-22 14:19:08 +00:00
|
|
|
var LockedCommands = []string{"install", "uninstall", "upgrade"}
|
2019-10-29 16:36:48 +00:00
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
const (
|
2020-11-20 18:02:54 +00:00
|
|
|
LuetCLIVersion = "0.9.5"
|
2019-12-27 19:12:08 +00:00
|
|
|
LuetEnvPrefix = "LUET"
|
|
|
|
)
|
2019-11-17 15:12:12 +00:00
|
|
|
|
2020-04-12 12:37:49 +00:00
|
|
|
// Build time and commit information.
|
|
|
|
//
|
|
|
|
// ⚠️ WARNING: should only be set by "-ldflags".
|
|
|
|
var (
|
|
|
|
BuildTime string
|
|
|
|
BuildCommit string
|
|
|
|
)
|
|
|
|
|
2019-10-29 16:36:48 +00:00
|
|
|
// RootCmd represents the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
2019-11-17 15:12:12 +00:00
|
|
|
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`,
|
2020-04-12 12:37:49 +00:00
|
|
|
Version: fmt.Sprintf("%s-g%s %s", LuetCLIVersion, BuildCommit, BuildTime),
|
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())
|
|
|
|
}
|
2020-04-30 18:29:28 +00:00
|
|
|
// Initialize tmpdir prefix. TODO: Move this with LoadConfig
|
|
|
|
// directly on sub command to ensure the creation only when it's
|
|
|
|
// needed.
|
|
|
|
err = config.LuetCfg.GetSystem().InitTmpDir()
|
|
|
|
if err != nil {
|
|
|
|
Fatal("failed on init tmp basedir:", err.Error())
|
|
|
|
}
|
2020-11-13 17:25:44 +00:00
|
|
|
|
|
|
|
viper.BindPFlag("plugin", cmd.Flags().Lookup("plugin"))
|
|
|
|
|
|
|
|
plugin := viper.GetStringSlice("plugin")
|
|
|
|
|
|
|
|
bus.Manager.Load(plugin...).Register()
|
|
|
|
if len(bus.Manager.Plugins) != 0 {
|
|
|
|
Info(":lollipop:Enabled plugins:")
|
|
|
|
for _, p := range bus.Manager.Plugins {
|
|
|
|
Info("\t:arrow_right:", p.Name)
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 18:29:28 +00:00
|
|
|
},
|
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// Cleanup all tmp directories used by luet
|
|
|
|
err := config.LuetCfg.GetSystem().CleanupTmpDir()
|
|
|
|
if err != nil {
|
2020-05-01 13:27:19 +00:00
|
|
|
Warning("failed on cleanup tmpdir:", err.Error())
|
2020-04-30 18:29:28 +00:00
|
|
|
}
|
2019-12-27 19:12:08 +00:00
|
|
|
},
|
2020-02-18 22:08:14 +00:00
|
|
|
SilenceErrors: true,
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(c *config.LuetConfig) error {
|
|
|
|
// If a config file is found, read it in.
|
2020-04-24 17:04:21 +00:00
|
|
|
c.Viper.ReadInConfig()
|
2019-12-27 19:12:08 +00:00
|
|
|
|
|
|
|
err := c.Viper.Unmarshal(&config.LuetCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:45:42 +00:00
|
|
|
noSpinner := c.Viper.GetBool("no_spinner")
|
|
|
|
|
2020-05-09 08:08:21 +00:00
|
|
|
InitAurora()
|
2020-06-27 14:45:42 +00:00
|
|
|
if !noSpinner {
|
|
|
|
NewSpinner()
|
|
|
|
}
|
2019-12-29 15:16:12 +00:00
|
|
|
|
2020-05-09 08:08:21 +00:00
|
|
|
Debug("Using config file:", c.Viper.ConfigFileUsed())
|
|
|
|
|
2020-05-07 06:14:37 +00:00
|
|
|
if c.GetLogging().EnableLogFile && c.GetLogging().Path != "" {
|
2020-01-03 14:22:55 +00:00
|
|
|
// Init zap logger
|
|
|
|
err = ZapLogger()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2020-03-22 14:19:08 +00:00
|
|
|
|
2019-11-26 17:05:13 +00:00
|
|
|
if os.Getenv("LUET_NOLOCK") != "true" {
|
2020-03-29 06:48:51 +00:00
|
|
|
if len(os.Args) > 1 {
|
|
|
|
for _, lockedCmd := range LockedCommands {
|
|
|
|
if os.Args[1] == lockedCmd {
|
|
|
|
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()
|
|
|
|
break
|
2020-03-22 14:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 17:05:13 +00:00
|
|
|
}
|
2019-11-25 19:28:08 +00:00
|
|
|
}
|
2020-02-18 22:08:14 +00:00
|
|
|
|
2019-10-29 16:36:48 +00:00
|
|
|
if err := RootCmd.Execute(); err != nil {
|
2020-02-18 22:08:14 +00:00
|
|
|
fmt.Println(err)
|
2019-10-29 16:36:48 +00:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
2019-12-29 15:33:10 +00:00
|
|
|
pflags := RootCmd.PersistentFlags()
|
|
|
|
pflags.StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luet.yaml)")
|
2020-03-06 15:53:36 +00:00
|
|
|
pflags.BoolP("debug", "d", false, "verbose output")
|
2020-01-03 14:41:45 +00:00
|
|
|
pflags.Bool("fatal", false, "Enables Warnings to exit")
|
2020-05-07 06:14:37 +00:00
|
|
|
pflags.Bool("enable-logfile", false, "Enable log to file")
|
2020-06-27 14:45:42 +00:00
|
|
|
pflags.Bool("no-spinner", false, "Disable spinner.")
|
2020-05-09 08:08:21 +00:00
|
|
|
pflags.Bool("color", config.LuetCfg.GetLogging().Color, "Enable/Disable color.")
|
|
|
|
pflags.Bool("emoji", config.LuetCfg.GetLogging().EnableEmoji, "Enable/Disable emoji.")
|
2020-10-02 20:25:21 +00:00
|
|
|
pflags.Bool("skip-config-protect", config.LuetCfg.ConfigProtectSkip,
|
|
|
|
"Disable config protect analysis.")
|
2020-05-07 06:14:37 +00:00
|
|
|
pflags.StringP("logfile", "l", config.LuetCfg.GetLogging().Path,
|
|
|
|
"Logfile path. Empty value disable log to file.")
|
2020-11-13 17:25:44 +00:00
|
|
|
pflags.StringSlice("plugin", []string{}, "A list of runtime plugins to load")
|
2020-02-01 16:58:23 +00:00
|
|
|
|
2020-05-30 14:51:10 +00:00
|
|
|
// os/user doesn't work in from scratch environments.
|
|
|
|
// Check if i can retrieve user informations.
|
|
|
|
_, err := user.Current()
|
2020-02-01 16:58:23 +00:00
|
|
|
if err != nil {
|
2020-04-11 18:05:56 +00:00
|
|
|
Warning("failed to retrieve user identity:", err.Error())
|
2020-02-01 16:58:23 +00:00
|
|
|
}
|
2020-05-30 14:51:10 +00:00
|
|
|
pflags.Bool("same-owner", config.LuetCfg.GetGeneral().SameOwner, "Maintain same owner on uncompress.")
|
2019-12-29 15:33:10 +00:00
|
|
|
pflags.Int("concurrency", runtime.NumCPU(), "Concurrency")
|
|
|
|
|
2020-05-09 08:08:21 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("logging.color", pflags.Lookup("color"))
|
|
|
|
config.LuetCfg.Viper.BindPFlag("logging.enable_emoji", pflags.Lookup("emoji"))
|
2020-05-07 06:14:37 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("logging.enable_logfile", pflags.Lookup("enable-logfile"))
|
|
|
|
config.LuetCfg.Viper.BindPFlag("logging.path", pflags.Lookup("logfile"))
|
|
|
|
|
2019-12-29 15:33:10 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("general.concurrency", pflags.Lookup("concurrency"))
|
2020-05-07 06:14:37 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("general.debug", pflags.Lookup("debug"))
|
2020-01-03 14:41:45 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("general.fatal_warnings", pflags.Lookup("fatal"))
|
2020-05-07 06:14:37 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("general.same_owner", pflags.Lookup("same-owner"))
|
2020-11-13 17:25:44 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("plugin", pflags.Lookup("plugin"))
|
|
|
|
|
2020-06-27 14:45:42 +00:00
|
|
|
// Currently I maintain this only from cli.
|
|
|
|
config.LuetCfg.Viper.BindPFlag("no_spinner", pflags.Lookup("no-spinner"))
|
2020-10-02 20:25:21 +00:00
|
|
|
config.LuetCfg.Viper.BindPFlag("config_protect_skip", pflags.Lookup("skip-config-protect"))
|
2020-06-12 15:54:57 +00:00
|
|
|
|
|
|
|
// Extensions must be binary with the "luet-" prefix to be able to be shown in the help.
|
|
|
|
// we also accept extensions in the relative path where luet is being started, "extensions/"
|
|
|
|
exts := extensions.Discover("luet", "extensions")
|
|
|
|
for _, ex := range exts {
|
|
|
|
cobraCmd := ex.CobraCommand()
|
|
|
|
RootCmd.AddCommand(cobraCmd)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
2020-05-30 08:08:41 +00:00
|
|
|
// Luet support these priorities on read configuration file:
|
|
|
|
// - command line option (if available)
|
|
|
|
// - $PWD/.luet.yaml
|
|
|
|
// - $HOME/.luet.yaml
|
|
|
|
// - /etc/luet/luet.yaml
|
|
|
|
//
|
|
|
|
// Note: currently a single viper instance support only one config name.
|
2019-11-25 19:28:08 +00:00
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
viper.SetEnvPrefix(LuetEnvPrefix)
|
2019-11-24 17:28:39 +00:00
|
|
|
viper.SetConfigType("yaml")
|
2020-05-30 08:08:41 +00:00
|
|
|
|
|
|
|
if cfgFile != "" { // enable ability to specify config file via flag
|
2019-10-29 16:36:48 +00:00
|
|
|
viper.SetConfigFile(cfgFile)
|
2019-12-27 19:12:08 +00:00
|
|
|
} else {
|
2020-05-30 08:08:41 +00:00
|
|
|
// Retrieve pwd directory
|
|
|
|
pwdDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
homeDir := helpers.GetHomeDir()
|
|
|
|
|
|
|
|
if helpers.Exists(filepath.Join(pwdDir, ".luet.yaml")) || (homeDir != "" && helpers.Exists(filepath.Join(homeDir, ".luet.yaml"))) {
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
if homeDir != "" {
|
|
|
|
viper.AddConfigPath(homeDir)
|
|
|
|
}
|
|
|
|
viper.SetConfigName(".luet")
|
|
|
|
} else {
|
|
|
|
viper.SetConfigName("luet")
|
|
|
|
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
|
|
|
}
|