forked from grafana/tanka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): diff UX enhancements (grafana#34)
* feat(cli): use pager for long outputs The outputs of eval, show and diff can be pretty overwhelming. Tanka now uses the systems pager to aid this. When finding no pager or running in non-interactive mode, plain fmt.Println is used. * feat(cli): exit status on diff Exits with `16` in case of differences * feat(cli): diff before apply Shows diff before applying
- Loading branch information
Showing
7 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/alecthomas/chroma/quick" | ||
) | ||
|
||
// pageln invokes the systems pager with the supplied data | ||
// falls back to fmt.Println() when paging fails or non-interactive | ||
func pageln(i ...interface{}) { | ||
// no paging in non-interactive mode | ||
if !interactive { | ||
fmt.Println(i...) | ||
return | ||
} | ||
|
||
// get system pager, fallback to `more` | ||
pager := os.Getenv("PAGER") | ||
if pager == "" { | ||
pager = "more" | ||
} | ||
|
||
// invoke pager | ||
cmd := exec.Command(pager) | ||
cmd.Stdin = strings.NewReader(fmt.Sprintln(i...)) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
// if this fails, just print it | ||
if err := cmd.Run(); err != nil { | ||
fmt.Println(i...) | ||
} | ||
} | ||
|
||
func highlight(lang, s string) string { | ||
var buf bytes.Buffer | ||
if err := quick.Highlight(&buf, s, lang, "terminal", "vim"); err != nil { | ||
log.Fatalln("Highlighting:", err) | ||
} | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters