Skip to content

hack/doc: Autogen CLI Documentation #2099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- Added `Operator Version: X.Y.Z` information in the operator logs.([#1953](https://github.com/operator-framework/operator-sdk/pull/1953))
- Make Ansible verbosity configurable via the `ansible-verbosity` flag. ([#2087](https://github.com/operator-framework/operator-sdk/pull/2087))
- Autogenerate CLI documentation via `make cli-doc` ([#2099](https://github.com/operator-framework/operator-sdk/pull/2099))

### Changed

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ help: ## Show this help screen
all: format test build/operator-sdk ## Test and Build the Operator SDK

# Code management.
.PHONY: test format tidy clean
.PHONY: format tidy clean cli-doc

format: ## Format the source code
$(Q)go fmt $(PKGS)
Expand All @@ -54,6 +54,9 @@ tidy: ## Update dependencies
clean: ## Clean up the build artifacts
$(Q)rm -rf build

cli-doc: ## Generate CLI Documentation
./hack/doc/gen_cli_doc.sh

# Build/install/release the SDK.
.PHONY: install release_builds release

Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ is generated as a 'validation' object. Generation can be disabled with the
--skip-generation flag.

Example:

$ operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService
$ tree pkg/apis
pkg/apis/
Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/add/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ This command must be run from the project root directory.
If the controller pkg for that Kind already exists at pkg/controller/<kind> then the command will not overwrite and return an error.

Example:

$ operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService
$ tree pkg/controller
pkg/controller/
Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This image will be automatically set in the deployment manifests.
After build completes, the image would be built locally in docker. Then it needs to
be pushed to remote registry.
For example:

$ operator-sdk build quay.io/example/operator:v0.0.1
$ docker push quay.io/example/operator:v0.0.1
`,
Expand Down
126 changes: 126 additions & 0 deletions cmd/operator-sdk/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2019 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that `run` and `up local` can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/add"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/build"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/completion"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/migrate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/new"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/olmcatalog"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/printdeps"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/run"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/scorecard"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/test"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/up"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/version"
"github.com/operator-framework/operator-sdk/internal/flags"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// GetCLIRoot is intended to creeate the base command structure for the OSDK for use in CLI and documentation
func GetCLIRoot() *cobra.Command {
root := &cobra.Command{
Use: "operator-sdk",
Short: "An SDK for building operators with ease",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool(flags.VerboseOpt) {
if err := projutil.SetGoVerbose(); err != nil {
log.Fatalf("Could not set GOFLAGS: (%v)", err)
}
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
if err := checkGoModulesForCmd(cmd); err != nil {
log.Fatal(err)
}
},
}

root.AddCommand(add.NewCmd())
root.AddCommand(alpha.NewCmd())
root.AddCommand(build.NewCmd())
root.AddCommand(completion.NewCmd())
root.AddCommand(generate.NewCmd())
root.AddCommand(migrate.NewCmd())
root.AddCommand(new.NewCmd())
root.AddCommand(olmcatalog.NewCmd())
root.AddCommand(printdeps.NewCmd())
root.AddCommand(run.NewCmd())
root.AddCommand(scorecard.NewCmd())
root.AddCommand(test.NewCmd())
root.AddCommand(up.NewCmd())
root.AddCommand(version.NewCmd())

return root
}

func checkGoModulesForCmd(cmd *cobra.Command) (err error) {
// Certain commands are able to be run anywhere or handle this check
// differently in their CLI code.
if skipCheckForCmd(cmd) {
return nil
}
// Do not perform this check if the project is non-Go, as they will not
// be using go modules.
if !projutil.IsOperatorGo() {
return nil
}
// Do not perform a go modules check if the working directory is not in
// the project root, as some sub-commands might not require project root.
// Individual subcommands will perform this check as needed.
if err := projutil.CheckProjectRoot(); err != nil {
return nil
}

return projutil.CheckGoModules()
}

var commandsToSkip = map[string]struct{}{
"new": struct{}{}, // Handles this logic in cmd/operator-sdk/new
"migrate": struct{}{}, // Handles this logic in cmd/operator-sdk/migrate
"operator-sdk": struct{}{}, // Alias for "help"
"help": struct{}{},
"completion": struct{}{},
"version": struct{}{},
"print-deps": struct{}{}, // Does not require this logic
}

func skipCheckForCmd(cmd *cobra.Command) (skip bool) {
if _, ok := commandsToSkip[cmd.Name()]; ok {
return true
}
cmd.VisitParents(func(pc *cobra.Command) {
if _, ok := commandsToSkip[pc.Name()]; ok {
// The bare "operator-sdk" command will be checked above.
if pc.Name() != "operator-sdk" {
skip = true
}
}
})
return skip
}
1 change: 1 addition & 0 deletions cmd/operator-sdk/generate/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ specs in pkg/apis/<group>/<version> directories to comply with kube-API
requirements. Go code is generated under
pkg/apis/<group>/<version>/zz_generated.deepcopy.go.
Example:

$ operator-sdk generate k8s
$ tree pkg/apis
pkg/apis/
Expand Down
1 change: 1 addition & 0 deletions cmd/operator-sdk/generate/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ deploy/crds/<full group>_<resource>_crd.yaml; OpenAPI V3 validation YAML
is generated as a 'validation' object.

Example:

$ operator-sdk generate openapi
$ tree pkg/apis
pkg/apis/
Expand Down
95 changes: 2 additions & 93 deletions cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,15 @@ import (
// to ensure that `run` and `up local` can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/add"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/build"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/completion"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/migrate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/new"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/olmcatalog"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/printdeps"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/run"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/scorecard"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/test"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/up"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/version"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/cli"
"github.com/operator-framework/operator-sdk/internal/flags"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func main() {
root := &cobra.Command{
Use: "operator-sdk",
Short: "An SDK for building operators with ease",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool(flags.VerboseOpt) {
if err := projutil.SetGoVerbose(); err != nil {
log.Fatalf("Could not set GOFLAGS: (%v)", err)
}
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
if err := checkGoModulesForCmd(cmd); err != nil {
log.Fatal(err)
}
},
}

root.AddCommand(add.NewCmd())
root.AddCommand(alpha.NewCmd())
root.AddCommand(build.NewCmd())
root.AddCommand(completion.NewCmd())
root.AddCommand(generate.NewCmd())
root.AddCommand(migrate.NewCmd())
root.AddCommand(new.NewCmd())
root.AddCommand(olmcatalog.NewCmd())
root.AddCommand(printdeps.NewCmd())
root.AddCommand(run.NewCmd())
root.AddCommand(scorecard.NewCmd())
root.AddCommand(test.NewCmd())
root.AddCommand(up.NewCmd())
root.AddCommand(version.NewCmd())
root := cli.GetCLIRoot()

root.PersistentFlags().Bool(flags.VerboseOpt, false, "Enable verbose logging")
if err := viper.BindPFlags(root.PersistentFlags()); err != nil {
Expand All @@ -85,49 +40,3 @@ func main() {
os.Exit(1)
}
}

func checkGoModulesForCmd(cmd *cobra.Command) (err error) {
// Certain commands are able to be run anywhere or handle this check
// differently in their CLI code.
if skipCheckForCmd(cmd) {
return nil
}
// Do not perform this check if the project is non-Go, as they will not
// be using go modules.
if !projutil.IsOperatorGo() {
return nil
}
// Do not perform a go modules check if the working directory is not in
// the project root, as some sub-commands might not require project root.
// Individual subcommands will perform this check as needed.
if err := projutil.CheckProjectRoot(); err != nil {
return nil
}

return projutil.CheckGoModules()
}

var commandsToSkip = map[string]struct{}{
"new": struct{}{}, // Handles this logic in cmd/operator-sdk/new
"migrate": struct{}{}, // Handles this logic in cmd/operator-sdk/migrate
"operator-sdk": struct{}{}, // Alias for "help"
"help": struct{}{},
"completion": struct{}{},
"version": struct{}{},
"print-deps": struct{}{}, // Does not require this logic
}

func skipCheckForCmd(cmd *cobra.Command) (skip bool) {
if _, ok := commandsToSkip[cmd.Name()]; ok {
return true
}
cmd.VisitParents(func(pc *cobra.Command) {
if _, ok := commandsToSkip[pc.Name()]; ok {
// The bare "operator-sdk" command will be checked above.
if pc.Name() != "operator-sdk" {
skip = true
}
}
})
return skip
}
1 change: 1 addition & 0 deletions cmd/operator-sdk/new/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ generates a default directory layout based on the input <project-name>.
<project-name> is the project name of the new operator. (e.g app-operator)

For example:

$ mkdir $HOME/projects/example.com/
$ cd $HOME/projects/example.com/
$ operator-sdk new app-operator
Expand Down
32 changes: 32 additions & 0 deletions doc/cli/operator-sdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## operator-sdk

An SDK for building operators with ease

### Synopsis

An SDK for building operators with ease

### Options

```
-h, --help help for operator-sdk
```

### SEE ALSO

* [operator-sdk add](operator-sdk_add.md) - Adds a controller or resource to the project
* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand
* [operator-sdk build](operator-sdk_build.md) - Compiles code and builds artifacts
* [operator-sdk completion](operator-sdk_completion.md) - Generators for shell completions
* [operator-sdk generate](operator-sdk_generate.md) - Invokes specific generator
* [operator-sdk migrate](operator-sdk_migrate.md) - Adds source code to an operator
* [operator-sdk new](operator-sdk_new.md) - Creates a new operator application
* [operator-sdk olm-catalog](operator-sdk_olm-catalog.md) - Invokes a olm-catalog command
* [operator-sdk print-deps](operator-sdk_print-deps.md) - Print Golang packages and versions required to run the operator
* [operator-sdk run](operator-sdk_run.md) - Runs a generic operator
* [operator-sdk scorecard](operator-sdk_scorecard.md) - Run scorecard tests
* [operator-sdk test](operator-sdk_test.md) - Tests the operator
* [operator-sdk up](operator-sdk_up.md) - Launches the operator
* [operator-sdk version](operator-sdk_version.md) - Prints the version of operator-sdk

###### Auto generated by spf13/cobra on 6-Nov-2019
22 changes: 22 additions & 0 deletions doc/cli/operator-sdk_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## operator-sdk add

Adds a controller or resource to the project

### Synopsis

Adds a controller or resource to the project

### Options

```
-h, --help help for add
```

### SEE ALSO

* [operator-sdk](operator-sdk.md) - An SDK for building operators with ease
* [operator-sdk add api](operator-sdk_add_api.md) - Adds a new api definition under pkg/apis
* [operator-sdk add controller](operator-sdk_add_controller.md) - Adds a new controller pkg
* [operator-sdk add crd](operator-sdk_add_crd.md) - Adds a Custom Resource Definition (CRD) and the Custom Resource (CR) files

###### Auto generated by spf13/cobra on 6-Nov-2019
Loading