From 808756c3deea954d4efde1a358d4d49316903ff4 Mon Sep 17 00:00:00 2001 From: geoffrey1330 Date: Wed, 21 Jun 2023 01:21:05 +0100 Subject: [PATCH] Added --help and creating already existing file --- main.go | 25 +++++++++++++++++++++++++ utils.go | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/main.go b/main.go index 2a98cb0..f77c1dd 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,36 @@ package main import ( "flag" "fmt" + "os" ) func main() { + + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: filecommander \n") + fmt.Fprintf(os.Stderr, "\nCommands:\n") + fmt.Fprintf(os.Stderr, " create Create a new file\n") + fmt.Fprintf(os.Stderr, " read Read the contents of a file\n") + fmt.Fprintf(os.Stderr, " write Write content to a file\n") + fmt.Fprintf(os.Stderr, " delete Delete a file\n") + fmt.Fprintf(os.Stderr, " list List files in a directory\n") + fmt.Fprintf(os.Stderr, " copy Copy a file to a new location\n") + fmt.Fprintf(os.Stderr, " move Move a file to a new location\n") + fmt.Fprintf(os.Stderr, " search 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 ") diff --git a/utils.go b/utils.go index 3ac3dc0..4917a5b 100644 --- a/utils.go +++ b/utils.go @@ -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)