Skip to content

Commit

Permalink
check unknown commands and flags
Browse files Browse the repository at this point in the history
  • Loading branch information
hariso committed Oct 25, 2024
1 parent 4f9ae9a commit 9dff8f4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
29 changes: 23 additions & 6 deletions pkg/conduit/internal/cli.go → pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal
package cli

import (
_ "embed"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions pkg/conduit/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

0 comments on commit 9dff8f4

Please sign in to comment.