Browse code

On Windows, use CRLF line endings in oc edit

Clayton Coleman authored on 2015/10/22 04:53:59
Showing 3 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"os"
9
+	"runtime"
9 10
 	"strings"
10 11
 
11 12
 	"k8s.io/kubernetes/pkg/api/errors"
... ...
@@ -19,11 +20,14 @@ import (
19 19
 
20 20
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
21 21
 	"github.com/openshift/origin/pkg/cmd/util/editor"
22
+	fileutil "github.com/openshift/origin/pkg/util/file"
22 23
 	"github.com/openshift/origin/pkg/util/jsonmerge"
23 24
 )
24 25
 
25 26
 // EditOptions is a struct that contains all variables needed for cli edit command.
26 27
 type EditOptions struct {
28
+	windowsLineEndings bool
29
+
27 30
 	out       io.Writer
28 31
 	printer   kubectl.ResourcePrinter
29 32
 	namespace string
... ...
@@ -50,7 +54,8 @@ be previously saved versions of resources.
50 50
 
51 51
 The files to edit will be output in the default API version, or a version specified
52 52
 by --output-version. The default format is YAML - if you would like to edit in JSON
53
-pass -o json.
53
+pass -o json. The flag --windows-line-endings can be used to force Windows line endings,
54
+otherwise the default for your operating system will be used.
54 55
 
55 56
 In the event an error occurs while updating, a temporary file will be created on disk
56 57
 that contains your unapplied changes. The most common error when updating a resource
... ...
@@ -94,6 +99,7 @@ func NewCmdEdit(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Com
94 94
 	kubectl.AddJsonFilenameFlag(cmd, &options.filenames, usage)
95 95
 	cmd.Flags().StringP("output", "o", "yaml", "Output format. One of: yaml|json.")
96 96
 	cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
97
+	cmd.Flags().BoolVar(&options.windowsLineEndings, "windows-line-endings", runtime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)")
97 98
 
98 99
 	return cmd
99 100
 }
... ...
@@ -162,12 +168,17 @@ func (o *EditOptions) RunEdit() error {
162 162
 
163 163
 		// generate the file to edit
164 164
 		buf := &bytes.Buffer{}
165
-		if _, err := results.header.WriteTo(buf); err != nil {
165
+		var w io.Writer = buf
166
+		if o.windowsLineEndings {
167
+			w = fileutil.NewCRLFWriter(w)
168
+		}
169
+		if _, err := results.header.WriteTo(w); err != nil {
166 170
 			return preservedFile(err, results.file, o.out)
167 171
 		}
168
-		if err := o.printer.PrintObj(obj, buf); err != nil {
172
+		if err := o.printer.PrintObj(obj, w); err != nil {
169 173
 			return preservedFile(err, results.file, o.out)
170 174
 		}
175
+
171 176
 		original := buf.Bytes()
172 177
 
173 178
 		// launch the editor
... ...
@@ -2,6 +2,8 @@ package file
2 2
 
3 3
 import (
4 4
 	"bufio"
5
+	"bytes"
6
+	"io"
5 7
 	"os"
6 8
 )
7 9
 
... ...
@@ -20,3 +22,36 @@ func ReadLines(fileName string) ([]string, error) {
20 20
 	}
21 21
 	return lines, scanner.Err()
22 22
 }
23
+
24
+type crlfWriter struct {
25
+	io.Writer
26
+}
27
+
28
+func NewCRLFWriter(w io.Writer) io.Writer {
29
+	return crlfWriter{w}
30
+}
31
+
32
+func (w crlfWriter) Write(b []byte) (n int, err error) {
33
+	for i, written := 0, 0; ; {
34
+		next := bytes.Index(b[i:], []byte("\n"))
35
+		if next == -1 {
36
+			n, err := w.Writer.Write(b[i:])
37
+			return written + n, err
38
+		}
39
+		next = next + i
40
+		n, err := w.Writer.Write(b[i:next])
41
+		if err != nil {
42
+			return written + n, err
43
+		}
44
+		written += n
45
+		n, err = w.Writer.Write([]byte("\r\n"))
46
+		if err != nil {
47
+			if n > 1 {
48
+				n = 1
49
+			}
50
+			return written + n, err
51
+		}
52
+		written += 1
53
+		i = next + 1
54
+	}
55
+}
... ...
@@ -12,7 +12,9 @@ os::log::install_errexit
12 12
 
13 13
 oc create -f examples/hello-openshift/hello-pod.json
14 14
 
15
-[ "$(OC_EDITOR='cat' oc edit pod/hello-openshift 2>&1 | grep 'Edit cancelled')" ]
16
-[ "$(OC_EDITOR='cat' oc edit pod/hello-openshift | grep 'name: hello-openshift')" ]
15
+[ "$(OC_EDITOR=cat oc edit pod/hello-openshift 2>&1 | grep 'Edit cancelled')" ]
16
+[ "$(OC_EDITOR=cat oc edit pod/hello-openshift | grep 'name: hello-openshift')" ]
17
+[ "$(OC_EDITOR=cat oc edit --windows-line-endings pod/hello-openshift | file - | grep CRLF)" ]
18
+[ ! "$(OC_EDITOR=cat oc edit --windows-line-endings=false pod/hello-openshift | file - | grep CRFL)" ]
17 19
 echo "edit: ok"
18 20