示例#1
0
文件: axiom.go 项目: postfix/axiom
func (w *crashHandler) Wrap(cmd cli.Command) cli.Command {
	oldAction := cmd.Action
	cmd.Action = func(c *cli.Context) {
		oldAction(c)
	}
	return cmd
}
示例#2
0
func addDriverFlagsToCommand(cliFlags []cli.Flag, cmd *cli.Command) *cli.Command {
	cmd.Flags = append(SharedCreateFlags, cliFlags...)
	cmd.SkipFlagParsing = false
	cmd.Action = runCommand(cmdCreateInner)
	sort.Sort(ByFlagName(cmd.Flags))

	return cmd
}
示例#3
0
func wrap_cmd(cfg *watch.Config, c cli.Command) cli.Command {
	a := c.Action.(func(*cli.Context))
	c.Action = func(c *cli.Context) {
		setup_action(cfg, c)
		a(c)
	}
	return c
}
示例#4
0
文件: ahoy.go 项目: dkinzer/ahoy
func getCommands(config Config) []cli.Command {
	exportCmds := []cli.Command{}

	var keys []string
	for k := range config.Commands {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, name := range keys {
		cmd := config.Commands[name]
		cmdName := name
		subCommands := []cli.Command{}

		// Handle the import of subcommands.
		if cmd.Import != "" {
			// If the first character isn't "/" or "~" we assume a relative path.
			subSource := cmd.Import
			if cmd.Import[0] != "/"[0] || cmd.Import[0] != "~"[0] {
				subSource = filepath.Join(sourcedir, cmd.Import)
			}
			logger("info", "Importing commands into '"+name+"' command from "+subSource)
			subConfig, _ := getConfig(subSource)
			subCommands = getCommands(subConfig)
		}

		newCmd := cli.Command{
			Name:            name,
			SkipFlagParsing: true,
			HideHelp:        cmd.Hide,
		}

		if cmd.Usage != "" {
			newCmd.Usage = cmd.Usage
		}

		if cmd.Cmd != "" {
			newCmd.Action = func(c *cli.Context) {
				args = c.Args()
				runCommand(cmdName, cmd.Cmd)
			}
		}

		if subCommands != nil {
			newCmd.Subcommands = subCommands
		}

		//log.Println("found command: ", name, " > ", cmd.Cmd )
		exportCmds = append(exportCmds, newCmd)
	}

	return exportCmds
}
示例#5
0
文件: selinux.go 项目: xingdl2007/os
func selinuxCommand() cli.Command {
	app := cli.Command{}
	app.Name = "selinux"
	app.Usage = "Launch SELinux tools container."
	app.Action = func(c *cli.Context) error {
		argv := []string{"system-docker", "run", "-it", "--privileged", "--rm",
			"--net", "host", "--pid", "host", "--ipc", "host",
			"-v", "/usr/bin/docker:/usr/bin/docker.dist:ro",
			"-v", "/usr/bin/ros:/usr/bin/dockerlaunch:ro",
			"-v", "/usr/bin/ros:/usr/bin/user-docker:ro",
			"-v", "/usr/bin/ros:/usr/bin/system-docker:ro",
			"-v", "/usr/bin/ros:/sbin/poweroff:ro",
			"-v", "/usr/bin/ros:/sbin/reboot:ro",
			"-v", "/usr/bin/ros:/sbin/halt:ro",
			"-v", "/usr/bin/ros:/sbin/shutdown:ro",
			"-v", "/usr/bin/ros:/usr/bin/respawn:ro",
			"-v", "/usr/bin/ros:/usr/bin/ros:ro",
			"-v", "/usr/bin/ros:/usr/bin/cloud-init:ro",
			"-v", "/usr/bin/ros:/usr/sbin/netconf:ro",
			"-v", "/usr/bin/ros:/usr/sbin/wait-for-network:ro",
			"-v", "/usr/bin/ros:/usr/sbin/wait-for-docker:ro",
			"-v", "/var/lib/docker:/var/lib/docker",
			"-v", "/var/lib/rkt:/var/lib/rkt",
			"-v", "/dev:/host/dev",
			"-v", "/etc/docker:/etc/docker",
			"-v", "/etc/hosts:/etc/hosts",
			"-v", "/etc/resolv.conf:/etc/resolv.conf",
			"-v", "/etc/rkt:/etc/rkt",
			"-v", "/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt.rancher",
			"-v", "/lib/firmware:/lib/firmware",
			"-v", "/lib/modules:/lib/modules",
			"-v", "/run:/run",
			"-v", "/usr/share/ros:/usr/share/ros",
			"-v", "/var/lib/rancher/conf:/var/lib/rancher/conf",
			"-v", "/var/lib/rancher:/var/lib/rancher",
			"-v", "/var/log:/var/log",
			"-v", "/var/run:/var/run",
			"-v", "/home:/home",
			"-v", "/opt:/opt",
			"-v", "/etc/selinux:/etc/selinux",
			"-v", "/var/lib/selinux:/var/lib/selinux",
			"-v", "/usr/share/selinux:/usr/share/selinux",
			fmt.Sprintf("rancher/os-selinuxtools:%s", config.VERSION+config.SUFFIX), "bash"}
		syscall.Exec("/bin/system-docker", argv, []string{})
		return nil
	}

	return app
}
示例#6
0
文件: axiom.go 项目: postfix/axiom
func (w *YAMLConfigLoader) Wrap(cmd cli.Command) cli.Command {
	cmd.Flags = append(cmd.Flags, []cli.Flag{
		cli.StringFlag{"config", w.DefaultPath, "path to YAML config file", ""},
	}...)
	oldAction := cmd.Action
	cmd.Action = func(c *cli.Context) {
		configFile := c.String("config")
		if configFile != "" {
			err := w.LoadFromFile(configFile)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}
		oldAction(c)
	}
	return cmd
}
示例#7
0
文件: axiom.go 项目: postfix/axiom
func (w *Logged) Wrap(cmd cli.Command) cli.Command {
	cmd.Flags = append(cmd.Flags, []cli.Flag{
		cli.StringFlag{"log", w.DefaultTarget, "path to log file, 'stdout', 'stderr' or 'false'", ""},
		cli.StringFlag{"log-level", w.DefaultLevel, "logging level", ""},
		cli.StringFlag{"log-format", w.DefaultFormat, "log record format: 'term', 'logfmt', 'json'", ""},
	}...)
	oldAction := cmd.Action
	cmd.Action = func(c *cli.Context) {
		handler, err := w.HandlerFor(c.String("log"), c.String("log-level"), c.String("log-format"))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		for _, l := range w.Loggers {
			l.SetHandler(handler)
		}
		oldAction(c)
	}
	return cmd
}
示例#8
0
文件: ahoy.go 项目: DevinciHQ/ahoy
func getCommands(config Config) []cli.Command {
	exportCmds := []cli.Command{}

	var keys []string
	for k := range config.Commands {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, name := range keys {
		cmd := config.Commands[name]
		cmdName := name

		newCmd := cli.Command{
			Name:            name,
			SkipFlagParsing: true,
			HideHelp:        cmd.Hide,
		}

		if cmd.Usage != "" {
			newCmd.Usage = cmd.Usage
		}

		if cmd.Cmd != "" {
			newCmd.Action = func(c *cli.Context) {
				args = c.Args()
				runCommand(cmdName, cmd.Cmd)
			}
		}

		subCommands := getSubCommands(cmd.Imports)
		if subCommands != nil {
			newCmd.Subcommands = subCommands
		}

		//log.Println("found command: ", name, " > ", cmd.Cmd )
		exportCmds = append(exportCmds, newCmd)
	}

	return exportCmds
}
func testTileAction(actionName string) {
	var (
		controlErrorHandler = new(ErrorHandler)
	)

	Context(fmt.Sprintf("when called with the %s command name", actionName), func() {
		controlName := actionName
		controlUsage := "something about the usage here"

		It("Then it should return a cli.Command with required values", func() {
			cmd := CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
			Ω(cmd.Name).Should(Equal(controlName))
			Ω(cmd.Usage).Should(Equal(controlUsage))
			Ω(cmd.Action).ShouldNot(BeNil())
		})

		Describe(fmt.Sprintf("given a %s command with an Action value", actionName), func() {
			var (
				controlExit       = 0
				controlCliContext *cli.Context
				controlCmd        cli.Command
			)
			BeforeEach(func() {
				controlErrorHandler = new(ErrorHandler)
				controlErrorHandler.ExitCode = controlExit
			})

			Context("when the action is called with all proper flags on a registered tile", func() {
				var (
					controlTileName      = "fake-tile"
					controlTileGenerator *fake.TileGenerator
					controlTile          *fake.Tile
				)
				BeforeEach(func() {
					controlTile = new(fake.Tile)
					controlTileGenerator = new(fake.TileGenerator)
					controlTileGenerator.TileSpy = controlTile
					tileregistry.Register(controlTileName, controlTileGenerator)
					set := flag.NewFlagSet("", 0)
					set.String("tile", controlTileName, "")
					set.String("opsmanagerhost", "*****", "")
					set.String("adminuser", "*****", "")
					set.String("adminpass", "*****", "")
					set.String("opsmanageruser", "*****", "")
					set.String("opsmanagerpass", "*****", "")
					set.String("destination", "*****", "")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})
				It("then it should execute a call on the tiles Action func()", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).Should(Equal(controlExit))
					Ω(controlErrorHandler.Error).ShouldNot(HaveOccurred())
					switch controlName {
					case "backup":
						Ω(controlTile.BackupCallCount).ShouldNot(Equal(0))
					case "restore":
						Ω(controlTile.RestoreCallCount).ShouldNot(Equal(0))
					default:
						panic("this should never happen")
					}
				})
			})

			Context("when the action is called w/o a matching registered tile", func() {
				BeforeEach(func() {
					set := flag.NewFlagSet(controlName, 0)
					set.Bool("myflag", false, "doc")
					set.String("otherflag", "hello world", "doc")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})

				It("then it should set an error and failure exit code", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).ShouldNot(Equal(controlExit))
					Ω(controlErrorHandler.Error).Should(HaveOccurred())
					Ω(controlErrorHandler.Error).Should(Equal(ErrInvalidTileSelection))
				})
			})

			Context("when the action is called w/o proper flags but with a registered tile", func() {
				var controlTileName = "fake-tile"
				BeforeEach(func() {
					tileregistry.Register(controlTileName, new(fake.TileGenerator))
					set := flag.NewFlagSet("", 0)
					set.String("tile", controlTileName, "doc")
					set.Bool("myflag", false, "doc")
					set.String("otherflag", "hello world", "doc")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})
				It("then it should set an error and failure exit code", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).ShouldNot(Equal(controlExit))
					Ω(controlErrorHandler.Error).Should(HaveOccurred())
					Ω(controlErrorHandler.Error).Should(Equal(ErrInvalidFlagArgs))
				})
			})
		})
	})
}