luet/cmd/root.go

134 lines
3.6 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 (
"fmt"
2019-10-29 16:36:48 +00:00
"os"
"github.com/mudler/luet/cmd/util"
bus "github.com/mudler/luet/pkg/bus"
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 (
2021-10-23 10:21:59 +00:00
LuetCLIVersion = "0.19.2"
2019-12-27 19:12:08 +00:00
LuetEnvPrefix = "LUET"
)
var license = []string{
"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.",
}
// Build time and commit information.
//
// ⚠️ WARNING: should only be set by "-ldflags".
var (
BuildTime string
BuildCommit string
)
func version() string {
return fmt.Sprintf("%s-g%s %s", LuetCLIVersion, BuildCommit, BuildTime)
}
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",
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:
$ 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:
$ luet search --installed .
2020-11-22 19:16:04 +00:00
To show hidden packages:
$ luet search --hidden package
2020-11-22 19:16:04 +00:00
To build a package, from a tree definition:
$ luet build --tree tree/path package
2020-11-22 19:16:04 +00:00
`,
Version: version(),
2019-12-27 19:12:08 +00:00
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := util.InitContext(util.DefaultContext)
2019-12-27 19:12:08 +00:00
if err != nil {
util.DefaultContext.Error("failed to load configuration:", err.Error())
2019-12-27 19:12:08 +00:00
}
util.DisplayVersionBanner(util.DefaultContext, util.IntroScreen, version, license)
// Initialize tmpdir prefix. TODO: Move this with LoadConfig
// directly on sub command to ensure the creation only when it's
// needed.
err = util.DefaultContext.Config.GetSystem().InitTmpDir()
if err != nil {
util.DefaultContext.Fatal("failed on init tmp basedir:", err.Error())
}
viper.BindPFlag("plugin", cmd.Flags().Lookup("plugin"))
plugin := viper.GetStringSlice("plugin")
bus.Manager.Initialize(plugin...)
if len(bus.Manager.Plugins) != 0 {
util.DefaultContext.Info(":lollipop:Enabled plugins:")
for _, p := range bus.Manager.Plugins {
util.DefaultContext.Info("\t:arrow_right:", p.Name)
}
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// Cleanup all tmp directories used by luet
err := util.DefaultContext.Config.GetSystem().CleanupTmpDir()
if err != nil {
util.DefaultContext.Warning("failed on cleanup tmpdir:", err.Error())
}
2019-12-27 19:12:08 +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() {
util.HandleLock(util.DefaultContext)
2019-10-29 16:36:48 +00:00
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
2019-10-29 16:36:48 +00:00
os.Exit(-1)
}
}
func init() {
util.InitViper(util.DefaultContext, RootCmd)
2019-10-29 16:36:48 +00:00
}