aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/main.go
blob: 4f813165339df53ab02d34d6c174217b985c288b (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
package main

import (
	"fmt"
	"os"
	"strings"

	_ "github.com/containers/podman/v4/cmd/podman/completion"
	_ "github.com/containers/podman/v4/cmd/podman/containers"
	_ "github.com/containers/podman/v4/cmd/podman/generate"
	_ "github.com/containers/podman/v4/cmd/podman/healthcheck"
	_ "github.com/containers/podman/v4/cmd/podman/images"
	_ "github.com/containers/podman/v4/cmd/podman/machine"
	_ "github.com/containers/podman/v4/cmd/podman/manifest"
	_ "github.com/containers/podman/v4/cmd/podman/networks"
	_ "github.com/containers/podman/v4/cmd/podman/play"
	_ "github.com/containers/podman/v4/cmd/podman/pods"
	"github.com/containers/podman/v4/cmd/podman/registry"
	_ "github.com/containers/podman/v4/cmd/podman/secrets"
	_ "github.com/containers/podman/v4/cmd/podman/system"
	_ "github.com/containers/podman/v4/cmd/podman/system/connection"
	_ "github.com/containers/podman/v4/cmd/podman/volumes"
	"github.com/containers/podman/v4/pkg/rootless"
	"github.com/containers/podman/v4/pkg/terminal"
	"github.com/containers/storage/pkg/reexec"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

func main() {
	if reexec.Init() {
		// We were invoked with a different argv[0] indicating that we
		// had a specific job to do as a subprocess, and it's done.
		return
	}

	rootCmd = parseCommands()

	Execute()
	os.Exit(0)
}

func parseCommands() *cobra.Command {
	cfg := registry.PodmanConfig()
	for _, c := range registry.Commands {
		if supported, found := c.Command.Annotations[registry.EngineMode]; found {
			if !strings.Contains(cfg.EngineMode.String(), supported) {
				continue
			}
		}

		// Command cannot be run rootless
		_, found := c.Command.Annotations[registry.UnshareNSRequired]
		if found {
			if rootless.IsRootless() && os.Getuid() != 0 && c.Command.Name() != "scp" {
				c.Command.RunE = func(cmd *cobra.Command, args []string) error {
					return fmt.Errorf("cannot run command %q in rootless mode, must execute `podman unshare` first", cmd.CommandPath())
				}
			}
		} else {
			_, found = c.Command.Annotations[registry.ParentNSRequired]
			if rootless.IsRootless() && found && c.Command.Name() != "scp" {
				c.Command.RunE = func(cmd *cobra.Command, args []string) error {
					return fmt.Errorf("cannot run command %q in rootless mode", cmd.CommandPath())
				}
			}
		}

		parent := rootCmd
		if c.Parent != nil {
			parent = c.Parent
		}
		parent.AddCommand(c.Command)

		c.Command.SetFlagErrorFunc(flagErrorFuncfunc)

		// - templates need to be set here, as PersistentPreRunE() is
		// not called when --help is used.
		// - rootCmd uses cobra default template not ours
		c.Command.SetHelpTemplate(helpTemplate)
		c.Command.SetUsageTemplate(usageTemplate)
		c.Command.DisableFlagsInUseLine = true
	}
	if err := terminal.SetConsole(); err != nil {
		logrus.Error(err)
		os.Exit(1)
	}

	rootCmd.SetFlagErrorFunc(flagErrorFuncfunc)
	return rootCmd
}

func flagErrorFuncfunc(c *cobra.Command, e error) error {
	e = fmt.Errorf("%w\nSee '%s --help'", e, c.CommandPath())
	return e
}