示例#1
0
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
}
示例#2
0
文件: curl.go 项目: palakmathur/cli
func (cmd *Curl) Run(c *cli.Context) {
	path := c.Args()[0]
	method := c.String("X")
	headers := c.StringSlice("H")
	body := c.String("d")
	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("Error creating request:\n%s", apiErr.Error())
		return
	}

	if verbose {
		return
	}

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

	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
}
示例#3
0
		rpcService, _ := rpc.NewRpcService(nil, nil, nil, nil, api.RepositoryLocator{}, nil)
		cmdFactory := command_factory.NewFactory(ui, config, manifestRepo, repoLocator, pluginConfig, rpcService)
		cmdRunner = &FakeRunner{cmdFactory: cmdFactory}
		app = NewApp(cmdRunner, cmdFactory.CommandMetadatas()...)
	})

	Describe("trace file integration", func() {
		var (
			output *bytes.Buffer
		)

		BeforeEach(func() {
			output = bytes.NewBuffer(make([]byte, 1024))
			trace.SetStdout(output)
			trace.EnableTrace()
		})

		It("prints its version number to the trace output when constructed", func() {
			Expect(strings.Split(output.String(), "\n")).To(ContainSubstrings(
				[]string{"VERSION:"},
				[]string{cf.Version},
			))
		})
	})

	Context("when given a command name to run", func() {
		It("runs the command with that name", func() {
			for _, cmdName := range expectedCommandNames {
				app.Run([]string{"", cmdName})
				Expect(cmdRunner.cmdName).To(Equal(cmdName))