Skip to content

Commit f0a987e

Browse files
author
Mengqi Yu
committed
✨ dynamically register commands based on PROJECT version.
If PROJECT doesn't exist or PROJECT exists with version=2, register `create webhook`, but no `alpha webhook`. If PROJECT exists and version=1, register `alpha webhook` but no `create webhook`
1 parent 1afe5ad commit f0a987e

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

cmd/create.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ func newCreateCmd() *cobra.Command {
3333
}
3434
cmd.AddCommand(
3535
newAPICommand(),
36-
newWebhookV2Cmd(),
3736
)
37+
38+
foundProject, version := getProjectVersion()
39+
// It add webhook v2 command in the following 2 cases:
40+
// - There are no PROJECT file found.
41+
// - version == 2 is found in the PROJECT file.
42+
if !foundProject || version == "2" {
43+
cmd.AddCommand(
44+
newWebhookV2Cmd(),
45+
)
46+
}
47+
3848
return cmd
3949
}

cmd/docs.go

-33
This file was deleted.

cmd/main.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,16 @@ func main() {
117117
newInitProjectCmd(),
118118
newCreateCmd(),
119119
version.NewVersionCmd(),
120-
newDocsCmd(),
121-
newVendorUpdateCmd(),
122-
newAlphaCommand(),
123120
)
124121

122+
foundProject, version := getProjectVersion()
123+
if foundProject && version == "1" {
124+
rootCmd.AddCommand(
125+
newAlphaCommand(),
126+
newVendorUpdateCmd(),
127+
)
128+
}
129+
125130
if err := rootCmd.Execute(); err != nil {
126131
log.Fatal(err)
127132
}
@@ -181,3 +186,16 @@ After the scaffold is written, api will run make on the project.
181186
},
182187
}
183188
}
189+
190+
// getProjectVersion tries to load PROJECT file and returns if the file exist
191+
// and the version string
192+
func getProjectVersion() (bool, string) {
193+
if _, err := os.Stat("PROJECT"); os.IsNotExist(err) {
194+
return false, ""
195+
}
196+
projectInfo, err := scaffold.LoadProjectFile("PROJECT")
197+
if err != nil {
198+
log.Fatalf("failed to read the PROJECT file: %v", err)
199+
}
200+
return true, projectInfo.Version
201+
}

0 commit comments

Comments
 (0)