diff --git a/cmd/root.go b/cmd/root.go index 0ec22ec9..e7ca1606 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,7 @@ package cmd import ( + "fmt" "os" "os/user" "path/filepath" @@ -24,6 +25,7 @@ import ( "github.com/marcsauter/single" config "github.com/mudler/luet/pkg/config" + helpers "github.com/mudler/luet/pkg/helpers" . "github.com/mudler/luet/pkg/logger" repo "github.com/mudler/luet/pkg/repository" @@ -51,12 +53,13 @@ var RootCmd = &cobra.Command{ Fatal("failed to load configuration:", err.Error()) } }, + SilenceErrors: true, } func LoadConfig(c *config.LuetConfig) error { // If a config file is found, read it in. if err := c.Viper.ReadInConfig(); err != nil { - Warning(err) + Debug(err) } err := c.Viper.Unmarshal(&config.LuetCfg) @@ -99,8 +102,18 @@ func Execute() { } defer s.TryUnlock() } + if err := RootCmd.Execute(); err != nil { - Error(err) + if len(os.Args) > 0 { + for _, c := range RootCmd.Commands() { + if c.Name() == os.Args[1] { + os.Exit(-1) // Something failed + } + } + // Try to load a bin from path. + helpers.Exec("luet-"+os.Args[1], os.Args[1:], os.Environ()) + } + fmt.Println(err) os.Exit(-1) } } diff --git a/pkg/helpers/sys.go b/pkg/helpers/sys.go new file mode 100644 index 00000000..d46a73c7 --- /dev/null +++ b/pkg/helpers/sys.go @@ -0,0 +1,32 @@ +// Copyright © 2020 Ettore Di Giacinto +// +// 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 . + +package helpers + +import ( + "os/exec" + "syscall" + + "github.com/pkg/errors" +) + +// This allows a multi-platform switch in the future +func Exec(cmd string, args []string, env []string) error { + path, err := exec.LookPath(cmd) + if err != nil { + return errors.Wrap(err, "Could not find binary in path: "+cmd) + } + return syscall.Exec(path, args, env) +}