Skip to content

Commit

Permalink
cmds/core/du: add usage error
Browse files Browse the repository at this point in the history
Signed-off-by: Siarhiej Siemianczuk <[email protected]>
  • Loading branch information
binjip978 committed Aug 29, 2024
1 parent abc8b89 commit 5b63e04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmds/core/du/du.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
"syscall"
)

var errNoStatInfo = errors.New("os.FileInfo has no stat_t info")
var (
errNoStatInfo = errors.New("os.FileInfo has no stat_t info")
errUsage = errors.New("usage: du [-k] [-a | -s] [file ...]")
)

type cmd struct {
stdout io.Writer
Expand All @@ -37,6 +40,10 @@ func command(stdout io.Writer, reportFiles, kbUnit, totalSum bool) *cmd {
}

func (c *cmd) run(files ...string) error {
if c.totalSum && c.reportFiles {
return errUsage
}

if len(files) == 0 {
files = append(files, ".")
}
Expand Down Expand Up @@ -102,6 +109,10 @@ func main() {
var totalSum = flag.Bool("s", false, "report only the total sum for each of the specified files")
flag.Parse()
if err := command(os.Stdout, *reportFiles, *kbUnit, *totalSum).run(flag.Args()...); err != nil {
if errors.Is(err, errUsage) {
fmt.Fprintln(os.Stderr, errUsage)
os.Exit(1)
}
log.Fatalf("du: %v", err)
}
}
6 changes: 6 additions & 0 deletions cmds/core/du/du_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ func TestRun(t *testing.T) {
t.Errorf("expected one line per file with -s flag, got %d", len(lines))
}
})
t.Run("both -s and -a", func(t *testing.T) {
err := command(io.Discard, true, false, true).run("")
if err == nil {
t.Errorf("expected %v, got %v", errUsage, err)
}
})
}

func prepareDir(t *testing.T) string {
Expand Down

0 comments on commit 5b63e04

Please sign in to comment.