Hone logo
Hone
Problems

File System Navigator: Go OS Package Challenge

This challenge focuses on utilizing the os package in Go to interact with the file system. You'll be building a simple tool that can list files in a directory, check if a file exists, and create a new directory. This exercise reinforces fundamental file system operations, crucial for many Go applications.

Problem Description

You are tasked with creating a Go program that provides basic file system navigation functionality. The program should accept a command-line argument specifying an action: list, exists, or create.

  • list: If the action is list, the program should list all files and directories within a specified directory path (provided as the second command-line argument). The output should be a newline-separated list of filenames and directory names.
  • exists: If the action is exists, the program should check if a specified file path (provided as the second command-line argument) exists. The program should print "true" if the file exists and "false" otherwise.
  • create: If the action is create, the program should create a new directory at the specified path (provided as the second command-line argument). If the directory already exists, the program should print "Directory already exists". If the creation fails, print "Error creating directory".

The program should handle errors gracefully and provide informative error messages to the user.

Examples

Example 1:

Input: list /tmp/testdir
Output:
file1.txt
dir1
file2.go

Explanation: The program lists the contents of the /tmp/testdir directory.

Example 2:

Input: exists /tmp/testdir/file1.txt
Output:
true

Explanation: The program checks if /tmp/testdir/file1.txt exists and prints "true" because it does.

Example 3:

Input: create /tmp/newdir
Output:

Explanation: The program creates a new directory named newdir inside /tmp. No output is expected if successful.

Example 4:

Input: create /tmp/newdir
Output:
Directory already exists

Explanation: The program attempts to create /tmp/newdir but it already exists.

Example 5:

Input: list non_existent_dir
Output:
Error: stat: no such file or directory

Explanation: The program attempts to list the contents of a directory that does not exist.

Constraints

  • The program must accept exactly two command-line arguments (action and path).
  • The path provided can be an absolute or relative path.
  • Error handling is crucial. The program should not panic and should provide informative error messages.
  • The program should be able to handle cases where the specified directory does not exist.
  • The program should not create files, only directories when using the create action.
  • Assume the user has appropriate permissions to perform the requested actions.

Notes

  • The os package provides functions for interacting with the operating system, including file system operations.
  • Consider using os.Args to access command-line arguments.
  • Use os.Stat to check if a file or directory exists.
  • Use os.Listdir (or os.ReadDir for more modern Go) to list the contents of a directory.
  • Use os.Mkdir to create a new directory.
  • Remember to handle errors returned by these functions. Use if err != nil to check for errors.
  • The /tmp directory is a common location for temporary files and directories on Unix-like systems. You can use it for testing purposes. Be aware that contents of /tmp may be deleted periodically by the system.
Loading editor...
go