|
| 1 | +// Copyright 2019 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cli |
| 16 | + |
| 17 | +import ( |
| 18 | + |
| 19 | + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) |
| 20 | + // to ensure that `run` and `up local` can make use of them. |
| 21 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 22 | + |
| 23 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/add" |
| 24 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha" |
| 25 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/build" |
| 26 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/completion" |
| 27 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate" |
| 28 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/migrate" |
| 29 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/new" |
| 30 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/olmcatalog" |
| 31 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/printdeps" |
| 32 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/run" |
| 33 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/scorecard" |
| 34 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/test" |
| 35 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/up" |
| 36 | + "github.com/operator-framework/operator-sdk/cmd/operator-sdk/version" |
| 37 | + "github.com/operator-framework/operator-sdk/internal/flags" |
| 38 | + "github.com/operator-framework/operator-sdk/internal/util/projutil" |
| 39 | + |
| 40 | + log "github.com/sirupsen/logrus" |
| 41 | + "github.com/spf13/cobra" |
| 42 | + "github.com/spf13/viper" |
| 43 | +) |
| 44 | + |
| 45 | +// GetCLIRoot is intended to creeate the base command structure for the OSDK for use in CLI and documentation |
| 46 | +func GetCLIRoot() *cobra.Command { |
| 47 | + root := &cobra.Command{ |
| 48 | + Use: "operator-sdk", |
| 49 | + Short: "An SDK for building operators with ease", |
| 50 | + PersistentPreRun: func(cmd *cobra.Command, args []string) { |
| 51 | + if viper.GetBool(flags.VerboseOpt) { |
| 52 | + if err := projutil.SetGoVerbose(); err != nil { |
| 53 | + log.Fatalf("Could not set GOFLAGS: (%v)", err) |
| 54 | + } |
| 55 | + log.SetLevel(log.DebugLevel) |
| 56 | + log.Debug("Debug logging is set") |
| 57 | + } |
| 58 | + if err := checkGoModulesForCmd(cmd); err != nil { |
| 59 | + log.Fatal(err) |
| 60 | + } |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + root.AddCommand(add.NewCmd()) |
| 65 | + root.AddCommand(alpha.NewCmd()) |
| 66 | + root.AddCommand(build.NewCmd()) |
| 67 | + root.AddCommand(completion.NewCmd()) |
| 68 | + root.AddCommand(generate.NewCmd()) |
| 69 | + root.AddCommand(migrate.NewCmd()) |
| 70 | + root.AddCommand(new.NewCmd()) |
| 71 | + root.AddCommand(olmcatalog.NewCmd()) |
| 72 | + root.AddCommand(printdeps.NewCmd()) |
| 73 | + root.AddCommand(run.NewCmd()) |
| 74 | + root.AddCommand(scorecard.NewCmd()) |
| 75 | + root.AddCommand(test.NewCmd()) |
| 76 | + root.AddCommand(up.NewCmd()) |
| 77 | + root.AddCommand(version.NewCmd()) |
| 78 | + |
| 79 | + return root |
| 80 | +} |
| 81 | + |
| 82 | +func checkGoModulesForCmd(cmd *cobra.Command) (err error) { |
| 83 | + // Certain commands are able to be run anywhere or handle this check |
| 84 | + // differently in their CLI code. |
| 85 | + if skipCheckForCmd(cmd) { |
| 86 | + return nil |
| 87 | + } |
| 88 | + // Do not perform this check if the project is non-Go, as they will not |
| 89 | + // be using go modules. |
| 90 | + if !projutil.IsOperatorGo() { |
| 91 | + return nil |
| 92 | + } |
| 93 | + // Do not perform a go modules check if the working directory is not in |
| 94 | + // the project root, as some sub-commands might not require project root. |
| 95 | + // Individual subcommands will perform this check as needed. |
| 96 | + if err := projutil.CheckProjectRoot(); err != nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + return projutil.CheckGoModules() |
| 101 | +} |
| 102 | + |
| 103 | +var commandsToSkip = map[string]struct{}{ |
| 104 | + "new": struct{}{}, // Handles this logic in cmd/operator-sdk/new |
| 105 | + "migrate": struct{}{}, // Handles this logic in cmd/operator-sdk/migrate |
| 106 | + "operator-sdk": struct{}{}, // Alias for "help" |
| 107 | + "help": struct{}{}, |
| 108 | + "completion": struct{}{}, |
| 109 | + "version": struct{}{}, |
| 110 | + "print-deps": struct{}{}, // Does not require this logic |
| 111 | +} |
| 112 | + |
| 113 | +func skipCheckForCmd(cmd *cobra.Command) (skip bool) { |
| 114 | + if _, ok := commandsToSkip[cmd.Name()]; ok { |
| 115 | + return true |
| 116 | + } |
| 117 | + cmd.VisitParents(func(pc *cobra.Command) { |
| 118 | + if _, ok := commandsToSkip[pc.Name()]; ok { |
| 119 | + // The bare "operator-sdk" command will be checked above. |
| 120 | + if pc.Name() != "operator-sdk" { |
| 121 | + skip = true |
| 122 | + } |
| 123 | + } |
| 124 | + }) |
| 125 | + return skip |
| 126 | +} |
0 commit comments