Skip to content

Commit

Permalink
Merge pull request #30 from gopherguides/home-directory
Browse files Browse the repository at this point in the history
replace tildas with users home directory when executing commands
  • Loading branch information
corylanou authored Sep 13, 2024
2 parents f2f3711 + d0089e6 commit 62b04bf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ func (c *Cmd) Execute(ctx context.Context, doc *Document) error {
cmd.Dir = dir
}

res, err := cmd.Run(ctx, c.Args...)
// Check the args for the ~ character and replace it with the home directory
// Make a copy of those args as we only want to change them when we pass it to the clam.Cmd
args := make([]string, len(c.Args))
copy(args, c.Args)
for i, arg := range args {
if strings.HasPrefix(arg, "~") {
args[i] = filepath.Join(homeDirectory(), arg[1:])
}
}

res, err := cmd.Run(ctx, args...)
if err != nil {
switch c.ExpectedExit {
case -1:
Expand Down
53 changes: 53 additions & 0 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package hype
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -186,3 +188,54 @@ func Test_Cmd_MarshalJSON(t *testing.T) {
testJSON(t, "cmd", c)

}
func Test_Cmd_HomeDirectory(t *testing.T) {
t.Parallel()
r := require.New(t)

homeDir, err := os.UserHomeDir()
r.NoError(err)

// Create a directory in the user's home directory with a random temporary name
dir := filepath.Join(homeDir, "tmp_test_dir")
t.Log("created dir:", dir)
err = os.MkdirAll(dir, 0755)
r.NoError(err)
t.Cleanup(func() {
os.RemoveAll(dir)
})

// Add three files to the directory: a.txt, b.txt, and c.txt
files := []string{"a.txt", "b.txt", "c.txt"}
for _, file := range files {
filePath := filepath.Join(dir, file)
_, err := os.Create(filePath)
r.NoError(err)
}

c := &Cmd{
Element: NewEl("cmd", nil),
Args: []string{"ls", "~/tmp_test_dir"},
}
r.NoError(c.Set("exec", "ls ~/tmp_test_dir"))
r.NoError(c.Set("hide-cmd", ""))

ctx := context.Background()
doc := &Document{
Parser: NewParser(nil),
}

doc.Nodes = append(doc.Nodes, c)
err = doc.Execute(ctx)
r.NoError(err)

r.NotNil(c.Result())

act := c.String()
act = strings.TrimSpace(act)

exp := `<cmd exec="ls ~/tmp_test_dir" hide-cmd=""><pre><code class="language-shell" language="shell">a.txt
b.txt
c.txt</code></pre></cmd>`

r.Equal(exp, act)
}
23 changes: 23 additions & 0 deletions home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hype

import (
"log"
"os"
)

// homeDirectory retursn the home directory of the current user
// it only runs once
var homeDir string

func homeDirectory() string {
if homeDir != "" {
return homeDir
}

hd, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
homeDir = hd
return homeDir
}

0 comments on commit 62b04bf

Please sign in to comment.