utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package command
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "github.com/docker/docker/api/types/filters"
  11. "github.com/docker/docker/pkg/system"
  12. )
  13. // CopyToFile writes the content of the reader to the specified file
  14. func CopyToFile(outfile string, r io.Reader) error {
  15. // We use sequential file access here to avoid depleting the standby list
  16. // on Windows. On Linux, this is a call directly to ioutil.TempFile
  17. tmpFile, err := system.TempFileSequential(filepath.Dir(outfile), ".docker_temp_")
  18. if err != nil {
  19. return err
  20. }
  21. tmpPath := tmpFile.Name()
  22. _, err = io.Copy(tmpFile, r)
  23. tmpFile.Close()
  24. if err != nil {
  25. os.Remove(tmpPath)
  26. return err
  27. }
  28. if err = os.Rename(tmpPath, outfile); err != nil {
  29. os.Remove(tmpPath)
  30. return err
  31. }
  32. return nil
  33. }
  34. // capitalizeFirst capitalizes the first character of string
  35. func capitalizeFirst(s string) string {
  36. switch l := len(s); l {
  37. case 0:
  38. return s
  39. case 1:
  40. return strings.ToLower(s)
  41. default:
  42. return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
  43. }
  44. }
  45. // PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter.
  46. func PrettyPrint(i interface{}) string {
  47. switch t := i.(type) {
  48. case nil:
  49. return "None"
  50. case string:
  51. return capitalizeFirst(t)
  52. default:
  53. return capitalizeFirst(fmt.Sprintf("%s", t))
  54. }
  55. }
  56. // PromptForConfirmation requests and checks confirmation from user.
  57. // This will display the provided message followed by ' [y/N] '. If
  58. // the user input 'y' or 'Y' it returns true other false. If no
  59. // message is provided "Are you sure you want to proceed? [y/N] "
  60. // will be used instead.
  61. func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool {
  62. if message == "" {
  63. message = "Are you sure you want to proceed?"
  64. }
  65. message += " [y/N] "
  66. fmt.Fprintf(outs, message)
  67. // On Windows, force the use of the regular OS stdin stream.
  68. if runtime.GOOS == "windows" {
  69. ins = NewInStream(os.Stdin)
  70. }
  71. reader := bufio.NewReader(ins)
  72. answer, _, _ := reader.ReadLine()
  73. return strings.ToLower(string(answer)) == "y"
  74. }
  75. // PruneFilters returns consolidated prune filters obtained from config.json and cli
  76. func PruneFilters(dockerCli Cli, pruneFilters filters.Args) filters.Args {
  77. if dockerCli.ConfigFile() == nil {
  78. return pruneFilters
  79. }
  80. for _, f := range dockerCli.ConfigFile().PruneFilters {
  81. parts := strings.SplitN(f, "=", 2)
  82. if len(parts) != 2 {
  83. continue
  84. }
  85. if parts[0] == "label" {
  86. // CLI label filter supersede config.json.
  87. // If CLI label filter conflict with config.json,
  88. // skip adding label! filter in config.json.
  89. if pruneFilters.Include("label!") && pruneFilters.ExactMatch("label!", parts[1]) {
  90. continue
  91. }
  92. } else if parts[0] == "label!" {
  93. // CLI label! filter supersede config.json.
  94. // If CLI label! filter conflict with config.json,
  95. // skip adding label filter in config.json.
  96. if pruneFilters.Include("label") && pruneFilters.ExactMatch("label", parts[1]) {
  97. continue
  98. }
  99. }
  100. pruneFilters.Add(parts[0], parts[1])
  101. }
  102. return pruneFilters
  103. }