From 49f0c0e149faf0fc100069bb9f45b3541ce502c1 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Tue, 12 Jan 2021 15:21:36 -0800 Subject: [PATCH] cmd: re-use existing ui library Also, move CLI-only utils into `cmd` rather than the top-level package. Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/compute.go | 6 +- cmd/dagger/cmd/root.go | 4 +- cmd/dagger/ui/ui.go | 144 ++++++++++++++++++++++++++++++++++++++ dagger/ui/ui.go | 26 ------- go.mod | 1 + go.sum | 2 + 6 files changed, 152 insertions(+), 31 deletions(-) create mode 100644 cmd/dagger/ui/ui.go delete mode 100644 dagger/ui/ui.go diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index 817a3b24..3a617e72 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -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()) diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 23a763aa..551f9714 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -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) } } diff --git a/cmd/dagger/ui/ui.go b/cmd/dagger/ui/ui.go new file mode 100644 index 00000000..382b920a --- /dev/null +++ b/cmd/dagger/ui/ui.go @@ -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")) +} diff --git a/dagger/ui/ui.go b/dagger/ui/ui.go deleted file mode 100644 index 159afe75..00000000 --- a/dagger/ui/ui.go +++ /dev/null @@ -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...) -} diff --git a/go.mod b/go.mod index 969b478d..65ddf13c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0580f8a1..3015c2ac 100644 --- a/go.sum +++ b/go.sum @@ -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=