예제 #1
0
func NewSSHOptions(fc flags.FlagContext) (*SSHOptions, error) {
	sshOptions := &SSHOptions{}

	sshOptions.AppName = fc.Args()[0]
	sshOptions.Index = uint(fc.Int("i"))
	sshOptions.SkipHostValidation = fc.Bool("k")
	sshOptions.SkipRemoteExecution = fc.Bool("N")
	sshOptions.Command = fc.StringSlice("c")

	if fc.IsSet("L") {
		for _, arg := range fc.StringSlice("L") {
			forwardSpec, err := sshOptions.parseLocalForwardingSpec(arg)
			if err != nil {
				return sshOptions, err
			}
			sshOptions.ForwardSpecs = append(sshOptions.ForwardSpecs, *forwardSpec)
		}
	}

	if fc.IsSet("t") && fc.Bool("t") {
		sshOptions.TerminalRequest = RequestTTYYes
	}

	if fc.IsSet("tt") && fc.Bool("tt") {
		sshOptions.TerminalRequest = RequestTTYForce
	}

	if fc.Bool("T") {
		sshOptions.TerminalRequest = RequestTTYNo
	}

	return sshOptions, nil
}
예제 #2
0
파일: curl.go 프로젝트: jasonkeene/cli
func (cmd *Curl) Execute(c flags.FlagContext) error {
	path := c.Args()[0]
	headers := c.StringSlice("H")

	var method string
	var body string

	if c.IsSet("d") {
		method = "POST"

		jsonBytes, err := util.GetContentsFromOptionalFlagValue(c.String("d"))
		if err != nil {
			return err
		}
		body = string(jsonBytes)
	}

	if c.IsSet("X") {
		method = c.String("X")
	}

	reqHeader := strings.Join(headers, "\n")

	responseHeader, responseBody, apiErr := cmd.curlRepo.Request(method, path, reqHeader, body)
	if apiErr != nil {
		return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
	}

	if trace.LoggingToStdout && !cmd.pluginCall {
		return nil
	}

	if c.Bool("i") {
		cmd.ui.Say(responseHeader)
	}

	if c.String("output") != "" {
		err := cmd.writeToFile(responseBody, c.String("output"))
		if err != nil {
			return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": err}))
		}
	} else {
		if strings.Contains(responseHeader, "application/json") {
			buffer := bytes.Buffer{}
			err := json.Indent(&buffer, []byte(responseBody), "", "   ")
			if err == nil {
				responseBody = buffer.String()
			}
		}

		cmd.ui.Say(responseBody)
	}
	return nil
}
예제 #3
0
			Expect(fc.Float64("i")).To(Equal(float64(0)))

			fc.NewFloat64FlagWithDefault("i", "i2", "setting new flag", 5.5)
			fc.Parse()
			Expect(fc.IsSet("i")).To(BeTrue())
			Expect(fc.IsSet("i2")).To(BeTrue())
			Expect(fc.Float64("i")).To(Equal(5.5))
			Expect(fc.Float64("i2")).To(Equal(5.5))
		})
	})

	Describe("NewStringSliceFlag()", func() {
		It("init the flag context with a new StringSlice flagset", func() {
			fc.Parse("-s", "5", "-s", "6")
			Expect(fc.IsSet("s")).To(BeFalse())
			Expect(fc.StringSlice("s")).To(Equal([]string{}))

			fc.NewStringSliceFlag("s", "s2", "setting new StringSlice flag")
			fc.Parse("-s", "5", "-s", "6")
			Expect(fc.IsSet("s")).To(BeTrue())
			Expect(fc.IsSet("s2")).To(BeTrue())
			Expect(fc.StringSlice("s")).To(Equal([]string{"5", "6"}))
			Expect(fc.StringSlice("s2")).To(Equal([]string{"5", "6"}))
		})
	})

	Describe("NewStringSliceFlagWithDefault()", func() {
		It("init the flag context with a new StringSlice flagset with default value", func() {
			fc.Parse()
			Expect(fc.IsSet("s")).To(BeFalse())
			Expect(fc.StringSlice("s")).To(Equal([]string{}))
예제 #4
0
				Expect(fCtx.Bool("non-exisit-flag")).To(Equal(false))
			})

			It("sets String(<flag>) to return provided value when a string flag is provided", func() {
				err := fCtx.Parse("--skip", "-name", "doe")
				Expect(err).NotTo(HaveOccurred())

				Expect(fCtx.String("name")).To(Equal("doe"))
				Ω(fCtx.Bool("skip")).To(Equal(true), "skip should be true")
			})

			It("sets StringSlice(<flag>) to return provided value when a stringSlice flag is provided", func() {
				err := fCtx.Parse("-slice", "value1", "-slice", "value2")
				Expect(err).NotTo(HaveOccurred())

				Ω(fCtx.StringSlice("slice")[0]).To(Equal("value1"), "slice[0] should be 'value1'")
				Ω(fCtx.StringSlice("slice")[1]).To(Equal("value2"), "slice[1] should be 'value2'")
			})

			It("errors when a non-boolean flag is provided without a value", func() {
				err := fCtx.Parse("-name")
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("No value provided for flag"))
				Expect(fCtx.String("name")).To(Equal(""))
			})

			It("sets Int(<flag>) to return provided value when a int flag is provided", func() {
				err := fCtx.Parse("--instance", "10")
				Expect(err).NotTo(HaveOccurred())

				Expect(fCtx.Int("instance")).To(Equal(10))