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 Jul 2, 2019
1 parent 3ae3873 commit 396a014
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
12 changes: 11 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ func newCreateCmd() *cobra.Command {
}
cmd.AddCommand(
newAPICommand(),
newWebhookV2Cmd(),
)

foundProject, version := getProjectVersion()
// It add webhook v2 command in the following 2 cases:
// - There are no PROJECT file found.
// - version == 2 is found in the PROJECT file.
if !foundProject || version == "2" {
cmd.AddCommand(
newWebhookV2Cmd(),
)
}

return cmd
}
33 changes: 0 additions & 33 deletions cmd/docs.go

This file was deleted.

24 changes: 21 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ func main() {
newInitProjectCmd(),
newCreateCmd(),
version.NewVersionCmd(),
newDocsCmd(),
newVendorUpdateCmd(),
newAlphaCommand(),
)

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

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -181,3 +186,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 396a014

Please sign in to comment.