Skip to content

Commit

Permalink
Added --help and creating already existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey1330 committed Jun 21, 2023
1 parent 493f4e9 commit 808756c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,36 @@ package main
import (
"flag"
"fmt"
"os"
)

func main() {

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: filecommander <command> <args>\n")
fmt.Fprintf(os.Stderr, "\nCommands:\n")
fmt.Fprintf(os.Stderr, " create <filename> Create a new file\n")
fmt.Fprintf(os.Stderr, " read <filename> Read the contents of a file\n")
fmt.Fprintf(os.Stderr, " write <filename> <content> Write content to a file\n")
fmt.Fprintf(os.Stderr, " delete <filename> Delete a file\n")
fmt.Fprintf(os.Stderr, " list <directory> List files in a directory\n")
fmt.Fprintf(os.Stderr, " copy <srcfile> <destfile> Copy a file to a new location\n")
fmt.Fprintf(os.Stderr, " move <srcfile> <destfile> Move a file to a new location\n")
fmt.Fprintf(os.Stderr, " search <directory> <filename> Search for a file in a directory\n")
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
}

// Define flags
help := flag.Bool("help", false, "Show help")

flag.Parse()

if *help {
flag.Usage()
return
}

args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: filecommander <command> <args>")
Expand Down
18 changes: 18 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import (
)

func createFile(filename string) {
if _, err := os.Stat(filename); err == nil {
// File exists, prompt user to overwrite
answer := ""
for answer != "y" && answer != "n" {
fmt.Print("File already exists. Do you want to overwrite it? (y/n): ")
_, err := fmt.Scanln(&answer)
if err != nil {
fmt.Println(err)
return
}
}

if answer == "n" {
return
}
}

// Create the file
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file:", err)
Expand Down

0 comments on commit 808756c

Please sign in to comment.