@@ -25,49 +25,59 @@ var (
2525)
2626
2727var deployCmd = & cobra.Command {
28- Use : "deploy [collection]" ,
28+ Use : "deploy [collection] [env] " ,
2929 Short : "Deploy MongoDB data collections" ,
30- Long : `Deploy MongoDB data collections to development or production environment.` ,
31- Args : cobra .MaximumNArgs (1 ),
30+ Long : `Deploy MongoDB data collections to development or production environment.
31+
32+ Examples:
33+ musing deploy # All collections to dev
34+ musing deploy news # Deploy news to dev
35+ musing deploy news prod # Deploy news to prod` ,
36+ Args : cobra .MaximumNArgs (2 ),
3237 RunE : func (cmd * cobra.Command , args []string ) error {
3338 collection := "all"
39+ env := "dev"
3440 if len (args ) > 0 {
3541 collection = args [0 ]
3642 }
43+ if len (args ) > 1 {
44+ env = args [1 ]
45+ }
46+
47+ if env != "dev" && env != "prod" {
48+ return fmt .Errorf ("invalid environment: %s (use 'dev' or 'prod')" , env )
49+ }
3750
38- env , _ := cmd .Flags ().GetString ("env" )
3951 return deployData (collection , env )
4052 },
4153 ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
42- // Dynamic completion for collection names
43- config .MustFindProjectRoot ()
44- cfg := config .GetConfig ()
45- if cfg == nil {
46- return nil , cobra .ShellCompDirectiveNoFileComp
47- }
54+ if len (args ) == 0 {
55+ // First arg: collection names
56+ config .MustFindProjectRoot ()
57+ cfg := config .GetConfig ()
58+ if cfg == nil {
59+ return nil , cobra .ShellCompDirectiveNoFileComp
60+ }
4861
49- collections , err := mongo .DiscoverCollections (cfg .Database .DataDir )
50- if err != nil {
51- return nil , cobra .ShellCompDirectiveNoFileComp
52- }
62+ collections , err := mongo .DiscoverCollections (cfg .Database .DataDir )
63+ if err != nil {
64+ return nil , cobra .ShellCompDirectiveNoFileComp
65+ }
5366
54- var names []string
55- for name := range collections {
56- names = append (names , name )
67+ var names []string
68+ for name := range collections {
69+ names = append (names , name )
70+ }
71+ return names , cobra .ShellCompDirectiveNoFileComp
72+ }
73+ if len (args ) == 1 {
74+ // Second arg: environment
75+ return []string {"dev" , "prod" }, cobra .ShellCompDirectiveNoFileComp
5776 }
58- return names , cobra .ShellCompDirectiveNoFileComp
77+ return nil , cobra .ShellCompDirectiveNoFileComp
5978 },
6079}
6180
62- func init () {
63- deployCmd .Flags ().StringP ("env" , "e" , "dev" , "Environment: dev or prod" )
64-
65- // Add completion for env flag
66- deployCmd .RegisterFlagCompletionFunc ("env" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
67- return []string {"dev" , "prod" }, cobra .ShellCompDirectiveNoFileComp
68- })
69- }
70-
7181func deployData (collection , env string ) error {
7282 // Find and load project configuration
7383 config .MustFindProjectRoot ()
0 commit comments