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 = REQUEST_TTY_YES } if fc.IsSet("tt") && fc.Bool("tt") { sshOptions.TerminalRequest = REQUEST_TTY_FORCE } if fc.Bool("T") { sshOptions.TerminalRequest = REQUEST_TTY_NO } return sshOptions, nil }
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 { 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 }
func (cmd *Curl) Execute(c flags.FlagContext) { path := c.Args()[0] method := c.String("X") headers := c.StringSlice("H") var body string if c.IsSet("d") { jsonBytes, err := util.GetContentsFromFlagValue(c.String("d")) if err != nil { cmd.ui.Failed(err.Error()) } body = string(jsonBytes) } verbose := c.Bool("v") reqHeader := strings.Join(headers, "\n") if verbose { trace.EnableTrace() } responseHeader, responseBody, apiErr := cmd.curlRepo.Request(method, path, reqHeader, body) if apiErr != nil { cmd.ui.Failed(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()})) } if verbose { return } if c.Bool("i") { cmd.ui.Say(responseHeader) } if c.String("output") != "" { err := cmd.writeToFile(responseBody, c.String("output")) if err != nil { cmd.ui.Failed(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 }
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))
Ω(fc.Float64("i")).To(Equal(float64(0))) fc.NewFloat64FlagWithDefault("i", "i2", "setting new flag", 5.5) fc.Parse() Ω(fc.IsSet("i")).To(BeTrue()) Ω(fc.IsSet("i2")).To(BeTrue()) Ω(fc.Float64("i")).To(Equal(5.5)) Ω(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") Ω(fc.IsSet("s")).To(BeFalse()) Ω(fc.StringSlice("s")).To(Equal([]string{})) fc.NewStringSliceFlag("s", "s2", "setting new StringSlice flag") fc.Parse("-s", "5", "-s", "6") Ω(fc.IsSet("s")).To(BeTrue()) Ω(fc.IsSet("s2")).To(BeTrue()) Ω(fc.StringSlice("s")).To(Equal([]string{"5", "6"})) Ω(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() Ω(fc.IsSet("s")).To(BeFalse()) Ω(fc.StringSlice("s")).To(Equal([]string{}))