Skip to content

Commit

Permalink
Add helper GetCurrentUsername
Browse files Browse the repository at this point in the history
currently we need this to get the username in windows
for mounting the shared directories, which is CIFS/SMB
based and needs the username for access
  • Loading branch information
anjannath committed Oct 11, 2022
1 parent 846d772 commit 34989df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/os/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"

Expand Down Expand Up @@ -37,3 +38,11 @@ func RemoveFileAsRoot(reason, filepath string) error {
_, _, err := RunPrivileged(reason, "rm", "-fr", filepath)
return err
}

func GetCurrentUsername() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
return u.Username, nil
}
15 changes: 15 additions & 0 deletions pkg/os/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package os

import (
"bytes"
"errors"
"io/ioutil"
"os/user"
"strings"

"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
Expand All @@ -28,3 +31,15 @@ func ReadFileUTF16LE(filename string) ([]byte, error) {
decoded, err := ioutil.ReadAll(unicodeReader)
return decoded, err
}

func GetCurrentUsername() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
userAndDomain := strings.Split(u.Username, "\\")
if len(userAndDomain) > 1 {
return userAndDomain[1], nil
}
return "", errors.New("unable to find the username of current user")
}

0 comments on commit 34989df

Please sign in to comment.