Skip to content

Instantly share code, notes, and snippets.

@Prnyself
Created August 8, 2019 10:17
Show Gist options
  • Save Prnyself/d75cc8d408a341bea92d550e2a846e67 to your computer and use it in GitHub Desktop.
Save Prnyself/d75cc8d408a341bea92d550e2a846e67 to your computer and use it in GitHub Desktop.
set params from cmd
package main
import (
"github.com/spf13/cobra"
"github.com/yunify/qsctl/v2/action"
"github.com/yunify/qsctl/v2/utils"
)
// CpCommand will handle copy command.
var CpCommand = &cobra.Command{
Use: "cp <source-path> <dest-path>",
Short: "copy from/to qingstor",
Long: "qsctl cp can copy file/folder/stdin to qingstor or copy qingstor objects to local/stdout",
Example: utils.AlignPrintWithColon(
"Copy file: qsctl cp /path/to/file qs://prefix/a",
"Copy folder: qsctl cp qs://prefix/a /path/to/folder -r",
"Read from stdin: cat /path/to/file | qsctl cp - qs://prefix/stdin",
"Write to stdout: qsctl cp qs://prefix/b - > /path/to/file",
),
Args: cobra.ExactArgs(2),
RunE: cpRun,
PreRunE: validateCpFlag,
}
func cpRun(_ *cobra.Command, args []string) (err error) {
// Package context
cpHandler := &action.CopyHandler{
FlagHandler: (&action.FlagHandler{}).
WithExpectSize(expectSize).
WithMaximumMemory(maximumMemoryContent).
WithZone(zone),
}
return cpHandler.WithSrc(args[0]).WithDest(args[1]).Copy()
}
func initCpFlag() {
CpCommand.PersistentFlags().StringVar(&_expectSize,
"expect-size",
"",
"expected size of the input file"+
"accept: 100MB, 1.8G\n"+
"(only used and required for input from stdin)",
)
CpCommand.PersistentFlags().StringVar(&_maximumMemoryContent,
"maximum-memory-content",
"",
"maximum content loaded in memory\n"+
"(only used for input from stdin)",
)
}
func validateCpFlag(_ *cobra.Command, _ []string) (err error) {
if _expectSize != "" {
expectSize, err = utils.ParseByteSize(_expectSize)
if err != nil {
return err
}
}
if _maximumMemoryContent != "" {
maximumMemoryContent, err = utils.ParseByteSize(_maximumMemoryContent)
if err != nil {
return err
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment