cmd: re-use existing ui library

Also, move CLI-only utils into `cmd` rather than the top-level package.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-01-12 15:21:36 -08:00
parent bbdd2b394a
commit 49f0c0e149
6 changed files with 152 additions and 31 deletions

View File

@ -4,8 +4,8 @@ import (
"context"
"fmt"
"dagger.cloud/go/cmd/dagger/ui"
"dagger.cloud/go/dagger"
"dagger.cloud/go/dagger/ui"
"github.com/spf13/cobra"
)
@ -18,14 +18,14 @@ var computeCmd = &cobra.Command{
// FIXME: boot and bootdir should be config fields, not args
c, err := dagger.NewClient(ctx, "", "", args[0])
if err != nil {
ui.Fatal(err)
ui.FatalErr(err)
}
// FIXME: configure which config to compute (duh)
// FIXME: configure inputs
ui.Info("Running")
output, err := c.Compute(ctx)
if err != nil {
ui.Fatal(err)
ui.FatalErr(err)
}
ui.Info("Processing output")
fmt.Println(output.JSON())

View File

@ -1,7 +1,7 @@
package cmd
import (
"dagger.cloud/go/dagger/ui"
"dagger.cloud/go/cmd/dagger/ui"
"github.com/spf13/cobra"
)
@ -30,6 +30,6 @@ func init() {
func Execute() {
if err := rootCmd.Execute(); err != nil {
ui.Fatal(err)
ui.FatalErr(err)
}
}

144
cmd/dagger/ui/ui.go Normal file
View File

@ -0,0 +1,144 @@
package ui
import (
"fmt"
"hash/adler32"
"io"
"os"
"unicode/utf8"
"github.com/mitchellh/colorstring"
)
var (
// Colorize is the main colorizer
Colorize = colorstring.Colorize{
Colors: colorstring.DefaultColors,
Reset: true,
}
)
// Finfo prints an info message to w.
func Finfo(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(w, Colorize.Color("[bold][blue]info[reset] %s\n"), fmt.Sprintf(msg, args...))
}
// Info prints an info message.
func Info(msg string, args ...interface{}) {
Finfo(os.Stdout, msg, args...)
}
// Fverbose prints a verbose message to w.
func Fverbose(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(w, Colorize.Color("[dim]%s\n"), fmt.Sprintf(msg, args...))
}
// Verbose prints a verbose message.
func Verbose(msg string, args ...interface{}) {
Fverbose(os.Stdout, msg, args...)
}
// Fsuccess prints a success message to w.
func Fsuccess(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(w, Colorize.Color("[bold][green]success[reset] %s\n"), fmt.Sprintf(msg, args...))
}
// Success prints a success message.
func Success(msg string, args ...interface{}) {
Fsuccess(os.Stdout, msg, args...)
}
// Fwrror prints an error message to w.
func Ferror(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(w, Colorize.Color("[bold][red]error[reset] %s\n"), fmt.Sprintf(msg, args...))
}
// Error prints an error message.
func Error(msg string, args ...interface{}) {
Ferror(os.Stdout, msg, args...)
}
// Fwarning prints a warning message to w.
func Fwarning(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, Colorize.Color("[bold][yellow]warning[reset] %s\n"), fmt.Sprintf(msg, args...))
}
// Warning prints a warning message.
func Warning(msg string, args ...interface{}) {
Fwarning(os.Stdout, msg, args...)
}
// Ffatal prints an error message to w and exits.
func Ffatal(w io.Writer, msg string, args ...interface{}) {
fmt.Fprintf(w, Colorize.Color("[bold][red]fatal[reset] %s\n"), fmt.Sprintf(msg, args...))
os.Exit(1)
}
// Fatal prints an error message and exits.
func Fatal(msg string, args ...interface{}) {
Ffatal(os.Stderr, msg, args...)
}
// FfatalErr prints an error object to w and exits.
func FfatalErr(w io.Writer, err error) {
Ffatal(w, "%v", err)
}
// FatalErr prints an error object and exits.
func FatalErr(err error) {
FfatalErr(os.Stderr, err)
}
// Small returns a `small` colored string.
func Small(msg string) string {
return Colorize.Color("[dim]" + msg)
}
// Primary returns a `primary` colored string.
func Primary(msg string) string {
return Colorize.Color("[light_green]" + msg)
}
// Highlight returns a `highlighted` colored string.
func Highlight(msg string) string {
return Colorize.Color("[cyan]" + msg)
}
// Truncate truncates a string to the given length
func Truncate(msg string, length int) string {
for utf8.RuneCountInString(msg) > length {
msg = msg[0:len(msg)-4] + "…"
}
return msg
}
// HashColor returns a consistent color for a given string
func HashColor(text string) string {
colors := []string{
"green",
"light_green",
"light_blue",
"blue",
"magenta",
"light_magenta",
"light_yellow",
"cyan",
"light_cyan",
"red",
"light_red",
}
h := adler32.Checksum([]byte(text))
return colors[int(h)%len(colors)]
}
// PrintLegend prints a demo of the ui functions
func PrintLegend() {
Info("info message")
Success("success message")
Error("error message")
Warning("warning message")
Verbose("verbose message")
fmt.Printf("this is %s\n", Small("small"))
fmt.Printf("this is %s\n", Primary("primary"))
fmt.Printf("this is a %s\n", Highlight("highlight"))
}

View File

@ -1,26 +0,0 @@
package ui
import (
"fmt"
"os"
"strings"
)
func Fatalf(msg string, args ...interface{}) {
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
func Fatal(msg interface{}) {
Fatalf("%s\n", msg)
}
func Info(msg string, args ...interface{}) {
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprintf(os.Stderr, "[info] "+msg, args...)
}

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/KromDaniel/jonson v0.0.0-20180630143114-d2f9c3c389db
github.com/containerd/console v1.0.1
github.com/emicklei/proto v1.9.0 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/moby/buildkit v0.8.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0

2
go.sum
View File

@ -615,6 +615,8 @@ github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88J
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=