Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Sep 6, 2024
1 parent 0badcd6 commit 67f171c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 63 deletions.
42 changes: 25 additions & 17 deletions cmd/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,30 @@ type Options struct {

const DefaultPath = "./docs"

func DeterminePath(args []string) (string, error) {
if len(args) == 0 {
func GetDocsPath(path string) (string, error) {
if path == "" {
fmt.Printf("No path was provided. Using default path: %s\n", DefaultPath)
return DefaultPath, nil
path = DefaultPath
}

return filepath.Abs(args[0])
}
absPath, err := filepath.Abs(path)

if err != nil {
return "", fmt.Errorf("error resolving absolute path: %w", err)
}

_, err = os.Stat(absPath)

func EnsureDirectoryExists(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
fmt.Printf("The directory %s does not exist. Creating it...\n", path)
return os.MkdirAll(path, os.ModePerm)
fmt.Printf("The directory %s does not exist. Creating it...\n", absPath)
if err := os.MkdirAll(absPath, os.ModePerm); err != nil {
return "", fmt.Errorf("error creating directory %s: %w", absPath, err)
}
} else if err != nil {
return "", fmt.Errorf("error checking directory %s: %w", absPath, err)
}
return err

return absPath, nil
}

func GenerateDocumentation(rootCmd *cobra.Command, path string) error {
Expand All @@ -48,23 +56,23 @@ func GenerateDocumentation(rootCmd *cobra.Command, path string) error {

func NewDocsCommand() *cobra.Command {
return &cobra.Command{
Use: "docs",
Use: "docs [path]",
Short: "Generates the documentation for the CLI",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Parent().Root().DisableAutoGenTag = true

path, err := DeterminePath(args)
var path string
if len(args) > 0 {
path = args[0]
}

resolvedPath, err := GetDocsPath(path)
if err != nil {
return err
}

if err := EnsureDirectoryExists(path); err != nil {
return fmt.Errorf("error creating documentation output directory %s: %s", path, err)
}

return GenerateDocumentation(cmd.Parent().Root(), path)
return GenerateDocumentation(cmd.Parent().Root(), resolvedPath)
},
}
}
90 changes: 44 additions & 46 deletions cmd/docs/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,77 @@ import (
"testing"
)

func TestDeterminePath(t *testing.T) {
func TestGetDocsPath(t *testing.T) {
t.Parallel()

t.Run("No path passed to command", func(t *testing.T) {
t.Run("No path provided", func(t *testing.T) {
t.Parallel()
got, err := DeterminePath([]string{})
actual, err := GetDocsPath("")

if err != nil {
t.Errorf("DeterminePath() error = %v, wantErr false", err)
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
return
}

if got != DefaultPath {
t.Errorf("DeterminePath() = %v, want %v", got, DefaultPath)
expected, _ := filepath.Abs(DefaultPath)
if actual != expected {
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
}
})

t.Run("With path passed to command", func(t *testing.T) {
t.Run("With path provided", func(t *testing.T) {
t.Parallel()
expected := "/tmp/docs"
got, err := DeterminePath([]string{expected})
inputPath := filepath.Join(os.TempDir(), "docs")
actual, err := GetDocsPath(inputPath)

if err != nil {
t.Errorf("DeterminePath() error = %v, wantErr false", err)
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
return
}

if got != expected {
t.Errorf("DeterminePath() = %v, want %v", got, expected)
expected, _ := filepath.Abs(inputPath)
if actual != expected {
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
}
})
}

func TestEnsureDirectoryExists(t *testing.T) {
t.Parallel()

t.Run("Existing directory", func(t *testing.T) {
t.Parallel()
tempDir, err := os.MkdirTemp(t.TempDir(), "docs_test_existing")

if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
if _, err := os.Stat(actual); os.IsNotExist(err) {
t.Errorf("GetDocsPath() failed to create directory %s", actual)
}
})

err = EnsureDirectoryExists(tempDir)
t.Run("Invalid path", func(t *testing.T) {
t.Parallel()
invalidPath := string([]byte{0})

if err != nil {
t.Errorf("EnsureDirectoryExists() error = %v, wantErr false", err)
return
}
_, err := GetDocsPath(invalidPath)

if _, err := os.Stat(tempDir); os.IsNotExist(err) {
t.Errorf("EnsureDirectoryExists() failed to recognize existing directory %s", tempDir)
if err == nil {
t.Errorf("GetDocsPath() error = nil, wantErr true")
}
})
}

t.Run("New directory", func(t *testing.T) {
t.Parallel()
tempDir, err := os.MkdirTemp(t.TempDir(), "docs_test_new")
func TestGetDocsPath_ExistingDirectory(t *testing.T) {
t.Parallel()

if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
tempDir, err := os.MkdirTemp("", "docs_test_existing")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}

newDir := filepath.Join(tempDir, "new_dir")
err = EnsureDirectoryExists(newDir)
actual, err := GetDocsPath(tempDir)

if err != nil {
t.Errorf("EnsureDirectoryExists() error = %v, wantErr false", err)
return
}
if _, err := os.Stat(newDir); os.IsNotExist(err) {
t.Errorf("EnsureDirectoryExists() failed to create directory %s", newDir)
}
})
if err != nil {
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
return
}

expected, _ := filepath.Abs(tempDir)
if actual != expected {
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
}

if _, err := os.Stat(actual); os.IsNotExist(err) {
t.Errorf("GetDocsPath() failed to recognize existing directory %s", actual)
}
}

0 comments on commit 67f171c

Please sign in to comment.