Skip to content
41 changes: 25 additions & 16 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ package root

import (
"fmt"
"io"
"time"
"log/slog"

"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/configurations"
Expand All @@ -38,16 +37,18 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/cmd/harbor/root/vulnerability"
"github.com/goharbor/harbor-cli/cmd/harbor/root/webhook"
"github.com/goharbor/harbor-cli/pkg/logger"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
output string
cfgFile string
verbose bool
logFormat string
output string
cfgFile string
verbose bool
)

func RootCmd() *cobra.Command {
Expand All @@ -69,23 +70,26 @@ harbor help
// Initialize configuration
utils.InitConfig(cfgFile, userSpecifiedConfig)

// Conditionally set the timestamp format only in verbose mode
formatter := &logrus.TextFormatter{}

if verbose {
formatter.FullTimestamp = true
formatter.TimestampFormat = time.RFC3339
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetOutput(io.Discard)
if logFormat != "json" && logFormat != "text" {
return fmt.Errorf("invalid log-format: %s, log-format can be one of: json|text", logFormat)
}
logrus.SetFormatter(formatter)

// Sets up logging
logger.Setup(verbose, logFormat)

// Logging Flags
arr := make([]any, 0) // slog requires any since the slog.Debug takes in (string, ...any)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
arr = append(arr, f.Name, f.Value.String())
})
slog.Debug("Flags: ", arr...)

return nil
},
}

root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVarP(&logFormat, "log-format", "l", "text", "Output format for logging. One of: json|text")
root.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/harbor-cli/config.yaml)")
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

Expand All @@ -94,6 +98,11 @@ harbor help
fmt.Println(err.Error())
}

err = viper.BindPFlag("log-format", root.PersistentFlags().Lookup("log-format"))
if err != nil {
fmt.Println(err.Error())
}

err = viper.BindPFlag("config", root.PersistentFlags().Lookup("config"))
if err != nil {
fmt.Println(err.Error())
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ harbor artifact delete [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-label-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ harbor artifact label add [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-label-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ harbor artifact label delete [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-label-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ harbor artifact label list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ harbor artifact label del <project>/<repository>/<reference> <label name>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ harbor artifact list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-scan-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ harbor artifact scan start <project>/<repository>/<reference>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-scan-stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ harbor artifact scan stop <project>/<repository>/<reference>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor artifact scan start <project>/<repository>/<reference>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-tags-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor artifact tags create <project>/<repository>/<reference> <tag>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-tags-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor artifact tags delete <project>/<repository>/<reference> <tag>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-tags-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor artifact tags list <project>/<repository>/<reference>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ weight: 85

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ harbor artifact view <project>/<repository>:<tag> OR harbor artifact view <proje

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-artifact.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Manage artifacts in Harbor Repository

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-config-apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ harbor config apply -f <config_file>

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-config-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ harbor config view [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Categories available:

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ harbor context delete <item> [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ harbor context get <item> [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor context list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context-switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor context switch harbor-cli@https-demo-goharbor-io

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ harbor context update <item> <value> [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ harbor context list

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-cve-allowlist-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ harbor cve-allowlist add [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-cve-allowlist-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ harbor cve-allowlist list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-cve-allowlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor cve-allowlist list

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-health.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ harbor health [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ harbor info [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-instance-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ harbor instance create [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-instance-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ harbor instance delete [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-instance-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ harbor instance list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ These instances represent external services such as Dragonfly or Kraken that hel

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-label-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ harbor label create

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-label-delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ harbor label delete [labelname]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-label-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ harbor label list [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-label-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ harbor label update [labelname]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ weight: 65

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
1 change: 1 addition & 0 deletions doc/cli-docs/harbor-ldap-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ harbor ldap import [userID] [flags]

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-l, --log-format string Output format for logging. One of: json|text (default "text")
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```
Expand Down
Loading
Loading