diff --git a/pkg/conduit/internal/cli.go b/pkg/cli/cli.go similarity index 88% rename from pkg/conduit/internal/cli.go rename to pkg/cli/cli.go index 4088c86d0..8a0f9198b 100644 --- a/pkg/conduit/internal/cli.go +++ b/pkg/cli/cli.go @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package internal +package cli import ( "fmt" - "os" - "github.com/spf13/cobra" + "golang.org/x/exp/slices" + "os" ) type InitArgs struct { @@ -40,17 +40,34 @@ var ( Name: "example-pipeline", Path: "./pipelines/generator-to-log.yaml", } + rootCmd *cobra.Command ) +func init() { + rootCmd = buildRootCmd() +} + +func Enabled() bool { + for _, cmd := range os.Args[1:] { + for _, sub := range rootCmd.Commands() { + if sub.Name() == cmd || slices.Contains(sub.Aliases, cmd) { + return true + } + } + } + + return false +} + func SwitchToCLI() { - rootCmd := cmd() + rootCmd := buildRootCmd() if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } -func cmd() *cobra.Command { +func buildRootCmd() *cobra.Command { var rootCmd = &cobra.Command{ Use: "conduit", Short: "Conuit CLI", diff --git a/pkg/conduit/internal/conduit_init.go b/pkg/cli/conduit_init.go similarity index 97% rename from pkg/conduit/internal/conduit_init.go rename to pkg/cli/conduit_init.go index 3a09d5cb8..d2dafc8b8 100644 --- a/pkg/conduit/internal/conduit_init.go +++ b/pkg/cli/conduit_init.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package internal +package cli type ConduitInit struct { Path string diff --git a/pkg/conduit/internal/pipeline.tmpl b/pkg/cli/pipeline.tmpl similarity index 100% rename from pkg/conduit/internal/pipeline.tmpl rename to pkg/cli/pipeline.tmpl diff --git a/pkg/conduit/internal/pipelines_init.go b/pkg/cli/pipelines_init.go similarity index 97% rename from pkg/conduit/internal/pipelines_init.go rename to pkg/cli/pipelines_init.go index cb920016d..550a6a67a 100644 --- a/pkg/conduit/internal/pipelines_init.go +++ b/pkg/cli/pipelines_init.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package internal +package cli import ( _ "embed" @@ -153,14 +153,17 @@ type PipelinesInit struct { func (b PipelinesInit) Run() error { var pipeline pipelineTemplate - if b.Source == "" && b.Destination == "" { + switch { + case b.Source == "" && b.Destination == "": pipeline = b.buildDemoPipeline() - } else { + case b.Source != "" && b.Destination != "": p, err := b.buildTemplatePipeline() if err != nil { return err } pipeline = p + default: + return cerrors.Errorf("only one of --source, --destination was provided") } err := b.write(pipeline) diff --git a/pkg/conduit/entrypoint.go b/pkg/conduit/entrypoint.go index f29ec8a07..385212fbe 100644 --- a/pkg/conduit/entrypoint.go +++ b/pkg/conduit/entrypoint.go @@ -18,11 +18,11 @@ import ( "context" "flag" "fmt" + "github.com/conduitio/conduit/pkg/cli" "os" "os/signal" "strings" - "github.com/conduitio/conduit/pkg/conduit/internal" "github.com/conduitio/conduit/pkg/foundation/cerrors" "github.com/peterbourgon/ff/v3" "github.com/peterbourgon/ff/v3/ffyaml" @@ -53,8 +53,8 @@ type Entrypoint struct{} // - environment variables // - config file (lowest priority) func (e *Entrypoint) Serve(cfg Config) { - if e.cliMode() { - internal.SwitchToCLI() + if cli.Enabled() { + cli.SwitchToCLI() os.Exit(0) } @@ -205,6 +205,12 @@ func (e *Entrypoint) ParseConfig(flags *flag.FlagSet) { if err != nil { e.exitWithError(err) } + // Check the arguments that weren't parsed + if len(flags.Args()) > 0 { + _, _ = fmt.Fprintf(os.Stderr, "Unknown flag(s): %v\n", flags.Args()) + flags.Usage() + os.Exit(exitCodeErr) + } // check if the -version flag is set if *version { @@ -254,7 +260,3 @@ func (*Entrypoint) Splash() string { " ‘‘‘‘ " return fmt.Sprintf(splash, Version(true)) } - -func (e *Entrypoint) cliMode() bool { - return len(os.Args) > 1 && (os.Args[1] == "init" || os.Args[1] == "pipelines") -}