@@ -17,19 +17,23 @@ import (
1717
1818var VERSION = "0.0.2"
1919var (
20- serverURL string
21- verbose bool
22- dryRun bool
23- SetPublic bool
24- SetUnlisted bool
25- subscribing bool
26- pullKey string
27- pushKey string
28- replicaID string
29- logger * zap.Logger
30- showVersion bool
20+ serverURL string
21+ verbose bool
22+ dryRun bool
23+ SetPublic bool
24+ SetUnlisted bool
25+ subscribing bool
26+ pullKey string
27+ pushKey string
28+ commitMessageParam string
29+ replicaID string
30+ logger * zap.Logger
31+ showVersion bool
3132)
3233
34+ var NoCommitMessage = "<!--NO_SQLRSYNC_MESSAGE-->"
35+ var MAX_MESSAGE_SIZE = 4096
36+
3337var rootCmd = & cobra.Command {
3438 Use : "sqlrsync [ORIGIN] [REPLICA] or [LOCAL] or [REMOTE]" ,
3539 Short : "SQLRsync v" + VERSION ,
@@ -70,6 +74,19 @@ func runSync(cmd *cobra.Command, args []string) error {
7074 return cmd .Help ()
7175 }
7276
77+ var commitMessage []byte
78+
79+ if commitMessageParam == NoCommitMessage {
80+ commitMessage = nil
81+ } else if len (commitMessageParam ) == 0 {
82+
83+ } else {
84+ if len (commitMessageParam ) > MAX_MESSAGE_SIZE {
85+ return fmt .Errorf ("commit message too long (max %d characters)" , MAX_MESSAGE_SIZE )
86+ }
87+ commitMessage = []byte (commitMessageParam )
88+ }
89+
7390 // Preprocess variables
7491 serverURL = strings .TrimRight (serverURL , "/" )
7592
@@ -81,13 +98,45 @@ func runSync(cmd *cobra.Command, args []string) error {
8198
8299 versionRaw := strings .SplitN (remotePath , "@" , 2 )
83100 version := "latest"
101+
102+ // permitted version formats:
103+ // # <none - no @ or anything>
104+ // @ # just the at sign
105+ // @latest
106+ // @1
107+ // @30
108+ // @v1
109+ // @v30
110+ // @latest-1
111+ // @latest-20
112+ //
113+
114+ // NOT permitted:
115+ // @latest1
116+ // @latest+1
117+ // Therefore this is a good regexp for this https://regex101.com/r/LooJFS/1 /^(latest-\d+)|(latest)|v?(\d+)$/
84118 if len (versionRaw ) == 2 {
85- version = strings .TrimPrefix (strings .ToLower (versionRaw [1 ]) , "v" )
119+ verStr : = strings .ToLower (strings .TrimPrefix (versionRaw [1 ], "v" ) )
86120 remotePath = versionRaw [0 ]
87- versionCheck , _ := strconv .Atoi (version )
88- if strings .HasPrefix (version , "latest" ) && versionCheck <= 0 {
89- return fmt .Errorf ("invalid version specified: %s (must be `latest`, `latest-<number>`, or `<number>` where the number is greater than 0)" , version )
121+
122+ if ! strings .HasPrefix (verStr , "latest" ) && ! strings .HasPrefix (verStr , "latest-" ) {
123+ // Accept plain numbers
124+ if _ , err := strconv .Atoi (verStr ); err != nil {
125+ return fmt .Errorf ("invalid version specified: %s (must be `latest`, `latest-<number>`, or `<number>`)" , verStr )
126+ }
127+ } else {
128+ // Accept latest or latest-N
129+ if ! strings .HasPrefix (verStr , "latest" ) {
130+ return fmt .Errorf ("invalid version specified: %s (must be `latest`, `latest-<number>`, or `<number>`)" , verStr )
131+ }
132+ if strings .HasPrefix (verStr , "latest-" ) {
133+ numStr := strings .TrimPrefix (verStr , "latest-" )
134+ if n , err := strconv .Atoi (numStr ); err != nil || n <= 0 {
135+ return fmt .Errorf ("invalid version specified: %s (must be `latest`, `latest-<number>`, or `<number>` where number > 0)" , verStr )
136+ }
137+ }
90138 }
139+ version = verStr
91140 }
92141
93142 visibility := 0
@@ -111,6 +160,7 @@ func runSync(cmd *cobra.Command, args []string) error {
111160 ReplicaPath : remotePath , // For LOCAL TO LOCAL, remotePath is actually the replica path
112161 Version : version , // Could be extended to parse @version syntax
113162 Operation : operation ,
163+ CommitMessage : commitMessage ,
114164 SetVisibility : visibility ,
115165 DryRun : dryRun ,
116166 Logger : logger ,
@@ -220,8 +270,9 @@ func setupLogger() {
220270func init () {
221271 rootCmd .Flags ().StringVar (& pullKey , "pullKey" , "" , "Authentication key for PULL operations" )
222272 rootCmd .Flags ().StringVar (& pushKey , "pushKey" , "" , "Authentication key for PUSH operations" )
273+ rootCmd .Flags ().StringVarP (& commitMessageParam , "message" , "m" , NoCommitMessage , "Commit message for the PUSH operation" )
223274 rootCmd .Flags ().StringVar (& replicaID , "replicaID" , "" , "Replica ID for the remote database" )
224- rootCmd .Flags ().StringVarP (& serverURL , "server" , "s" , "wss://sqlrsync.com" , "Server URL for operations" )
275+ rootCmd .Flags ().StringVarP (& serverURL , "server" , "s" , "wss://sqlrsync.com" , "Server URL for remote operations" )
225276 rootCmd .Flags ().BoolVar (& subscribing , "subscribe" , false , "Enable subscription to PULL changes" )
226277 rootCmd .Flags ().BoolVar (& verbose , "verbose" , false , "Enable verbose logging" )
227278 rootCmd .Flags ().BoolVar (& SetUnlisted , "unlisted" , false , "Enable unlisted access to the replica (initial PUSH only)" )
0 commit comments