aboutsummaryrefslogtreecommitdiff
path: root/cmd/podmanV2/root.go
blob: cb4cb4e00e957526ac406575a3a7537cd94dff2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main

import (
	"fmt"
	"log/syslog"
	"os"
	"path"

	"github.com/containers/libpod/cmd/podmanV2/registry"
	"github.com/containers/libpod/libpod/define"
	"github.com/containers/libpod/pkg/domain/entities"
	"github.com/containers/libpod/version"
	"github.com/sirupsen/logrus"
	logrusSyslog "github.com/sirupsen/logrus/hooks/syslog"
	"github.com/spf13/cobra"
)

var (
	rootCmd = &cobra.Command{
		Use:               path.Base(os.Args[0]),
		Long:              "Manage pods, containers and images",
		SilenceUsage:      true,
		SilenceErrors:     true,
		TraverseChildren:  true,
		PersistentPreRunE: preRunE,
		RunE:              registry.SubCommandExists,
		Version:           version.Version,
	}

	logLevels = entities.NewStringSet("debug", "info", "warn", "error", "fatal", "panic")
	logLevel  = "error"
	useSyslog bool
)

func init() {
	// Override default --help information of `--version` global flag}
	var dummyVersion bool
	// TODO had to disable shorthand -v for version due to -v rm with volume
	rootCmd.PersistentFlags().BoolVar(&dummyVersion, "version", false, "Version of Podman")
	rootCmd.PersistentFlags().StringVarP(&registry.EngineOptions.Uri, "remote", "r", "", "URL to access Podman service")
	rootCmd.PersistentFlags().StringSliceVar(&registry.EngineOptions.Identities, "identity", []string{}, "path to SSH identity file")
	rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "error", fmt.Sprintf("Log messages above specified level (%s)", logLevels.String()))
	rootCmd.PersistentFlags().BoolVar(&useSyslog, "syslog", false, "Output logging information to syslog as well as the console (default false)")

	cobra.OnInitialize(
		logging,
		syslogHook,
	)
}

func preRunE(cmd *cobra.Command, args []string) error {
	cmd.SetHelpTemplate(registry.HelpTemplate())
	cmd.SetUsageTemplate(registry.UsageTemplate())
	return nil
}

func logging() {
	if !logLevels.Contains(logLevel) {
		fmt.Fprintf(os.Stderr, "Log Level \"%s\" is not supported, choose from: %s\n", logLevel, logLevels.String())
		os.Exit(1)
	}

	level, err := logrus.ParseLevel(logLevel)
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
	logrus.SetLevel(level)

	if logrus.IsLevelEnabled(logrus.InfoLevel) {
		logrus.Infof("%s filtering at log level %s", os.Args[0], logrus.GetLevel())
	}
}

func syslogHook() {
	if useSyslog {
		hook, err := logrusSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
		if err != nil {
			logrus.WithError(err).Error("Failed to initialize syslog hook")
		}
		if err == nil {
			logrus.AddHook(hook)
		}
	}
}

func Execute() {
	o := registry.NewOptions(rootCmd.Context(), &registry.EngineOptions)
	if err := rootCmd.ExecuteContext(o); err != nil {
		fmt.Fprintln(os.Stderr, "Error:", err.Error())
	} else if registry.GetExitCode() == define.ExecErrorCodeGeneric {
		// The exitCode modified from define.ExecErrorCodeGeneric,
		// indicates an application
		// running inside of a container failed, as opposed to the
		// podman command failed.  Must exit with that exit code
		// otherwise command exited correctly.
		registry.SetExitCode(0)
	}
	os.Exit(registry.GetExitCode())
}