Skip to content

Commit

Permalink
mkdir: added verbose flag, changed error handling method
Browse files Browse the repository at this point in the history
- The verbose flag needs more work in how it behaves with
  the -parents flag. To observe, compare output between this
  implementation and the actual mkdir, with both parent and
  verbose flags, and create a nested directory whose
  parent does not exist.
- When mkdir encounters errors, they are simply displayed.
  Any proceeding arguments are processed. Because the Fatal
  functions call os.Exit(1) and the Panic functions call
  panic after writing their log message, I removed usage of
  the log package and opted to simply display the error.
  • Loading branch information
Xercoy committed Jun 17, 2014
1 parent 9085320 commit 0c29544
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions mkdir/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
)

Expand All @@ -21,6 +20,7 @@ const (
--version output version information and exit
--parents create parent directory/directories as
needed, do nothing if already existing
--verbose print a message for each created directory
`

version_text = `
Expand All @@ -37,13 +37,16 @@ const (
already exist, do nothing.
`

usage_text = `usage: mkdir [-parents] directory ...`
usage_text = `usage: mkdir [-parents, -verbose] directory ...`

verbose_text = `Print a message for each created directory.`
)

var (
help = flag.Bool("help", false, help_text)
version = flag.Bool("version", false, version_text)
parents = flag.Bool("parents", false, parents_text)
verbose = flag.Bool("verbose", false, verbose_text)
)

func main() {
Expand All @@ -69,13 +72,17 @@ func main() {
mkdirAllError := os.MkdirAll(flag.Arg(i), os.ModePerm)

if mkdirAllError != nil {
log.Fatalln(mkdirAllError)
fmt.Println(mkdirAllError)
} else if *verbose {
fmt.Printf("%s\n", flag.Arg(i))
}
} else {
mkdirError := os.Mkdir(flag.Arg(i), os.ModePerm)

if mkdirError != nil {
log.Fatalln(mkdirError)
fmt.Println(mkdirError)
} else if *verbose {
fmt.Printf("mkdir: created directory '%s'\n", flag.Arg(i))
}
}
}
Expand Down

0 comments on commit 0c29544

Please sign in to comment.