Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / utils.go
1 package command
2
3 import (
4         "bufio"
5         "fmt"
6         "io"
7         "os"
8         "path/filepath"
9         "runtime"
10         "strings"
11
12         "github.com/docker/docker/api/types/filters"
13         "github.com/docker/docker/pkg/system"
14 )
15
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_")
21         if err != nil {
22                 return err
23         }
24
25         tmpPath := tmpFile.Name()
26
27         _, err = io.Copy(tmpFile, r)
28         tmpFile.Close()
29
30         if err != nil {
31                 os.Remove(tmpPath)
32                 return err
33         }
34
35         if err = os.Rename(tmpPath, outfile); err != nil {
36                 os.Remove(tmpPath)
37                 return err
38         }
39
40         return nil
41 }
42
43 // capitalizeFirst capitalizes the first character of string
44 func capitalizeFirst(s string) string {
45         switch l := len(s); l {
46         case 0:
47                 return s
48         case 1:
49                 return strings.ToLower(s)
50         default:
51                 return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
52         }
53 }
54
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) {
58         case nil:
59                 return "None"
60         case string:
61                 return capitalizeFirst(t)
62         default:
63                 return capitalizeFirst(fmt.Sprintf("%s", t))
64         }
65 }
66
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 {
73         if message == "" {
74                 message = "Are you sure you want to proceed?"
75         }
76         message += " [y/N] "
77
78         fmt.Fprintf(outs, message)
79
80         // On Windows, force the use of the regular OS stdin stream.
81         if runtime.GOOS == "windows" {
82                 ins = NewInStream(os.Stdin)
83         }
84
85         reader := bufio.NewReader(ins)
86         answer, _, _ := reader.ReadLine()
87         return strings.ToLower(string(answer)) == "y"
88 }
89
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 {
93                 return pruneFilters
94         }
95         for _, f := range dockerCli.ConfigFile().PruneFilters {
96                 parts := strings.SplitN(f, "=", 2)
97                 if len(parts) != 2 {
98                         continue
99                 }
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]) {
105                                 continue
106                         }
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]) {
112                                 continue
113                         }
114                 }
115                 pruneFilters.Add(parts[0], parts[1])
116         }
117
118         return pruneFilters
119 }