Skip to content

Commit

Permalink
✨ dynamically register commands based on PROJECT version.
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Mengqi Yu committed Jun 28, 2019
1 parent edfc2ab commit f5f6768
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func newCreateCmd() *cobra.Command {
}
cmd.AddCommand(
newAPICommand(),
newWebhookV2Cmd(),
)

foundProject, version := getProjectVersion()
if !(foundProject && version == "1") {
cmd.AddCommand(
newWebhookV2Cmd(),
)
}

return cmd
}
21 changes: 20 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ func main() {
version.NewVersionCmd(),
newDocsCmd(),
newVendorUpdateCmd(),
newAlphaCommand(),
)

foundProject, version := getProjectVersion()
if foundProject && version == "1" {
rootCmd.AddCommand(
newAlphaCommand(),
)
}

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -181,3 +187,16 @@ After the scaffold is written, api will run make on the project.
},
}
}

// getProjectVersion tries to load PROJECT file and returns if the file exist
// and the version string
func getProjectVersion() (bool, string) {
if _, err := os.Stat("PROJECT"); os.IsNotExist(err) {
return false, ""
}
projectInfo, err := scaffold.LoadProjectFile("PROJECT")
if err != nil {
log.Fatalf("failed to read the PROJECT file: %v", err)
}
return true, projectInfo.Version
}

0 comments on commit f5f6768

Please sign in to comment.