@@ -34,74 +34,75 @@ func NewAddCmd() *cli.Command {
3434 },
3535
3636 Action : func (c * cli.Context ) error {
37- // 1. get settings from the global config file.
38- settings := settings .GetSettings ()
39- if settings .ErrorEvent != nil {
40- return settings .ErrorEvent
41- }
37+ return KpmAdd (c )
38+ },
39+ }
40+ }
4241
43- // 2. acquire the lock of the package cache.
44- err := settings .AcquirePackageCacheLock ()
45- if err != nil {
46- return err
47- }
42+ func KpmAdd (c * cli.Context ) error {
43+ // 1. get settings from the global config file.
44+ settings := settings .GetSettings ()
45+ if settings .ErrorEvent != (* reporter .KpmEvent )(nil ) {
46+ return settings .ErrorEvent
47+ }
4848
49- defer func () {
50- // 3. release the lock of the package cache after the function returns.
51- releaseErr := settings .ReleasePackageCacheLock ()
52- if releaseErr != nil && err == nil {
53- err = releaseErr
54- }
55- }()
49+ // 2. acquire the lock of the package cache.
50+ err := settings .AcquirePackageCacheLock ()
51+ if err != nil {
52+ return err
53+ }
5654
57- pwd , err := os .Getwd ()
55+ defer func () {
56+ // 3. release the lock of the package cache after the function returns.
57+ releaseErr := settings .ReleasePackageCacheLock ()
58+ if releaseErr != nil && err == nil {
59+ err = releaseErr
60+ }
61+ }()
5862
59- if err != nil {
60- reporter .Fatal ("kpm: internal bugs, please contact us to fix it" )
61- }
63+ pwd , err := os .Getwd ()
6264
63- globalPkgPath , err := env .GetAbsPkgPath ()
64- if err != nil {
65- return err
66- }
65+ if err != nil {
66+ return reporter .NewErrorEvent (reporter .Bug , err , "internal bugs, please contact us to fix it." )
67+ }
6768
68- kclPkg , err := pkg . LoadKclPkg ( pwd )
69- if err != nil {
70- reporter . Fatal ( "kpm: could not load `kcl.mod` in `" , pwd , "`" )
71- }
69+ globalPkgPath , err := env . GetAbsPkgPath ( )
70+ if err != nil {
71+ return err
72+ }
7273
73- err = kclPkg . ValidateKpmHome ( globalPkgPath )
74- if err != nil {
75- return err
76- }
74+ kclPkg , err := pkg . LoadKclPkg ( pwd )
75+ if err != nil {
76+ return err
77+ }
7778
78- addOpts , err := parseAddOptions ( c , globalPkgPath )
79- if err != nil {
80- return err
81- }
79+ err = kclPkg . ValidateKpmHome ( globalPkgPath )
80+ if err != ( * reporter . KpmEvent )( nil ) {
81+ return err
82+ }
8283
83- err = addOpts . Validate ( )
84- if err != nil {
85- return err
86- }
84+ addOpts , err := parseAddOptions ( c , globalPkgPath )
85+ if err != nil {
86+ return err
87+ }
8788
88- err = kclPkg .AddDeps (addOpts )
89- if err != nil {
90- return err
91- }
92- reporter .Report ("kpm: add dependency successfully." )
93- return nil
94- },
89+ err = addOpts .Validate ()
90+ if err != nil {
91+ return err
92+ }
93+
94+ err = kclPkg .AddDeps (addOpts )
95+ if err != nil {
96+ return err
9597 }
98+ return nil
9699}
97100
98101// onlyOnceOption is used to check that the value of some parameters can only appear once.
99- func onlyOnceOption (c * cli.Context , name string ) (* string , error ) {
102+ func onlyOnceOption (c * cli.Context , name string ) (* string , * reporter. KpmEvent ) {
100103 inputOpt := c .StringSlice (name )
101104 if len (inputOpt ) > 1 {
102- reporter .ExitWithReport ("kpm: the argument '" , name , "' cannot be used multiple times" )
103- reporter .ExitWithReport ("kpm: run 'kpm add help' for more information." )
104- return nil , fmt .Errorf ("kpm: Invalid command" )
105+ return nil , reporter .NewErrorEvent (reporter .InvalidCmd , fmt .Errorf ("the argument '%s' cannot be used multiple times" , name ))
105106 } else if len (inputOpt ) == 1 {
106107 return & inputOpt [0 ], nil
107108 } else {
@@ -114,7 +115,10 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
114115 // parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'.
115116 if c .NArg () == 0 {
116117 gitOpts , err := parseGitRegistryOptions (c )
117- if err != nil {
118+ if err != (* reporter .KpmEvent )(nil ) {
119+ if err .Type () == reporter .InvalidGitUrl {
120+ return nil , reporter .NewErrorEvent (reporter .InvalidCmd , errors .InvalidAddOptions )
121+ }
118122 return nil , err
119123 }
120124 return & opt.AddOptions {
@@ -135,19 +139,27 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
135139}
136140
137141// parseGitRegistryOptions will parse the git registry information from user cli inputs.
138- func parseGitRegistryOptions (c * cli.Context ) (* opt.RegistryOptions , error ) {
142+ func parseGitRegistryOptions (c * cli.Context ) (* opt.RegistryOptions , * reporter. KpmEvent ) {
139143 gitUrl , err := onlyOnceOption (c , "git" )
140144
141- if err != nil {
142- return nil , nil
145+ if err != ( * reporter . KpmEvent )( nil ) {
146+ return nil , err
143147 }
144148
145149 gitTag , err := onlyOnceOption (c , "tag" )
146150
147- if err != nil {
151+ if err != ( * reporter . KpmEvent )( nil ) {
148152 return nil , err
149153 }
150154
155+ if gitUrl == nil {
156+ return nil , reporter .NewErrorEvent (reporter .InvalidGitUrl , fmt .Errorf ("the argument 'git' is required" ))
157+ }
158+
159+ if gitTag == nil {
160+ return nil , reporter .NewErrorEvent (reporter .WithoutGitTag , fmt .Errorf ("the argument 'tag' is required" ))
161+ }
162+
151163 return & opt.RegistryOptions {
152164 Git : & opt.GitOptions {
153165 Url : * gitUrl ,
@@ -188,11 +200,11 @@ func parseOciPkgNameAndVersion(s string) (string, string, error) {
188200 }
189201
190202 if len (parts ) > 2 {
191- return "" , "" , errors . InvalidAddOptionsInvalidOciRef
203+ return "" , "" , reporter . NewErrorEvent ( reporter . InvalidPkgRef , fmt . Errorf ( "invalid oci package reference '%s'" , s ))
192204 }
193205
194206 if parts [1 ] == "" {
195- return "" , "" , errors . InvalidAddOptionsInvalidOciRef
207+ return "" , "" , reporter . NewErrorEvent ( reporter . InvalidPkgRef , fmt . Errorf ( "invalid oci package reference '%s'" , s ))
196208 }
197209
198210 return parts [0 ], parts [1 ], nil
0 commit comments