@@ -19,6 +19,7 @@ import (
1919 "encoding/json"
2020 "fmt"
2121 "os"
22+ "slices"
2223 "strings"
2324 "time"
2425
@@ -42,8 +43,8 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
4243 Short : "Create a migration" ,
4344 Args : cobra .NoArgs ,
4445 Example : fmt .Sprintf (` Create a migration:
45- $ %[1]s serverless migration create -c <cluster-id> --display-name <name> --config-file <file-path> --dry-run
46- $ %[1]s serverless migration create -c <cluster-id> --display-name <name> --config-file <file-path>
46+ $ %[1]s serverless migration create -c <cluster-id> --display-name <name> --config-file <file-path> --dry-run
47+ $ %[1]s serverless migration create -c <cluster-id> --display-name <name> --config-file <file-path>
4748` , config .CliName ),
4849 PreRunE : func (cmd * cobra.Command , args []string ) error {
4950 return markCreateMigrationRequiredFlags (cmd )
@@ -67,6 +68,9 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
6768 if err != nil {
6869 return errors .Trace (err )
6970 }
71+ if strings .TrimSpace (name ) == "" {
72+ return errors .New ("display name is required" )
73+ }
7074 configPath , err := cmd .Flags ().GetString (flag .MigrationConfigFile )
7175 if err != nil {
7276 return errors .Trace (err )
@@ -81,9 +85,6 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
8185 }
8286 definitionStr := string (definitionBytes )
8387
84- if strings .TrimSpace (name ) == "" {
85- return errors .New ("display name is required" )
86- }
8788 sources , target , mode , err := parseMigrationDefinition (definitionStr )
8889 if err != nil {
8990 return err
@@ -111,16 +112,16 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
111112 return errors .Trace (err )
112113 }
113114
114- taskID := aws .ToString (resp .MigrationId )
115- fmt .Fprintln (h .IOStreams .Out , color .GreenString ("migration %s(%s) created" , name , taskID ))
115+ migrationID := aws .ToString (resp .MigrationId )
116+ fmt .Fprintln (h .IOStreams .Out , color .GreenString ("migration %s(%s) created" , name , migrationID ))
116117 return nil
117118 },
118119 }
119120
120121 cmd .Flags ().StringP (flag .ClusterID , flag .ClusterIDShort , "" , "The ID of the target cluster." )
121122 cmd .Flags ().StringP (flag .DisplayName , flag .DisplayNameShort , "" , "Display name for the migration." )
122123 cmd .Flags ().String (flag .MigrationConfigFile , "" , "Path to a migration config JSON file. Use \" ticloud serverless migration template --mode <mode>\" to print templates." )
123- cmd .Flags ().Bool (flag .MigrationDryRun , false , "Run a migration precheck (dry run) with the provided inputs without creating a task ." )
124+ cmd .Flags ().Bool (flag .MigrationDryRun , false , "Run a migration precheck (dry run) with the provided inputs without creating a migration ." )
124125
125126 return cmd
126127}
@@ -168,7 +169,7 @@ func runMigrationPrecheck(ctx context.Context, client cloud.TiDBCloudClient, clu
168169 if err != nil {
169170 return errors .Trace (err )
170171 }
171- finished , err := printPrecheckSummary (precheckID , result . GetStatus (), result , h )
172+ finished , err := printPrecheckSummary (result , h )
172173 if err != nil {
173174 return err
174175 }
@@ -195,15 +196,15 @@ func isPrecheckUnfinished(status pkgmigration.MigrationPrecheckStatus) bool {
195196 }
196197}
197198
198- func printPrecheckSummary (id string , status pkgmigration. MigrationPrecheckStatus , result * pkgmigration.MigrationPrecheck , h * internal.Helper ) (bool , error ) {
199- if isPrecheckUnfinished (status ) {
200- fmt .Fprintf (h .IOStreams .Out , "precheck %s summary (status %s)\n " , id , status )
199+ func printPrecheckSummary (result * pkgmigration.MigrationPrecheck , h * internal.Helper ) (bool , error ) {
200+ if isPrecheckUnfinished (result . GetStatus () ) {
201+ fmt .Fprintf (h .IOStreams .Out , "precheck %s summary (status %s)\n " , result . GetPrecheckId (), result . GetStatus () )
201202 fmt .Fprintf (h .IOStreams .Out , "Total: %d, Success: %d, Warn: %d, Failed: %d\n " ,
202203 aws .ToInt32 (result .Total ), aws .ToInt32 (result .SuccessCnt ), aws .ToInt32 (result .WarnCnt ), aws .ToInt32 (result .FailedCnt ))
203204 return false , nil
204205 }
205206
206- fmt .Fprintf (h .IOStreams .Out , "precheck %s finished with status %s\n " , id , status )
207+ fmt .Fprintf (h .IOStreams .Out , "precheck %s finished with status %s\n " , result . GetPrecheckId (), result . GetStatus () )
207208 fmt .Fprintf (h .IOStreams .Out , "Total: %d, Success: %d, Warn: %d, Failed: %d\n " ,
208209 aws .ToInt32 (result .Total ), aws .ToInt32 (result .SuccessCnt ), aws .ToInt32 (result .WarnCnt ), aws .ToInt32 (result .FailedCnt ))
209210 if len (result .Items ) == 0 {
@@ -289,24 +290,14 @@ func parseMigrationDefinition(value string) ([]pkgmigration.Source, pkgmigration
289290func parseMigrationMode (value string ) (pkgmigration.TaskMode , error ) {
290291 trimmed := strings .TrimSpace (value )
291292 if trimmed == "" {
292- return "" , errors .New ("mode is required in the migration definition " )
293+ return "" , errors .New ("empty config file " )
293294 }
294295 normalized := strings .ToUpper (trimmed )
295296 mode := pkgmigration .TaskMode (normalized )
296- for _ , allowed := range pkgmigration .AllowedTaskModeEnumValues {
297- if mode == allowed {
298- return mode , nil
299- }
300- }
301- return "" , errors .Errorf ("invalid mode %q, allowed values: %s" , value , strings .Join (taskModeValues (), ", " ))
302- }
303-
304- func taskModeValues () []string {
305- values := make ([]string , 0 , len (pkgmigration .AllowedTaskModeEnumValues ))
306- for _ , mode := range pkgmigration .AllowedTaskModeEnumValues {
307- values = append (values , string (mode ))
297+ if slices .Contains (pkgmigration .AllowedTaskModeEnumValues , mode ) {
298+ return mode , nil
308299 }
309- return values
300+ return "" , errors . Errorf ( "invalid mode %q, allowed values: %s" , value , pkgmigration . AllowedTaskModeEnumValues )
310301}
311302
312303// standardizeJSON accepts JSON With Commas and Comments(JWCC) see
0 commit comments