command

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2020 License: MIT Imports: 21 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CatCommand = &cli.Command{
	Name:               "cat",
	HelpName:           "cat",
	Usage:              "print remote object's contents to stdout",
	CustomHelpTemplate: catHelpTemplate,
	Before: func(c *cli.Context) error {
		if c.Args().Len() != 1 {
			return fmt.Errorf("expected only one argument")
		}

		src, err := url.New(c.Args().Get(0))
		if err != nil {
			return err
		}

		if !src.IsRemote() {
			return fmt.Errorf("source must be a remote object")
		}

		if src.IsBucket() || src.IsPrefix() {
			return fmt.Errorf("remote source must an object")
		}

		if src.HasGlob() {
			return fmt.Errorf("remote source %q can not contain glob characters", src)
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		src, err := url.New(c.Args().Get(0))
		if err != nil {
			return err
		}

		return Cat(c.Context, src)
	},
}
View Source
var CopyCommand = &cli.Command{
	Name:               "cp",
	HelpName:           "cp",
	Usage:              "copy objects",
	Flags:              copyCommandFlags,
	CustomHelpTemplate: copyHelpTemplate,
	Before: func(c *cli.Context) error {
		return Validate(c)
	},
	Action: func(c *cli.Context) error {
		copyCommand := Copy{
			src:          c.Args().Get(0),
			dst:          c.Args().Get(1),
			op:           c.Command.Name,
			fullCommand:  givenCommand(c),
			deleteSource: false,

			noClobber:      c.Bool("no-clobber"),
			ifSizeDiffer:   c.Bool("if-size-differ"),
			ifSourceNewer:  c.Bool("if-source-newer"),
			flatten:        c.Bool("flatten"),
			followSymlinks: !c.Bool("no-follow-symlinks"),
			storageClass:   storage.LookupClass(c.String("storage-class")),
			concurrency:    c.Int("concurrency"),
			partSize:       c.Int64("part-size") * megabytes,
		}
		return copyCommand.Run(c.Context)
	},
}
View Source
var DeleteCommand = &cli.Command{
	Name:               "rm",
	HelpName:           "rm",
	Usage:              "remove objects",
	CustomHelpTemplate: deleteHelpTemplate,
	Before: func(c *cli.Context) error {
		if !c.Args().Present() {
			return fmt.Errorf("expected at least 1 object to remove")
		}

		return sourcesHaveSameType(c.Args().Slice()...)
	},
	Action: func(c *cli.Context) error {
		return Delete(
			c.Context,
			c.Command.Name,
			givenCommand(c),
			c.Args().Slice()...,
		)
	},
}
View Source
var ListCommand = &cli.Command{
	Name:               "ls",
	HelpName:           "ls",
	Usage:              "list buckets and objects",
	CustomHelpTemplate: listHelpTemplate,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "etag",
			Aliases: []string{"e"},
			Usage:   "show entity tag (ETag) in the output",
		},
		&cli.BoolFlag{
			Name:    "humanize",
			Aliases: []string{"H"},
			Usage:   "human-readable output for object sizes",
		},
	},
	Before: func(c *cli.Context) error {
		if c.Args().Len() > 1 {
			return fmt.Errorf("expected only 1 argument")
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		if !c.Args().Present() {
			return ListBuckets(c.Context)
		}

		showEtag := c.Bool("etag")
		humanize := c.Bool("humanize")

		return List(
			c.Context,
			c.Args().First(),
			showEtag,
			humanize,
		)
	},
}
View Source
var MakeBucketCommand = &cli.Command{
	Name:               "mb",
	HelpName:           "mb",
	Usage:              "make bucket",
	CustomHelpTemplate: makeBucketHelpTemplate,
	Before: func(c *cli.Context) error {
		if c.Args().Len() != 1 {
			return fmt.Errorf("expected only 1 argument")
		}

		src := c.Args().First()
		bucket, err := url.New(src)
		if err != nil {
			return err
		}
		if !bucket.IsBucket() {
			return fmt.Errorf("invalid s3 bucket")
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		return MakeBucket(
			c.Context,
			c.Command.Name,
			c.Args().First(),
		)
	},
}
View Source
var MoveCommand = &cli.Command{
	Name:               "mv",
	HelpName:           "mv",
	Usage:              "move/rename objects",
	Flags:              copyCommandFlags,
	CustomHelpTemplate: moveHelpTemplate,
	Before: func(c *cli.Context) error {
		return CopyCommand.Before(c)
	},
	Action: func(c *cli.Context) error {
		copyCommand := Copy{
			src:          c.Args().Get(0),
			dst:          c.Args().Get(1),
			op:           c.Command.Name,
			fullCommand:  givenCommand(c),
			deleteSource: true,

			noClobber:     c.Bool("no-clobber"),
			ifSizeDiffer:  c.Bool("if-size-differ"),
			ifSourceNewer: c.Bool("if-source-newer"),
			flatten:       c.Bool("flatten"),
			storageClass:  storage.LookupClass(c.String("storage-class")),
		}

		return copyCommand.Run(c.Context)
	},
}
View Source
var RunCommand = &cli.Command{
	Name:               "run",
	HelpName:           "run",
	Usage:              "run commands in batch",
	CustomHelpTemplate: runHelpTemplate,
	Before: func(c *cli.Context) error {
		if c.Args().Len() > 1 {
			return fmt.Errorf("expected only 1 file")
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		reader := os.Stdin
		if c.Args().Len() == 1 {
			f, err := os.Open(c.Args().First())
			if err != nil {
				return err
			}
			defer f.Close()

			reader = f
		}

		pm := parallel.New(c.Int("numworkers"))
		defer pm.Close()

		waiter := parallel.NewWaiter()

		var errDoneCh = make(chan bool)
		go func() {
			defer close(errDoneCh)
			for range waiter.Err() {

			}
		}()

		scanner := NewScanner(c.Context, reader)
		lineno := -1
		for line := range scanner.Scan() {
			lineno++

			line = strings.Split(line, " #")[0]

			line = strings.TrimSpace(line)
			if line == "" {
				continue
			}

			if strings.HasPrefix(line, "#") {
				continue
			}

			fields := strings.Fields(line)
			if len(fields) == 0 {
				continue
			}

			if fields[0] == "run" {
				err := fmt.Errorf("%q command (line: %v) is not permitted in run-mode", "run", lineno)
				printError(givenCommand(c), c.Command.Name, err)
				continue
			}

			fn := func() error {
				subcmd := fields[0]

				cmd := app.Command(subcmd)
				if cmd == nil {
					err := fmt.Errorf("%q command (line: %v) not found", subcmd, lineno)
					printError(givenCommand(c), c.Command.Name, err)
					return nil
				}

				flagset := flag.NewFlagSet(subcmd, flag.ExitOnError)
				if err := flagset.Parse(fields); err != nil {
					printError(givenCommand(c), c.Command.Name, err)
					return nil
				}

				ctx := cli.NewContext(app, flagset, c)
				return cmd.Run(ctx)
			}

			pm.Run(fn, waiter)
		}

		waiter.Wait()
		<-errDoneCh

		return scanner.Err()
	},
}
View Source
var SizeCommand = &cli.Command{
	Name:               "du",
	HelpName:           "du",
	Usage:              "show object size usage",
	CustomHelpTemplate: sizeHelpTemplate,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "group",
			Aliases: []string{"g"},
			Usage:   "group sizes by storage class",
		},
		&cli.BoolFlag{
			Name:    "humanize",
			Aliases: []string{"H"},
			Usage:   "human-readable output for object sizes",
		},
	},
	Before: func(c *cli.Context) error {
		if c.Args().Len() != 1 {
			return fmt.Errorf("expected only 1 argument")
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		groupByClass := c.Bool("group")
		humanize := c.Bool("humanize")

		return Size(
			c.Context,
			c.Args().First(),
			groupByClass,
			humanize,
		)
	},
}
View Source
var VersionCommand = &cli.Command{
	Name:     "version",
	HelpName: "version",
	Usage:    "print version",
	Action: func(c *cli.Context) error {
		fmt.Println(version.GetHumanVersion())
		return nil
	},
}

Functions

func Cat

func Cat(ctx context.Context, src *url.URL) error

func Delete

func Delete(
	ctx context.Context,
	op string,
	fullCommand string,
	src ...string,
) error

func List

func List(
	ctx context.Context,
	src string,

	showEtag bool,
	humanize bool,
) error

func ListBuckets

func ListBuckets(ctx context.Context) error

func Main

func Main(ctx context.Context, args []string) error

func MakeBucket

func MakeBucket(
	ctx context.Context,
	op string,
	src string,
) error

func Size

func Size(
	ctx context.Context,
	src string,
	groupByClass bool,
	humanize bool,
) error

func Validate

func Validate(c *cli.Context) error

Types

type Copy

type Copy struct {
	// contains filtered or unexported fields
}

func (Copy) Run

func (c Copy) Run(ctx context.Context) error

type ListMessage

type ListMessage struct {
	Object *storage.Object `json:"object"`
	// contains filtered or unexported fields
}

ListMessage is a structure for logging ls results.

func (ListMessage) JSON

func (l ListMessage) JSON() string

JSON returns the JSON representation of ListMessage.

func (ListMessage) String

func (l ListMessage) String() string

String returns the string representation of ListMessage.

type Scanner

type Scanner struct {
	*bufio.Scanner
	// contains filtered or unexported fields
}

Scanner is a cancelable scanner.

func NewScanner

func NewScanner(ctx context.Context, r io.Reader) *Scanner

NewScanner creates a new scanner with cancellation.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns encountered errors, if any.

func (*Scanner) Scan

func (s *Scanner) Scan() <-chan string

Scan returns read-only channel to consume lines.

type SizeMessage

type SizeMessage struct {
	Source       string `json:"source"`
	StorageClass string `json:"storage_class,omitempty"`
	Count        int64  `json:"count"`
	Size         int64  `json:"size"`
	// contains filtered or unexported fields
}

SizeMessage is the structure for logging disk usage.

func (SizeMessage) JSON

func (s SizeMessage) JSON() string

JSON returns the JSON representation of SizeMessage.

func (SizeMessage) String

func (s SizeMessage) String() string

String returns the string representation of SizeMessage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL