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"
|
|
|
|
|
2019-11-25 19:28:08 +00:00
|
|
|
"github.com/marcsauter/single"
|
2021-10-19 20:26:23 +00:00
|
|
|
"github.com/mudler/luet/cmd/util"
|
2020-11-13 17:25:44 +00:00
|
|
|
bus "github.com/mudler/luet/pkg/bus"
|
|
|
|
|
2019-12-27 19:12:08 +00:00
|
|
|
config "github.com/mudler/luet/pkg/config"
|
2019-11-02 09:26:28 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
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 (
|
2021-10-10 19:27:35 +00:00
|
|
|
LuetCLIVersion = "0.18.1"
|
2019-12-27 19:12:08 +00:00
|
|
|
LuetEnvPrefix = "LUET"
|
2021-07-09 09:22:13 +00:00
|
|
|
license = `
|
|
|
|
Luet Copyright (C) 2019-2021 Ettore Di Giacinto
|
|
|
|
This program comes with ABSOLUTELY NO WARRANTY.
|
|
|
|
This is free software, and you are welcome to redistribute it
|
|
|
|
under certain conditions.
|
|
|
|
`
|
2019-12-27 19:12:08 +00:00
|
|
|
)
|
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
|
|
|
|
)
|
|
|
|
|
2021-07-09 09:22:13 +00:00
|
|
|
func version() string {
|
|
|
|
return fmt.Sprintf("%s-g%s %s", LuetCLIVersion, BuildCommit, BuildTime)
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:26:23 +00:00
|
|
|
var bannerCommands = []string{"install", "build", "uninstall", "upgrade"}
|
2021-07-11 09:00:57 +00:00
|
|
|
|
|
|
|
func displayVersionBanner() {
|
2021-10-19 20:26:23 +00:00
|
|
|
display := false
|
2021-07-11 09:00:57 +00:00
|
|
|
if len(os.Args) > 1 {
|
2021-10-19 20:26:23 +00:00
|
|
|
for _, c := range bannerCommands {
|
2021-07-11 09:00:57 +00:00
|
|
|
if os.Args[1] == c {
|
2021-10-19 20:26:23 +00:00
|
|
|
display = true
|
2021-07-11 09:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if display {
|
2021-10-19 20:26:23 +00:00
|
|
|
util.IntroScreen()
|
2021-07-11 09:00:57 +00:00
|
|
|
Info("Luet version", version())
|
|
|
|
Info(license)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLock() {
|
|
|
|
if os.Getenv("LUET_NOLOCK") != "true" {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:36:48 +00:00
|
|
|
// RootCmd represents the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
2020-11-22 19:16:04 +00:00
|
|
|
Use: "luet",
|
2020-12-02 17:24:32 +00:00
|
|
|
Short: "Container based package manager",
|
2020-11-22 19:16:04 +00:00
|
|
|
Long: `Luet is a single-binary package manager based on containers to build packages.
|
|
|
|
|
|
|
|
To install a package:
|
|
|
|
|
2020-12-02 17:24:32 +00:00
|
|
|
$ luet install package
|
2020-11-22 19:16:04 +00:00
|
|
|
|
|
|
|
To search for a package in the repositories:
|
|
|
|
|
|
|
|
$ luet search package
|
|
|
|
|
|
|
|
To list all packages installed in the system:
|
|
|
|
|
2020-12-02 17:24:32 +00:00
|
|
|
$ luet search --installed .
|
2020-11-22 19:16:04 +00:00
|
|
|
|
|
|
|
To show hidden packages:
|
|
|
|
|
2020-12-02 17:24:32 +00:00
|
|
|
$ luet search --hidden package
|
2020-11-22 19:16:04 +00:00
|
|
|
|
|
|
|
To build a package, from a tree definition:
|
|
|
|
|
2020-12-02 17:24:32 +00:00
|
|
|
$ luet build --tree tree/path package
|
2020-11-22 19:16:04 +00:00
|
|
|
|
|
|
|
`,
|
2021-07-09 09:22:13 +00:00
|
|
|
Version: version(),
|
2019-12-27 19:12:08 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2021-07-11 09:00:57 +00:00
|
|
|
|
2021-10-19 20:26:23 +00:00
|
|
|
_, err := util.LoadConfig()
|
2019-12-27 19:12:08 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatal("failed to load configuration:", err.Error())
|
|
|
|
}
|
2021-10-19 20:26:23 +00:00
|
|
|
|
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")
|
|
|
|
|
2021-05-13 11:20:23 +00:00
|
|
|
bus.Manager.Initialize(plugin...)
|
2020-11-13 17:25:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-07-11 09:00:57 +00:00
|
|
|
handleLock()
|
|
|
|
displayVersionBanner()
|
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() {
|
2021-10-19 20:26:23 +00:00
|
|
|
util.InitViper(RootCmd)
|
2019-10-29 16:36:48 +00:00
|
|
|
}
|