12 "github.com/docker/docker/api/types/filters"
13 "github.com/docker/docker/pkg/system"
16 // CopyToFile writes the content of the reader to the specified file
17 func CopyToFile(outfile string, r io.Reader) error {
18 // We use sequential file access here to avoid depleting the standby list
19 // on Windows. On Linux, this is a call directly to ioutil.TempFile
20 tmpFile, err := system.TempFileSequential(filepath.Dir(outfile), ".docker_temp_")
25 tmpPath := tmpFile.Name()
27 _, err = io.Copy(tmpFile, r)
35 if err = os.Rename(tmpPath, outfile); err != nil {
43 // capitalizeFirst capitalizes the first character of string
44 func capitalizeFirst(s string) string {
45 switch l := len(s); l {
49 return strings.ToLower(s)
51 return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
55 // PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter.
56 func PrettyPrint(i interface{}) string {
57 switch t := i.(type) {
61 return capitalizeFirst(t)
63 return capitalizeFirst(fmt.Sprintf("%s", t))
67 // PromptForConfirmation requests and checks confirmation from user.
68 // This will display the provided message followed by ' [y/N] '. If
69 // the user input 'y' or 'Y' it returns true other false. If no
70 // message is provided "Are you sure you want to proceed? [y/N] "
71 // will be used instead.
72 func PromptForConfirmation(ins io.Reader, outs io.Writer, message string) bool {
74 message = "Are you sure you want to proceed?"
78 fmt.Fprintf(outs, message)
80 // On Windows, force the use of the regular OS stdin stream.
81 if runtime.GOOS == "windows" {
82 ins = NewInStream(os.Stdin)
85 reader := bufio.NewReader(ins)
86 answer, _, _ := reader.ReadLine()
87 return strings.ToLower(string(answer)) == "y"
90 // PruneFilters returns consolidated prune filters obtained from config.json and cli
91 func PruneFilters(dockerCli Cli, pruneFilters filters.Args) filters.Args {
92 if dockerCli.ConfigFile() == nil {
95 for _, f := range dockerCli.ConfigFile().PruneFilters {
96 parts := strings.SplitN(f, "=", 2)
100 if parts[0] == "label" {
101 // CLI label filter supersede config.json.
102 // If CLI label filter conflict with config.json,
103 // skip adding label! filter in config.json.
104 if pruneFilters.Include("label!") && pruneFilters.ExactMatch("label!", parts[1]) {
107 } else if parts[0] == "label!" {
108 // CLI label! filter supersede config.json.
109 // If CLI label! filter conflict with config.json,
110 // skip adding label filter in config.json.
111 if pruneFilters.Include("label") && pruneFilters.ExactMatch("label", parts[1]) {
115 pruneFilters.Add(parts[0], parts[1])