From 81246484025946a81c9ccc308836653c5b249f59 Mon Sep 17 00:00:00 2001 From: Roman Leonenkov <6890447+grafviktor@users.noreply.github.com> Date: Sun, 19 Nov 2023 01:17:51 +0000 Subject: [PATCH] IMPROVEMENT-12: Refactor and fix a couple minor bugs --- cmd/goto/main.go | 2 +- internal/constant/constant.go | 5 ++- internal/ui/component/edithost/edit_host.go | 2 +- internal/utils/utils.go | 40 +++++++++++++++------ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/cmd/goto/main.go b/cmd/goto/main.go index 8ee717e..f4ddd4b 100644 --- a/cmd/goto/main.go +++ b/cmd/goto/main.go @@ -46,7 +46,7 @@ func main() { flag.Parse() var err error - commandLineParams.AppHome, err = utils.GetAppDir(appName, commandLineParams.AppHome) + commandLineParams.AppHome, err = utils.AppDir(appName, commandLineParams.AppHome) if err != nil { log.Fatalf("Can't get application home folder: %v", err) } diff --git a/internal/constant/constant.go b/internal/constant/constant.go index 6588a51..7151764 100644 --- a/internal/constant/constant.go +++ b/internal/constant/constant.go @@ -2,7 +2,10 @@ package constant import "errors" -var ErrNotFound = errors.New("not found") +var ( + ErrNotFound = errors.New("not found") + ErrBadArgument = errors.New("bad argument") +) // ErrDuplicateRecord = errors.New("duplicate record") // ErrDeleted = errors.New("deleted") diff --git a/internal/ui/component/edithost/edit_host.go b/internal/ui/component/edithost/edit_host.go index 57c6887..c9e588a 100644 --- a/internal/ui/component/edithost/edit_host.go +++ b/internal/ui/component/edithost/edit_host.go @@ -77,7 +77,7 @@ func New(ctx context.Context, storage storage.HostStorage, state *state.Applicat case 3: t.Label = "Login" t.CharLimit = 128 - t.Placeholder = fmt.Sprintf("default: %s", utils.GetCurrentOSUser()) + t.Placeholder = fmt.Sprintf("default: %s", utils.CurrentOSUsername()) t.SetValue(host.LoginName) case 4: t.Label = "Port" diff --git a/internal/utils/utils.go b/internal/utils/utils.go index fc93b8c..28937dd 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -6,29 +6,45 @@ import ( "os/user" "path" "path/filepath" + "strings" + + "github.com/grafviktor/goto/internal/constant" ) type Logger interface { Debug(format string, args ...any) } +func stringEmpty(s string) bool { + return len(strings.TrimSpace(s)) == 0 +} + func CreateAppDirIfNotExists(appConfigDir string) error { - _, err := os.Stat(appConfigDir) + if stringEmpty(appConfigDir) { + return constant.ErrBadArgument + } + stat, err := os.Stat(appConfigDir) if os.IsNotExist(err) { - err = os.MkdirAll(appConfigDir, 0o700) - if err != nil { - return err - } + return os.MkdirAll(appConfigDir, 0o700) } else if err != nil { return err } + if !stat.IsDir() { + return errors.New("app home path exists and it is not a directory") + } + return nil } -func GetAppDir(appName, userDefinedPath string) (string, error) { - if len(userDefinedPath) > 0 { +// AppDir - returns application home folder where all all files are stored. +// appName is application name which will be used as folder name. +// userDefinedPath allows you to set a custom path to application home folder, can be relative or absolute. +// If userDefinedPath is not empty, it will be used as application home folder +// Else, userConfigDir will be used, which is system dependent. +func AppDir(appName, userDefinedPath string) (string, error) { + if !stringEmpty(userDefinedPath) { absolutePath, err := filepath.Abs(userDefinedPath) if err != nil { return "", err @@ -46,9 +62,12 @@ func GetAppDir(appName, userDefinedPath string) (string, error) { return absolutePath, nil } - // FIXME: -------- DEBUG ------------- / + if stringEmpty(appName) { + return "", errors.New("application home folder name is not provided") + } + + // Left for debugging purposes // userConfigDir, err := os.Getwd() - // -------- RELEASE ----------- / userConfigDir, err := os.UserConfigDir() if err != nil { return "", err @@ -57,7 +76,8 @@ func GetAppDir(appName, userDefinedPath string) (string, error) { return path.Join(userConfigDir, appName), nil } -func GetCurrentOSUser() string { +// CurrentOSUsername - returns current OS username or "n/a" if it can't be determined. +func CurrentOSUsername() string { user, err := user.Current() if err != nil { return "n/a"