Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/connorcli/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
62 changes: 62 additions & 0 deletions cmd/connorcli/size/size.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions cmd/connorcli/size/size_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}