Skip to content

Commit c903ffb

Browse files
authored
feat: logging enhancements (#257)
1 parent 8a1898b commit c903ffb

File tree

15 files changed

+78
-31
lines changed

15 files changed

+78
-31
lines changed

.execs/test.flow

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,15 @@ executables:
139139
envKey: "COUNT"
140140
type: "int"
141141
default: "1"
142+
143+
- verb: test
144+
name: colored-output
145+
visibility: internal
146+
description: Test executable to demonstrate colored output in the terminal.
147+
exec:
148+
dir: //
149+
cmd: |
150+
echo -e "\033[32mThis is green text\033[0m"
151+
echo -e "\033[31mThis is red text\033[0m"
152+
echo -e "\033[34mThis is blue text\033[0m"
153+
echo -e "\033[33mThis is yellow text\033[0m"

cmd/internal/exec.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/gen2brain/beeep"
11+
tuikitIO "github.com/jahvon/tuikit/io"
1112
"github.com/jahvon/tuikit/views"
1213
"github.com/spf13/cobra"
1314

@@ -55,6 +56,10 @@ func RegisterExecCmd(ctx *context.Context, rootCmd *cobra.Command) {
5556
return execIDs, cobra.ShellCompDirectiveNoFileComp
5657
},
5758
PreRun: func(cmd *cobra.Command, args []string) {
59+
logMode := flags.ValueFor[string](ctx, cmd, *flags.LogModeFlag, false)
60+
if err := tuikitIO.LogMode(logMode).Validate(); err != nil {
61+
ctx.Logger.FatalErr(err)
62+
}
5863
execPreRun(ctx, cmd, args)
5964
},
6065
Run: func(cmd *cobra.Command, args []string) {
@@ -64,6 +69,7 @@ func RegisterExecCmd(ctx *context.Context, rootCmd *cobra.Command) {
6469
},
6570
}
6671
RegisterFlag(ctx, subCmd, *flags.ParameterValueFlag)
72+
RegisterFlag(ctx, subCmd, *flags.LogModeFlag)
6773
rootCmd.AddCommand(subCmd)
6874
}
6975

@@ -81,6 +87,11 @@ func execPreRun(_ *context.Context, _ *cobra.Command, _ []string) {
8187
//nolint:funlen,gocognit
8288
func execFunc(ctx *context.Context, cmd *cobra.Command, verb executable.Verb, args []string) {
8389
logger := ctx.Logger
90+
logMode := flags.ValueFor[string](ctx, cmd, *flags.LogModeFlag, false)
91+
if logMode != "" {
92+
logger.SetMode(tuikitIO.LogMode(logMode))
93+
}
94+
8495
if err := verb.Validate(); err != nil {
8596
logger.FatalErr(err)
8697
}

cmd/internal/flags/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ var LogLevel = &Metadata{
2323
Required: false,
2424
}
2525

26+
var LogModeFlag = &Metadata{
27+
Name: "log-mode",
28+
Shorthand: "m",
29+
Usage: "Log mode (text, logfmt, json, hidden)",
30+
Default: "logfmt",
31+
Required: false,
32+
}
33+
2634
var SyncCacheFlag = &Metadata{
2735
Name: "sync",
2836
Usage: "Sync flow cache and workspaces",

docs/cli/flow_exec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ flow exec EXECUTABLE_ID [args...] [flags]
5151

5252
```
5353
-h, --help help for exec
54+
-m, --log-mode string Log mode (text, logfmt, json, hidden) (default "logfmt")
5455
-p, --param stringArray Set a parameter value by env key. (i.e. KEY=value) Use multiple times to set multiple parameters.This will override any existing parameter values defined for the executable.
5556
```
5657

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/gen2brain/beeep v0.11.1
1313
github.com/jahvon/glamour v0.8.1-patch3
1414
github.com/jahvon/open-golang v0.0.0-20240522004812-68511c3bc9ef
15-
github.com/jahvon/tuikit v0.1.2
15+
github.com/jahvon/tuikit v0.1.4
1616
github.com/jahvon/vault v0.1.1
1717
github.com/mattn/go-runewidth v0.0.16
1818
github.com/onsi/ginkgo/v2 v2.23.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ github.com/jahvon/glamour v0.8.1-patch3 h1:LfyMACZavV8yxK4UsPENNQQOqafWuq4ZdLuEA
115115
github.com/jahvon/glamour v0.8.1-patch3/go.mod h1:30MVJwG3rcEHrN277NrA4DKzndSL9lBtEmpcfOygwCQ=
116116
github.com/jahvon/open-golang v0.0.0-20240522004812-68511c3bc9ef h1:4PS/MNVT6Rsv15x5Rtwaw971e6kFvNUAf9nvUsZ5hcc=
117117
github.com/jahvon/open-golang v0.0.0-20240522004812-68511c3bc9ef/go.mod h1:dUmuT5CN6osIeLSRtTPJOf0Yz+qAbcyU6omnCzI+ZfQ=
118-
github.com/jahvon/tuikit v0.1.2 h1:BGN942W8S89d+hkKxMsUUB50+63T1VgkBamsKwzXpLc=
119-
github.com/jahvon/tuikit v0.1.2/go.mod h1:Bi6zph90IAONRweA/9eUKjVNaxCstxhwubnhFOthEFs=
118+
github.com/jahvon/tuikit v0.1.4 h1:hdIMwyZRYYVeEkzI2cPDnMcy1RKdTTpQG/nvIJ2K2bc=
119+
github.com/jahvon/tuikit v0.1.4/go.mod h1:s9fRhgO1IlMLzkY/KGikBrqGm41Fn0QOQNHLwQvrDbE=
120120
github.com/jahvon/vault v0.1.1 h1:qzfRh/+yoRR44MhfmiVyMIXAalKPNN+DRgeTQ1frC9A=
121121
github.com/jahvon/vault v0.1.1/go.mod h1:1StJR/E0WSXPKsgMs1t68N0g3gjIOwSo97Zsb56Ll28=
122122
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

internal/io/vault/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (vc *vaultCollection) Plural() string {
8282
}
8383

8484
func (vc *vaultCollection) Items() []*types.EntityInfo {
85-
items := make([]*types.EntityInfo, 0, len(vc.Vaults))
85+
items := make([]*types.EntityInfo, len(vc.Vaults))
8686
for i, v := range vc.Vaults {
8787
items[i] = &types.EntityInfo{
8888
Header: v.Name,

internal/runner/parallel/parallel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func handleExec(
123123

124124
switch {
125125
case exec.Exec != nil:
126-
fields := map[string]interface{}{"step": exec.ID()}
126+
fields := map[string]interface{}{"step": exec.Ref().String()}
127127
exec.Exec.SetLogFields(fields)
128128
if parallelSpec.Dir != "" && exec.Exec.Dir == "" {
129129
exec.Exec.Dir = parallelSpec.Dir

internal/runner/serial/serial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func handleExec(
118118

119119
switch {
120120
case exec.Exec != nil:
121-
fields := map[string]interface{}{"step": exec.ID()}
121+
fields := map[string]interface{}{"step": exec.Ref().String()}
122122
exec.Exec.SetLogFields(fields)
123123
if serialSpec.Dir != "" && exec.Exec.Dir == "" {
124124
exec.Exec.Dir = serialSpec.Dir

internal/services/run/run.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"github.com/jahvon/tuikit/io"
1112
"mvdan.cc/sh/v3/expand"
1213
"mvdan.cc/sh/v3/interp"
1314
"mvdan.cc/sh/v3/syntax"
14-
15-
"github.com/jahvon/tuikit/io"
1615
)
1716

17+
func init() {
18+
if _, exists := os.LookupEnv("TERM"); !exists {
19+
_ = os.Setenv("TERM", "xterm-256color")
20+
}
21+
if _, exists := os.LookupEnv("FORCE_COLOR"); !exists {
22+
_ = os.Setenv("FORCE_COLOR", "1")
23+
}
24+
if _, exists := os.LookupEnv("CLICOLOR_FORCE"); !exists {
25+
_ = os.Setenv("CLICOLOR_FORCE", "1")
26+
}
27+
}
28+
1829
// RunCmd executes a command in the current shell in a specific directory.
1930
func RunCmd(
2031
commandStr, dir string,

0 commit comments

Comments
 (0)