Skip to content

Commit

Permalink
Merge pull request #200 from fujiwara/color
Browse files Browse the repository at this point in the history
Enable colored output.
  • Loading branch information
fujiwara authored Dec 10, 2021
2 parents 687e426 + b6a3732 commit b37d9ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
18 changes: 16 additions & 2 deletions cmd/lambroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"os"

"github.com/alecthomas/kingpin"
"github.com/fatih/color"
"github.com/fujiwara/lambroll"
"github.com/hashicorp/logutils"
"github.com/fujiwara/logutils"
"github.com/mattn/go-isatty"
)

// Version number
Expand All @@ -21,6 +23,11 @@ func _main() int {
kingpin.Command("version", "show version")
logLevel := kingpin.Flag("log-level", "log level (trace, debug, info, warn, error)").Default("info").Enum("trace", "debug", "info", "warn", "error")
function := kingpin.Flag("function", "Function file path").Default(lambroll.FunctionFilename).String()
colorDefault := "false"
if isatty.IsTerminal(os.Stdout.Fd()) {
colorDefault = "true"
}
colorOpt := kingpin.Flag("color", "enable colored output").Default(colorDefault).Bool()

opt := lambroll.Option{
Profile: kingpin.Flag("profile", "AWS credential profile name").Default(os.Getenv("AWS_PROFILE")).String(),
Expand Down Expand Up @@ -108,9 +115,16 @@ func _main() int {
fmt.Println("lambroll", Version)
return 0
}
color.NoColor = !*colorOpt

filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"trace", "debug", "info", "warn", "error"},
Levels: []logutils.LogLevel{"debug", "info", "warn", "error"},
ModifierFuncs: []logutils.ModifierFunc{
logutils.Color(color.FgHiBlack), // debug
nil, // info
logutils.Color(color.FgYellow), // warn
logutils.Color(color.FgRed), // error
},
MinLevel: logutils.LogLevel(*logLevel),
Writer: os.Stderr,
}
Expand Down
23 changes: 20 additions & 3 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package lambroll

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/service/lambda"
"github.com/fatih/color"
"github.com/kylelemons/godebug/diff"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -40,9 +43,23 @@ func (app *App) Diff(opt DiffOption) error {
newJSON, _ := marshalJSON(newFunc)

if ds := diff.Diff(string(latestJSON), string(newJSON)); ds != "" {
fmt.Println("---", app.functionArn(name))
fmt.Println("+++", *opt.FunctionFilePath)
fmt.Println(ds)
fmt.Println(color.RedString("---", app.functionArn(name)))
fmt.Println(color.GreenString("+++", *opt.FunctionFilePath))
fmt.Println(coloredDiff(ds))
}
return nil
}

func coloredDiff(src string) string {
var b strings.Builder
for _, line := range strings.Split(src, "\n") {
if strings.HasPrefix(line, "-") {
b.WriteString(color.RedString(line) + "\n")
} else if strings.HasPrefix(line, "+") {
b.WriteString(color.GreenString(line) + "\n")
} else {
b.WriteString(line + "\n")
}
}
return b.String()
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ require (
github.com/Songmu/prompter v0.5.0
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/aws/aws-sdk-go v1.40.52
github.com/fatih/color v1.13.0
github.com/fujiwara/logutils v1.1.0
github.com/fujiwara/tfstate-lookup v0.4.0
github.com/go-test/deep v1.0.7
github.com/google/go-jsonnet v0.17.0
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
github.com/hashicorp/logutils v1.0.0
github.com/kayac/go-config v0.6.0
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-isatty v0.0.14
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fujiwara/logutils v1.1.0 h1:JAYmqW40d/ZjzouB01sfZiaTxwNe4hwmB6lLajZqm1s=
github.com/fujiwara/logutils v1.1.0/go.mod h1:pdb/Uk70rjQWEmFm/OvYH7OG8meZt1fEIqC0qZbvro4=
github.com/fujiwara/tfstate-lookup v0.4.0 h1:AaubXvolethCndWpZ+c/Sibm6J4SCIlSxcxvn6cYon4=
github.com/fujiwara/tfstate-lookup v0.4.0/go.mod h1:F56Bz3W497ZJA0ijFLVgcYeAVuhtbzvV6/hMtu6f2Ek=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -211,8 +215,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/jsonapi v0.0.0-20210518035559-1e50d74c8db3 h1:mzwkutymYIXR5oQT9YnfbLuuw7LZmksiHKRPUTN5ijo=
github.com/hashicorp/jsonapi v0.0.0-20210518035559-1e50d74c8db3/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -245,6 +247,8 @@ github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopu
github.com/lestrrat-go/strftime v1.0.1 h1:o7qz5pmLzPDLyGW4lG6JvTKPUfTFXwe+vOamIYWtnVU=
github.com/lestrrat-go/strftime v1.0.1/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-ieproxy v0.0.1 h1:qiyop7gCflfhwCzGyeT0gro3sF9AIg9HU98JORTkqfI=
github.com/mattn/go-ieproxy v0.0.1/go.mod h1:pYabZ6IHcRpFh7vIaLfK7rdcWgFEb3SFJ6/gNWuh88E=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
Expand Down

0 comments on commit b37d9ba

Please sign in to comment.