@@ -8,30 +8,37 @@ import (
8
8
"path/filepath"
9
9
"strings"
10
10
11
- "github.com/spf13/pflag"
11
+ "cdr.dev/coder-cli/internal/sync"
12
+ "github.com/urfave/cli"
13
+ "golang.org/x/xerrors"
12
14
13
- "go.coder.com/cli"
14
15
"go.coder.com/flog"
15
-
16
- "cdr.dev/coder-cli/internal/sync"
17
16
)
18
17
19
- type syncCmd struct {
20
- init bool
21
- }
22
-
23
- func (cmd * syncCmd ) Spec () cli.CommandSpec {
24
- return cli.CommandSpec {
25
- Name : "sync" ,
26
- Usage : "[local directory] [<env name>:<remote directory>]" ,
27
- Desc : "establish a one way directory sync to a remote environment" ,
18
+ func makeSyncCmd () cli.Command {
19
+ var init bool
20
+ return cli.Command {
21
+ Name : "sync" ,
22
+ Usage : "synchronize local files to a Coder environment" ,
23
+ Description : "Establish a one way directory sync to a Coder environment." ,
24
+ ArgsUsage : "[local directory] [<env name>:<remote directory>]" ,
25
+ Before : func (c * cli.Context ) error {
26
+ if c .Args ().Get (0 ) == "" || c .Args ().Get (1 ) == "" {
27
+ return xerrors .Errorf ("[local] and [remote] arguments are required" )
28
+ }
29
+ return nil
30
+ },
31
+ Action : makeRunSync (& init ),
32
+ Flags : []cli.Flag {
33
+ cli.BoolFlag {
34
+ Name : "init" ,
35
+ Usage : "do initial transfer and exit" ,
36
+ Destination : & init ,
37
+ },
38
+ },
28
39
}
29
40
}
30
41
31
- func (cmd * syncCmd ) RegisterFlags (fl * pflag.FlagSet ) {
32
- fl .BoolVarP (& cmd .init , "init" , "i" , false , "do initial transfer and exit" )
33
- }
34
-
35
42
// version returns local rsync protocol version as a string.
36
43
func rsyncVersion () string {
37
44
cmd := exec .Command ("rsync" , "--version" )
@@ -49,63 +56,61 @@ func rsyncVersion() string {
49
56
return versionString [1 ]
50
57
}
51
58
52
- func (cmd * syncCmd ) Run (fl * pflag.FlagSet ) {
53
- var (
54
- local = fl .Arg (0 )
55
- remote = fl .Arg (1 )
56
- )
57
- if local == "" || remote == "" {
58
- exitUsage (fl )
59
- }
60
-
61
- entClient := requireAuth ()
62
-
63
- info , err := os .Stat (local )
64
- if err != nil {
65
- flog .Fatal ("%v" , err )
66
- }
67
- if ! info .IsDir () {
68
- flog .Fatal ("%s must be a directory" , local )
69
- }
70
-
71
- remoteTokens := strings .SplitN (remote , ":" , 2 )
72
- if len (remoteTokens ) != 2 {
73
- flog .Fatal ("remote misformatted" )
74
- }
75
- var (
76
- envName = remoteTokens [0 ]
77
- remoteDir = remoteTokens [1 ]
78
- )
79
-
80
- env := findEnv (entClient , envName )
81
-
82
- absLocal , err := filepath .Abs (local )
83
- if err != nil {
84
- flog .Fatal ("make abs path out of %v: %v" , local , absLocal )
85
- }
86
-
87
- s := sync.Sync {
88
- Init : cmd .init ,
89
- Env : env ,
90
- RemoteDir : remoteDir ,
91
- LocalDir : absLocal ,
92
- Client : entClient ,
93
- }
94
-
95
- localVersion := rsyncVersion ()
96
- remoteVersion , rsyncErr := s .Version ()
97
-
98
- if rsyncErr != nil {
99
- flog .Info ("Unable to determine remote rsync version. Proceeding cautiously." )
100
- } else if localVersion != remoteVersion {
101
- flog .Fatal ("rsync protocol mismatch: local = %v, remote = %v" , localVersion , rsyncErr )
102
- }
103
-
104
- for err == nil || err == sync .ErrRestartSync {
105
- err = s .Run ()
106
- }
107
-
108
- if err != nil {
109
- flog .Fatal ("%v" , err )
59
+ func makeRunSync (init * bool ) func (c * cli.Context ) {
60
+ return func (c * cli.Context ) {
61
+ var (
62
+ local = c .Args ().Get (0 )
63
+ remote = c .Args ().Get (1 )
64
+ )
65
+
66
+ entClient := requireAuth ()
67
+
68
+ info , err := os .Stat (local )
69
+ if err != nil {
70
+ flog .Fatal ("%v" , err )
71
+ }
72
+ if ! info .IsDir () {
73
+ flog .Fatal ("%s must be a directory" , local )
74
+ }
75
+
76
+ remoteTokens := strings .SplitN (remote , ":" , 2 )
77
+ if len (remoteTokens ) != 2 {
78
+ flog .Fatal ("remote misformatted" )
79
+ }
80
+ var (
81
+ envName = remoteTokens [0 ]
82
+ remoteDir = remoteTokens [1 ]
83
+ )
84
+
85
+ env := findEnv (entClient , envName )
86
+
87
+ absLocal , err := filepath .Abs (local )
88
+ if err != nil {
89
+ flog .Fatal ("make abs path out of %v: %v" , local , absLocal )
90
+ }
91
+
92
+ s := sync.Sync {
93
+ Init : * init ,
94
+ Env : env ,
95
+ RemoteDir : remoteDir ,
96
+ LocalDir : absLocal ,
97
+ Client : entClient ,
98
+ }
99
+
100
+ localVersion := rsyncVersion ()
101
+ remoteVersion , rsyncErr := s .Version ()
102
+
103
+ if rsyncErr != nil {
104
+ flog .Info ("Unable to determine remote rsync version. Proceeding cautiously." )
105
+ } else if localVersion != remoteVersion {
106
+ flog .Fatal ("rsync protocol mismatch: local = %v, remote = %v" , localVersion , rsyncErr )
107
+ }
108
+
109
+ for err == nil || err == sync .ErrRestartSync {
110
+ err = s .Run ()
111
+ }
112
+ if err != nil {
113
+ flog .Fatal ("%v" , err )
114
+ }
110
115
}
111
116
}
0 commit comments