on -- A CLI tool helps you work on certain context
Tags: go, showdev, opensource
Published: 2019-03-30 03:14:44

---
title: on -- A CLI tool helps you work on certain context
published: true
description: 
tags: go,showdev,opensource
---

[on](https://github.com/dannypsnl/on) is created for reducing typing

When I'm working with `kubectl`, at the most time I would use `kubectl get pod` to get all pods then use `kubectl exec -ti <pod-name> bash` into the pod to debugging/testing our Router, with it all the days I found it's pretty repeated, and as a software engineer, we hate repeating. So I create a command line tool could execute the command that bases on existing context, that's `on`. But I just mention `kubectl`, why I create a general CLI? Because I also have to work with other commands such as `go`, `neutron`, `docker` work in the same pattern.

After the background introduction, let's see how it work like:
```
$ on go
on(go)> test
PASS
ok  	github.com/dannypsnl/rocket	0.031s
on(go)> test ./...
ok  	github.com/dannypsnl/rocket	0.033s
ok  	github.com/dannypsnl/rocket/cookie	0.024s
ok  	github.com/dannypsnl/rocket/fairing	0.018s
?   	github.com/dannypsnl/rocket/internal/context	[no test files]
?   	github.com/dannypsnl/rocket/internal/filepath	[no test files]
ok  	github.com/dannypsnl/rocket/response	0.024s
ok  	github.com/dannypsnl/rocket/router	0.013s
on(go)> test ./... -count 1 -cover -failfast
ok  	github.com/dannypsnl/rocket	0.041s	coverage: 86.5% of statements
ok  	github.com/dannypsnl/rocket/cookie	0.024s	coverage: 100.0% of statements
ok  	github.com/dannypsnl/rocket/fairing	0.022s	coverage: 100.0% of statements
?   	github.com/dannypsnl/rocket/internal/context	[no test files]
?   	github.com/dannypsnl/rocket/internal/filepath	[no test files]
ok  	github.com/dannypsnl/rocket/response	0.032s	coverage: 94.1% of statements
ok  	github.com/dannypsnl/rocket/router	0.015s	coverage: 95.3% of statements
on(go)>
         test
         test ./...
         test ./... -count 1 -cover -failfast
```

You can see the hint would appear in the prompt box, you could use `tab` to select from them.

And you can append new context on to current context by type `Ctrl+a` then type the append part, e.g.

```
on(go)> test
on(go test)> ./...
ok  	github.com/dannypsnl/rocket	(cached)
ok  	github.com/dannypsnl/rocket/cookie	(cached)
ok  	github.com/dannypsnl/rocket/fairing	(cached)
?   	github.com/dannypsnl/rocket/internal/context	[no test files]
?   	github.com/dannypsnl/rocket/internal/filepath	[no test files]
ok  	github.com/dannypsnl/rocket/response	(cached)
ok  	github.com/dannypsnl/rocket/router	(cached)
on(go test)>
ok  	github.com/dannypsnl/rocket	(cached)
on(go test)
         ./...

```

As you see, the prompt would change for a different context. Then you can use `Ctrl+c` to delete the last element from the context to back. And the prompt would change back too, e.g.

```
on(go test)>
on(go)>
         test
         test ./...
         test ./... -count 1 -cover -failfast
```

Everything looks good, but not just that, `on` won't try to store a failed command into prompts, so it won't be too annoying.

And `on` could let you run a shell inside of it, e.g.

```
$ on kubectl
on(kubectl)> get po
NAME                             READY   STATUS    RESTARTS   AGE
alpine-deploy-5694b87b56-2gscz   1/1     Running   0          11h
alpine-deploy-5694b87b56-xnwl2   1/1     Running   0          11h
on(kubectl)> exec -ti alpine-deploy-5694b87b56-2gscz bash
bash-4.4# ls -la
total 64
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 .
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 ..
-rwxr-xr-x    1 root     root             0 Mar 29 16:11 .dockerenv
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 bin
drwxr-xr-x    5 root     root           360 Mar 29 16:11 dev
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 etc
drwxr-xr-x    2 root     root          4096 Mar  4 15:43 home
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 lib
drwxr-xr-x    5 root     root          4096 Mar  4 15:43 media
drwxr-xr-x    2 root     root          4096 Mar  4 15:43 mnt
drwxr-xr-x    2 root     root          4096 Mar  4 15:43 opt
dr-xr-xr-x  224 root     root             0 Mar 29 16:11 proc
drwx------    1 root     root          4096 Mar 29 16:11 root
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 run
drwxr-xr-x    2 root     root          4096 Mar  4 15:43 sbin
drwxr-xr-x    2 root     root          4096 Mar  4 15:43 srv
dr-xr-xr-x   13 root     root             0 Mar 29 16:09 sys
drwxrwxrwt    2 root     root          4096 Mar  4 15:43 tmp
drwxr-xr-x    1 root     root          4096 Mar 29 16:11 usr
drwxr-xr-x    1 root     root          4096 Mar  4 15:43 var
bash-4.4# exit
exit
on(kubectl)>
```

After you have done your job, just typed `Ctrl-d` then you could leave `on`

Thanks for taking a look, and I only test the command in MacOS with iTerm2, so I would like to know the result on others platform :)