From 4665cc65cf2bff82b68e3c53cd8a76c2191c9824 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Sun, 18 May 2025 13:16:36 -0700 Subject: [PATCH] Added feature to check total directory size and added testing --- cmd/connorcli/root/root.go | 2 ++ cmd/connorcli/size/size.go | 62 +++++++++++++++++++++++++++++++++ cmd/connorcli/size/size_test.go | 30 ++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 cmd/connorcli/size/size.go create mode 100644 cmd/connorcli/size/size_test.go diff --git a/cmd/connorcli/root/root.go b/cmd/connorcli/root/root.go index 059b71f..3088b44 100644 --- a/cmd/connorcli/root/root.go +++ b/cmd/connorcli/root/root.go @@ -10,6 +10,7 @@ import ( del "connorcli/cmd/connorcli/delete" "connorcli/cmd/connorcli/list" "connorcli/cmd/connorcli/rename" + "connorcli/cmd/connorcli/size" "github.com/spf13/cobra" ) @@ -31,6 +32,7 @@ func RootCommand() *cobra.Command { cmd.AddCommand(rename.RenameCommand()) cmd.AddCommand(del.DeleteCommand()) // Delete is a keyword I shouldn't use it cmd.AddCommand(create.CreateCommand()) + cmd.AddCommand(size.SizeCommand()) return cmd } diff --git a/cmd/connorcli/size/size.go b/cmd/connorcli/size/size.go new file mode 100644 index 0000000..78706aa --- /dev/null +++ b/cmd/connorcli/size/size.go @@ -0,0 +1,62 @@ +package size + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/spf13/cobra" +) + +func SizeCommand() *cobra.Command { + return &cobra.Command{ + Use: "sz", + Short: "Finds the size of the current directory in Bytes", + Long: "Finds the size of the files and current directory of all subdirectories measured in Bytes, can specify a directory with arguments", + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 1 { + if strings.HasPrefix(args[0], "./") && strings.HasSuffix(args[0], "/") { + fmt.Fprint(cmd.OutOrStdout(), getSize(args[0], 0), " Bytes\n") + } else { + dirName := args[0] + if !strings.HasPrefix(args[0], "./") { + dirName = "./" + dirName + } + if !strings.HasSuffix(args[0], "/") { + dirName += "/" + } + fmt.Fprint(cmd.OutOrStdout(), getSize(dirName, 0), " Bytes\n") + } + } else { + fmt.Fprint(cmd.OutOrStdout(), getSize("./", 0), " Bytes\n") + } + }, + } + +} + +func getSize(dirName string, currSize int64) int64 { + // Recursively enter directories add file size to running total + files, err := os.ReadDir(dirName) + + if err != nil { + log.Fatal(err) + } + + for _, file := range files { + var isHidden bool = strings.HasPrefix(file.Name(), ".") + if !isHidden && !file.IsDir() { + // Add file size + fileInfo, err := os.Stat(dirName + file.Name()) + if err != nil { + log.Fatal(err) + } + currSize += fileInfo.Size() + } else if !isHidden && file.IsDir() { + currSize += getSize(dirName+file.Name()+"/", currSize) + } + } + return currSize +} diff --git a/cmd/connorcli/size/size_test.go b/cmd/connorcli/size/size_test.go new file mode 100644 index 0000000..a1350f1 --- /dev/null +++ b/cmd/connorcli/size/size_test.go @@ -0,0 +1,30 @@ +package size_test + +import ( + "bytes" + "connorcli/cmd/connorcli/size" + "testing" +) + +func TestSizeCmd_Execute(t *testing.T) { + // Create Command + cmd := size.SizeCommand() + + // Redirect stdout + var stdout bytes.Buffer + cmd.SetOut(&stdout) + + // Catch Errors + err := cmd.Execute() + + if err != nil { + t.Errorf("Unexpected Error: %v", err) + } + + // Check if output is correct + + expectedOutput := "2086 Bytes\n" + if stdout.String() != expectedOutput { + t.Errorf("Expected output: %q, but received: %q", expectedOutput, stdout.String()) + } +}