Browse code

oc rsync: pass-thru global command line options

Cesar Wong authored on 2015/12/01 03:18:46
Showing 3 changed files
... ...
@@ -3,12 +3,15 @@ package rsync
3 3
 import (
4 4
 	"bytes"
5 5
 	"errors"
6
+	"fmt"
6 7
 	"io"
7 8
 	"strings"
8 9
 
9 10
 	"github.com/golang/glog"
10 11
 	"github.com/spf13/cobra"
12
+	"github.com/spf13/pflag"
11 13
 	kerrors "k8s.io/kubernetes/pkg/util/errors"
14
+	"k8s.io/kubernetes/pkg/util/sets"
12 15
 
13 16
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
14 17
 )
... ...
@@ -24,13 +27,19 @@ type rsyncStrategy struct {
24 24
 	RemoteExecutor executor
25 25
 }
26 26
 
27
+var rshExcludeFlags = sets.NewString("delete", "strategy", "quiet")
28
+
27 29
 func newRsyncStrategy(f *clientcmd.Factory, c *cobra.Command, o *RsyncOptions) (copyStrategy, error) {
28 30
 	// Determine the rsh command to pass to the local rsync command
29 31
 	rsh := siblingCommand(c, "rsh")
30
-	rshCmd := []string{rsh, "-n", o.Namespace}
31
-	if len(o.ContainerName) > 0 {
32
-		rshCmd = append(rshCmd, "-c", o.ContainerName)
33
-	}
32
+	rshCmd := []string{rsh}
33
+	// Append all original flags to rsh command
34
+	c.Flags().Visit(func(flag *pflag.Flag) {
35
+		if rshExcludeFlags.Has(flag.Name) {
36
+			return
37
+		}
38
+		rshCmd = append(rshCmd, fmt.Sprintf("--%s=%s", flag.Name, flag.Value.String()))
39
+	})
34 40
 	rshCmdStr := strings.Join(rshCmd, " ")
35 41
 	glog.V(4).Infof("Rsh command: %s", rshCmdStr)
36 42
 
37 43
new file mode 100644
... ...
@@ -0,0 +1,24 @@
0
+package rsync
1
+
2
+import (
3
+	"io/ioutil"
4
+	"testing"
5
+
6
+	"github.com/spf13/pflag"
7
+	"k8s.io/kubernetes/pkg/util/sets"
8
+)
9
+
10
+// rshAllowedFlags is the set of flags in the rsync command that
11
+// can be passed to the rsh command
12
+var rshAllowedFlags = sets.NewString("container")
13
+
14
+// TestRshExcludeFlags ensures that only rsync flags that are allowed to be set on the rsh flag
15
+// are not included in the rshExcludeFlags set.
16
+func TestRshExcludeFlags(t *testing.T) {
17
+	rsyncCmd := NewCmdRsync("rsync", "oc", nil, ioutil.Discard, ioutil.Discard)
18
+	rsyncCmd.Flags().VisitAll(func(flag *pflag.Flag) {
19
+		if !rshExcludeFlags.Has(flag.Name) && !rshAllowedFlags.Has(flag.Name) {
20
+			t.Errorf("Unknown flag %s. Please add to rshExcludeFlags or to rshAllowedFlags", flag.Name)
21
+		}
22
+	})
23
+}
... ...
@@ -88,6 +88,9 @@ func NewCmdRsync(name, parent string, f *clientcmd.Factory, out, errOut io.Write
88 88
 		},
89 89
 	}
90 90
 
91
+	// NOTE: When adding new flags to the command, please update the rshExcludeFlags in copyrsync.go
92
+	// if those flags should not be passed to the rsh command.
93
+
91 94
 	cmd.Flags().StringVarP(&o.ContainerName, "container", "c", "", "Container within the pod")
92 95
 	cmd.Flags().StringVar(&o.StrategyName, "strategy", "", "Specify which strategy to use for copy: rsync, rsync-daemon, or tar")
93 96