Categorygithub.com/arjit95/cobi
repositorypackage
0.1.1
Repository: https://github.com/arjit95/cobi.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

Build Status Issues MIT License Code Coverage

cobi

cobi (cobra Interactive) is a small wrapper on top of cobra and tview to build interactive cli applications
Explore the docs »

Examples · Report Bug · Request Feature

Table of Contents

About The Project

cobi Screenshot

Cobra provides a great way to build cli applications whereas tview provides powerful cui. But there are scenarios where the application needs to execute a long running task, for eg port forwarding in kubernetes. This could be solved with an interactive prompt, while normal operations can still work with the default cli application.

Overview

cobi works by using command completion provided by cobra. These completions are propagated to tview providing almost the same experience in both interactive and cli modes. Since cobi implements the same interface as cobra, it becomes very easy to port your existing project to cobi.

Installing

First, use go get to install the latest version of the library. This command will download cobi with all its dependencies:

go get -u github.com/arjit95/cobi

Next, include cobi in your app:

import "github.com/arjit95/cobi"

Migrating from cobra

Suppose you have an existing cobra command:

import "github.com/spf13/cobra"

cmd := &cobra.Command{
  Use:   "demo",
  Short: "This is a demo cobra command",
  Run: func(cmd *cobra.Command, args []string) {
    // Do Stuff Here
  },
}

This would be re-written as

import (
    "github.com/arjit95/cobi"
    "github.com/arjit95/cobi/editor"
    "github.com/spf13/cobra"
)

// Only wrapping the top most command is sufficient
// There is no need to touch other commands.
cmd := cobi.NewCommand(editor.NewEditor(), &cobra.Command{
  Use:   "demo",
  Short: "This is a demo cobra command",
  Run: func(cmd *cobra.Command, args []string) {
      // Do Stuff Here
  }, 
})

// Execute the command normally
cmd.Execute()

// Alternaitvely run the command in interactive mode
// Ctrl+C to exit
cmd.ExecuteInteractive()

Shortcuts

ShortcutOperation
Ctrl+LClear logger pane
Ctrl+OClear output pane
UpPrevious command
DownNext command
TabFocus next suggestion
Shift+TabFocus previous suggestion
EnterExecute command/Select suggestion

TODO

  • We should suggest only the next keyword instead of populating the entire command. This could be useful for long commands. Currently tview only provides the functionality for replacing the text in input field, there is no option to append the auto complete suggestion. A workaround would be to add a completely new input type based on original tview inputfield with better append suggestion support.

Contributions

Contributions are welcome, this was my first experience with golang so a lot of things may not be up to the mark. Feel free to open an issue or raise a pull request.

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements