Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ See ochami-config(5) for more details on configuring the ochami config file(s).`
rootCmd.PersistentFlags().StringVarP(&cli.ConfigFile, "config", "c", "", "path to configuration file to use")
rootCmd.PersistentFlags().StringP("log-format", "L", "", "log format (json,rfc3339,basic)")
rootCmd.PersistentFlags().StringP("log-level", "l", "", "set verbosity of logs (info,warning,debug)")
rootCmd.PersistentFlags().String("log-color", "", "set coloring of logs (auto,on,off)")
rootCmd.PersistentFlags().StringP("cluster", "C", "", "name of cluster whose config to use for this command")
rootCmd.PersistentFlags().StringP("cluster-uri", "u", "", "base URI for OpenCHAMI services, excluding service base path (overrides cluster.uri in config file)")
rootCmd.PersistentFlags().StringVar(&cli.CACertPath, "cacert", "", "path to root CA certificate in PEM format")
Expand Down
7 changes: 7 additions & 0 deletions doc/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ log:
# basic - Specifies log level but no timestamp data
format: json

# Color output. Specify whether or not to color output. Available values are:
#
# on - Logs are colored unconditionally
# off - Logs are uncolored unconditionally
# auto - Logs are colored if stderr is a terminal (default)
color: auto

# Specify the name of the cluster to use by default. If this is not specified,
# --cluster must be used on the command line to specify the name of the cluster
# to use when communicating with OpenCHAMI services.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/spf13/cobra v1.10.2
github.com/synackd/go-kargs v0.0.1-beta.1
github.com/vbauerster/mpb/v8 v8.10.2
golang.org/x/term v0.44.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
golang.org/x/tools v0.45.0 h1:18qN3FAooORvApf5XjCXgsuayZOEtXf6JK18I3+ONa8=
Expand Down
10 changes: 9 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ func InitLogging(cmd *cobra.Command) error {
config.GlobalConfig.Log.Level = ll
}

if err := log.Init(config.GlobalConfig.Log.Level, config.GlobalConfig.Log.Format); err != nil {
if cmd.Flags().Changed("log-color") {
lc, err := cmd.Flags().GetString("log-color")
if err != nil {
return fmt.Errorf("failed to fetch flag log-color: %w", err)
}
config.GlobalConfig.Log.Color = lc
}

if err := log.Init(config.GlobalConfig.Log.Level, config.GlobalConfig.Log.Format, config.GlobalConfig.Log.Color); err != nil {
return fmt.Errorf("failed to Initialize logger: %w", err)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var DefaultConfig = Config{
Log: ConfigLog{
Format: "rfc3339",
Level: "warning",
Color: "auto",
},
Timeout: 30 * time.Second,
}
Expand Down Expand Up @@ -118,7 +119,7 @@ func (c *Config) UnmarshalYAML(value *yaml.Node) error {
Line: n.Content[i].Line,
}
}
// If key was found and is not empty, set our sentinal
// If key was found and is not empty, set our sentinel
hasTimeout = true
break
}
Expand Down Expand Up @@ -157,6 +158,7 @@ func (c Config) GetCluster(name string) (ConfigCluster, error) {
type ConfigLog struct {
Format string `yaml:"format,omitempty"`
Level string `yaml:"level,omitempty"`
Color string `yaml:"color,omitempty"`
}

// ConfigCluster is a "wrapper" around an individual cluster configuration. It
Expand Down
15 changes: 14 additions & 1 deletion internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/rs/zerolog"
"golang.org/x/term"

"github.com/OpenCHAMI/ochami/internal/version"
)
Expand All @@ -30,7 +31,7 @@ var (

// Init() initializes the global logging object so it can be used for logging by
// any package that imports this internal log package.
func Init(ll, lf string) error {
func Init(ll, lf, lc string) error {
var loggerLevel zerolog.Level
switch ll {
case "warning":
Expand All @@ -44,6 +45,18 @@ func Init(ll, lf string) error {
}

cw := zerolog.ConsoleWriter{Out: os.Stderr}

switch lc {
case "", "auto":
cw.NoColor = !term.IsTerminal(int(os.Stderr.Fd()))
case "on":
cw.NoColor = false
case "off":
cw.NoColor = true
default:
return fmt.Errorf("invalid log-color: %s", lc)
}

switch lf {
case "rfc3339":
cw.TimeFormat = time.RFC3339
Expand Down
16 changes: 15 additions & 1 deletion internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestInit(t *testing.T) {
type args struct {
ll string
lf string
lc string
}
tests := []struct {
name string
Expand All @@ -26,6 +27,7 @@ func TestInit(t *testing.T) {
args: args{
ll: "warning",
lf: "basic",
lc: "auto",
},
wantErr: false,
},
Expand All @@ -34,6 +36,7 @@ func TestInit(t *testing.T) {
args: args{
ll: "unsupported",
lf: "basic",
lc: "auto",
},
wantErr: true,
},
Expand All @@ -42,6 +45,7 @@ func TestInit(t *testing.T) {
args: args{
ll: "warning",
lf: "unsupported",
lc: "auto",
},
wantErr: true,
},
Expand All @@ -50,13 +54,23 @@ func TestInit(t *testing.T) {
args: args{
ll: "unsupported",
lf: "unsupported",
lc: "auto",
},
wantErr: true,
},
{
name: "supported level and format, unsupported color",
args: args{
ll: "warning",
lf: "basic",
lc: "unsupported",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Init(tt.args.ll, tt.args.lf); (err != nil) != tt.wantErr {
if err := Init(tt.args.ll, tt.args.lf, tt.args.lc); (err != nil) != tt.wantErr {
t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
11 changes: 11 additions & 0 deletions man/ochami-config.5.sc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ These configuration options are global configuration options.
- _warning_
- _debug_

*color:* _color_
Enable/disable logging color. *auto* detects whether or not stderr is a
terminal and enables color accordingly.

Default: *auto*
Supported:

- _auto_
- _on_
- _off_

*timeout:* _duration_
The timeout to use for HTTP requests. This is a duration string as accepted
by Go's duration parser (e.g. _30s_, _5m_, _1m30s_).
Expand Down
10 changes: 10 additions & 0 deletions man/ochami.1.sc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ _foobar_.
- _warning_
- _debug_

*--log-color* _color_
Enable/disable logging color. *auto* detects whether or not stderr is a
terminal and enables color accordingly. Defaults to _auto_.

Supported:

- _auto_
- _on_
- _off_

*--no-token*
Disable reading of and checking for access token and do not include any
token in the request headers. This overrides the value of *enable-auth* set
Expand Down
Loading