<commit_before>package objectcommands

import (
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/codegangsta/cli"
	"github.com/jrperritt/rack/handler"
	"github.com/jrperritt/rack/util"
	osObjects "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
	"github.com/rackspace/gophercloud/rackspace/objectstorage/v1/objects"
)

var upload = cli.Command{
	Name:        "upload",
	Usage:       util.Usage(commandPrefix, "upload", "--container <containerName> --name <objectName>"),
	Description: "Uploads an object",
	Action:      actionUpload,
	Flags:       util.CommandFlags(flagsUpload, keysUpload),
	BashComplete: func(c *cli.Context) {
		util.CompleteFlags(util.CommandFlags(flagsUpload, keysUpload))
	},
}

func flagsUpload() []cli.Flag {
	return []cli.Flag{
		cli.StringFlag{
			Name:  "container",
			Usage: "[required] The name of the container to upload the object upload",
		},
		cli.StringFlag{
			Name:  "name",
			Usage: "[required] The name the object should have in the Cloud Files container",
		},
		cli.StringFlag{
			Name:  "content",
			Usage: "[optional; required if `file` or `stdin` isn't provided] The string contents to upload",
		},
		cli.StringFlag{
			Name:  "file",
			Usage: "[optional; required if `content` or `stdin` isn't provided] The file name containing the contents to upload",
		},
		cli.StringFlag{
			Name:  "stdin",
			Usage: "[optional; required if `file` or `content` isn't provided] The field being piped to STDIN, if any. Valid values are: content",
		},
	}
}

var keysUpload = []string{}

type paramsUpload struct {
	container string
	object    string
	content   string
	fileName  string
	stream    bool
	opts      osObjects.CreateOpts
}

type commandUpload handler.Command

func actionUpload(c *cli.Context) {
	command := &commandUpload{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	handler.Handle(command)
}

func (command *commandUpload) Context() *handler.Context {
	return command.Ctx
}

func (command *commandUpload) Keys() []string {
	return keysUpload
}

func (command *commandUpload) ServiceClientType() string {
	return serviceClientType
}

func (command *commandUpload) HandleFlags(resource *handler.Resource) error {
	err := command.Ctx.CheckFlagsSet([]string{"container", "name"})
	if err != nil {
		return err
	}

	container := command.Ctx.CLIContext.String("container")
	object := command.Ctx.CLIContext.String("name")
	resource.Params = &paramsUpload{
		container: container,
		object:    object,
	}
	return nil
}

func (command *commandUpload) HandlePipe(resource *handler.Resource, item string) error {
	return nil
}

func (command *commandUpload) HandleSingle(resource *handler.Resource) error {
	if command.Ctx.CLIContext.IsSet("file") {
		resource.Params.(*paramsUpload).fileName = command.Ctx.CLIContext.String("file")
	} else if command.Ctx.CLIContext.IsSet("content") {
		resource.Params.(*paramsUpload).content = command.Ctx.CLIContext.String("content")
	} else {<commit_msg>add flags for 'object upload'<commit_after>package objectcommands

import (
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/codegangsta/cli"
	"github.com/jrperritt/rack/handler"
	"github.com/jrperritt/rack/util"
	osObjects "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
	"github.com/rackspace/gophercloud/rackspace/objectstorage/v1/objects"
)

var upload = cli.Command{
	Name:        "upload",
	Usage:       util.Usage(commandPrefix, "upload", "--container <containerName> --name <objectName>"),
	Description: "Uploads an object",
	Action:      actionUpload,
	Flags:       util.CommandFlags(flagsUpload, keysUpload),
	BashComplete: func(c *cli.Context) {
		util.CompleteFlags(util.CommandFlags(flagsUpload, keysUpload))
	},
}

func flagsUpload() []cli.Flag {
	return []cli.Flag{
		cli.StringFlag{
			Name:  "container",
			Usage: "[required] The name of the container to upload the object upload",
		},
		cli.StringFlag{
			Name:  "name",
			Usage: "[required] The name the object should have in the Cloud Files container",
		},
		cli.StringFlag{
			Name:  "content",
			Usage: "[optional; required if `file` or `stdin` isn't provided] The string contents to upload",
		},
		cli.StringFlag{
			Name:  "file",
			Usage: "[optional; required if `content` or `stdin` isn't provided] The file name containing the contents to upload",
		},
		cli.StringFlag{
			Name:  "stdin",
			Usage: "[optional; required if `file` or `content` isn't provided] The field being piped to STDIN, if any. Valid values are: content",
		},
		cli.StringFlag{
			Name:  "content-type",
			Usage: "[optional] The Content-Type header.",
		},
		cli.IntFlag{
			Name:  "content-length",
			Usage: "[optional] The Content-Length header.",
		},
		cli.StringFlag{
			Name:  "content-encoding",
			Usage: "[optional] The Content-Encoding header.",
		},
	}
}

var keysUpload = []string{}

type paramsUpload struct {
	container string
	object    string
	content   string
	fileName  string
	stream    bool
	opts      osObjects.CreateOpts
}

type commandUpload handler.Command

func actionUpload(c *cli.Context) {
	command := &commandUpload{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	handler.Handle(command)
}

func (command *commandUpload) Context() *handler.Context {
	return command.Ctx
}

func (command *commandUpload) Keys() []string {
	return keysUpload
}

func (command *commandUpload) ServiceClientType() string {
	return serviceClientType
}

func (command *commandUpload) HandleFlags(resource *handler.Resource) error {
	err := command.Ctx.CheckFlagsSet([]string{"container", "name"})
	if err != nil {
		return err
	}

	c := command.Ctx.CLIContext

	opts := osObjects.CreateOpts{
		ContentEncoding: c.String("content-encoding"),
		ContentLength:   int64(c.Int("content-length")),
		ContentType:     c.String("content-type"),
	}

	resource.Params = &paramsUpload{
		container: c.String("container"),
		object:    c.String("name"),
		opts:      opts,
	}

	return nil
}

func (command *commandUpload) HandlePipe(resource *handler.Resource, item string) error {
	return nil
}

func (command *commandUpload) HandleSingle(resource *handler.Resource) error {
	if command.Ctx.CLIContext.IsSet("file") {
		resource.Params.(*paramsUpload).fileName = command.Ctx.CLIContext.String("file")
	} else if command.Ctx.CLIContext.IsSet("content") {
		resource.Params.(*paramsUpload).content = command.Ctx.CLIContext.String("content")
	} else {
{'fraction_non_alphanumeric': 0.13253955820147267, 'fraction_numerical': 0.0009399968666771111, 'mean_word_length': 4.355704697986577, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5004182', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2059, 'n_words': 516}