cobi

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: MIT Imports: 9 Imported by: 2

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

Shortcut Operation
Ctrl+L Clear logger pane
Ctrl+O Clear output pane
Up Previous command
Down Next command
Tab Focus next suggestion
Shift+Tab Focus previous suggestion
Enter Execute 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	*cobra.Command
	App    *tview.Application
	Editor *editor.Editor
	// contains filtered or unexported fields
}

Command is a modified struct to handle interactive sessions combined with tview & cobra.

func NewCommand

func NewCommand(editor *editor.Editor, cmd *cobra.Command) *Command

NewCommand returns an instance of cobi command

func (*Command) ExecuteInteractive

func (co *Command) ExecuteInteractive() error

ExecuteInteractive will start the command in interactive mode

func (*Command) InteractiveMode

func (co *Command) InteractiveMode() bool

InteractiveMode returns true if the commands are running in interactive mode

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL