package cmdlogger import ( "fmt" "os" "os/exec" "path/filepath" "github.com/spf13/cobra" plog "novit.nc/direktil/pkg/log" ) func Command() (c *cobra.Command) { c = &cobra.Command{ Use: "logger [--] [args]", Short: "run a command through the dkl logger", Args: cobra.MinimumNArgs(1), Run: run, } c.Flags().StringP("name", "n", "", "log name") return } func run(c *cobra.Command, args []string) { logName, _ := c.Flags().GetString("name") err := Run(logName, args[0], args[1:]...) if err == nil { os.Exit(0) } else if exitErr, ok := err.(*exec.ExitError); ok { os.Exit(exitErr.ExitCode()) } else { fmt.Fprintln(os.Stderr, "failed to run command: "+err.Error()) os.Exit(1) } } func Run(logName, command string, args ...string) (err error) { if logName == "" { logName = filepath.Base(command) } logger := plog.Get(logName) plog.EnableFiles() cmd := exec.Command(command, args...) cmd.Stdout = logger cmd.Stderr = logger return cmd.Run() }