Skip to content

Commit

Permalink
IMPROVEMENT-12: Refactor and fix a couple minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
grafviktor committed Nov 19, 2023
1 parent 5346d8b commit 8124648
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/goto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/component/edithost/edit_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 30 additions & 10 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 8124648

Please sign in to comment.