From 98be079310991cdcc0c957c7cd2527733a9c14d0 Mon Sep 17 00:00:00 2001 From: "L.Dongming" Date: Tue, 14 Nov 2023 10:15:18 +0800 Subject: [PATCH] chore: add logs dir to store kbcli log --- pkg/cmd/cli.go | 1 - pkg/types/types.go | 3 +++ pkg/util/log.go | 2 +- pkg/util/util.go | 15 +++++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index d7ef0c4a1..fab7047ec 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -21,7 +21,6 @@ package cmd import ( "fmt" - "os" "strings" diff --git a/pkg/types/types.go b/pkg/types/types.go index 9769ad7e9..694fa7f9d 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -42,6 +42,9 @@ const ( // CliChartsCache defines kbcli charts cache dir name CliChartsCache = "charts" + // CliLogDir defines kbcli log dir name + CliLogDir = "logs" + // CliHomeEnv defines kbcli home system env CliHomeEnv = "KBCLI_HOME" diff --git a/pkg/util/log.go b/pkg/util/log.go index 8d3501519..57022243f 100644 --- a/pkg/util/log.go +++ b/pkg/util/log.go @@ -64,7 +64,7 @@ func EnableLogToFile(fs *pflag.FlagSet) error { } func getCliLogFile() (string, error) { - homeDir, err := GetCliHomeDir() + homeDir, err := GetCliLogDir() if err != nil { return "", err } diff --git a/pkg/util/util.go b/pkg/util/util.go index e2b209b33..29a47626c 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -106,6 +106,21 @@ func GetCliHomeDir() (string, error) { return cliHome, nil } +// GetCliLogDir returns kbcli log dir +func GetCliLogDir() (string, error) { + cliHome, err := GetCliHomeDir() + if err != nil { + return "", err + } + logDir := filepath.Join(cliHome, types.CliLogDir) + if _, err := os.Stat(logDir); err != nil && os.IsNotExist(err) { + if err = os.MkdirAll(logDir, 0750); err != nil { + return "", errors.Wrap(err, "error when create kbcli log directory") + } + } + return logDir, nil +} + // GetKubeconfigDir returns the kubeconfig directory. func GetKubeconfigDir() string { var kubeconfigDir string