2016-09-08 13:11:39 -04:00
|
|
|
package command
|
2014-03-28 19:21:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-13 20:25:28 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-03-28 19:21:55 -04:00
|
|
|
"os"
|
2015-12-13 20:25:28 -05:00
|
|
|
"path/filepath"
|
2016-12-09 17:21:45 -05:00
|
|
|
"runtime"
|
2016-06-13 22:56:23 -04:00
|
|
|
"strings"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2016-06-08 15:58:45 -04:00
|
|
|
// CopyToFile writes the content of the reader to the specified file
|
2016-06-05 10:42:19 -04:00
|
|
|
func CopyToFile(outfile string, r io.Reader) error {
|
2015-12-13 20:25:28 -05:00
|
|
|
tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpPath := tmpFile.Name()
|
|
|
|
|
|
|
|
_, err = io.Copy(tmpFile, r)
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(tmpPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Rename(tmpPath, outfile); err != nil {
|
|
|
|
os.Remove(tmpPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-03 13:55:33 -05:00
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
// capitalizeFirst capitalizes the first character of string
|
|
|
|
func capitalizeFirst(s string) string {
|
|
|
|
switch l := len(s); l {
|
|
|
|
case 0:
|
|
|
|
return s
|
|
|
|
case 1:
|
|
|
|
return strings.ToLower(s)
|
|
|
|
default:
|
|
|
|
return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter.
|
|
|
|
func PrettyPrint(i interface{}) string {
|
|
|
|
switch t := i.(type) {
|
|
|
|
case nil:
|
|
|
|
return "None"
|
|
|
|
case string:
|
|
|
|
return capitalizeFirst(t)
|
|
|
|
default:
|
|
|
|
return capitalizeFirst(fmt.Sprintf("%s", t))
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 17:04:34 -04:00
|
|
|
|
2016-10-08 22:29:58 -04:00
|
|
|
// PromptForConfirmation requests and checks confirmation from user.
|
2016-09-22 17:04:34 -04:00
|
|
|
// This will display the provided message followed by ' [y/N] '. If
|
|
|
|
// the user input 'y' or 'Y' it returns true other false. If no
|
2016-10-08 22:29:58 -04:00
|
|
|
// message is provided "Are you sure you want to proceed? [y/N] "
|
2016-09-22 17:04:34 -04:00
|
|
|
// will be used instead.
|
|
|
|
func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool {
|
|
|
|
if message == "" {
|
2016-10-08 22:29:58 -04:00
|
|
|
message = "Are you sure you want to proceed?"
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
message += " [y/N] "
|
|
|
|
|
|
|
|
fmt.Fprintf(outs, message)
|
|
|
|
|
2016-12-09 17:21:45 -05:00
|
|
|
// On Windows, force the use of the regular OS stdin stream.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
ins = NewInStream(os.Stdin)
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:04:34 -04:00
|
|
|
answer := ""
|
|
|
|
n, _ := fmt.Fscan(ins, &answer)
|
|
|
|
if n != 1 || (answer != "y" && answer != "Y") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|