Skip to content

Commit

Permalink
Use "create", "list" and "extract" subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
angelsolaorbaiceta committed Sep 4, 2024
1 parent 4ebfb4e commit d9a2af6
Showing 1 changed file with 41 additions and 52 deletions.
93 changes: 41 additions & 52 deletions cmd/aar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,64 @@ import (
"github.com/angelsolaorbaiceta/aar/cmd"
)

var (
createFlag = flag.Bool("c", false, "Create a new archive")
extractFlag = flag.Bool("x", false, "Extract an archive")
extractNameFlag = flag.String("n", "", "Extract a specific file by name from the archive")
listFlag = flag.Bool("l", false, "List the contents of an archive")
fileName = flag.String("f", "", "Output filename of the archive")
)

func main() {
flag.Parse()
validateFlags()
var (
createCmd = flag.NewFlagSet("create", flag.ExitOnError)
createFileNameFlag = createCmd.String("f", "", "Output filename of the archive")

extractCmd = flag.NewFlagSet("extract", flag.ExitOnError)
extractFileNameFlag = extractCmd.String("f", "", "Filename of the archive to extract")
extractNameFlag = extractCmd.String("n", "", "Extract a specific file by name from the archive")

if *createFlag {
createArchive()
} else if *extractFlag {
if *extractNameFlag != "" {
extractArchiveFile(*extractNameFlag)
listCmd = flag.NewFlagSet("list", flag.ExitOnError)
listFileNameFlag = listCmd.String("f", "", "Filename of the archive to list")
)

if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: aar <command> [options]\n")
os.Exit(1)
}

switch os.Args[1] {
case "create":
createCmd.Parse(os.Args[2:])
validateFileName(*createFileNameFlag)
fileNames := createCmd.Args()
createArchive(*createFileNameFlag, fileNames)

case "extract":
extractCmd.Parse(os.Args[2:])
validateFileName(*extractFileNameFlag)

if *extractNameFlag == "" {
cmd.ExtractArchive(*extractFileNameFlag)
} else {
extractArchive()
cmd.ExtractArchiveFile(*extractFileNameFlag, *extractNameFlag)
}
} else if *listFlag {
listArchive()
} else {
fmt.Println("Usage: aar [options] -f <filename>")
flag.PrintDefaults()
}
}

func validateFlags() {
activeFlags := 0
if *createFlag {
activeFlags++
}
if *extractFlag {
activeFlags++
}
if *listFlag {
activeFlags++
}
case "list":
listCmd.Parse(os.Args[2:])
validateFileName(*listFileNameFlag)
cmd.ListArchive(*listFileNameFlag)

if activeFlags != 1 {
fmt.Fprintf(os.Stderr, "You must specify one of the -c (create), -x (extract), or -l (list) flags.\n")
default:
fmt.Fprintf(os.Stderr, "Usage: aar <command> [options]\n")
os.Exit(1)
}
}

if *fileName == "" {
func validateFileName(name string) {
if name == "" {
fmt.Fprintf(os.Stderr, "You must specify a filename with the -f flag.\n")
os.Exit(1)
}
}

func createArchive() {
fileNames := flag.Args()
func createArchive(fileName string, fileNames []string) {
if len(fileNames) == 0 {
fmt.Fprintf(os.Stderr, "You must specify at least one file to add to the archive.\n")
os.Exit(1)
}

cmd.CreateArchive(*fileName, fileNames)
}

func extractArchive() {
cmd.ExtractArchive(*fileName)
}

func extractArchiveFile(fileToExtract string) {
cmd.ExtractArchiveFile(*fileName, fileToExtract)
}

func listArchive() {
cmd.ListArchive(*fileName)
cmd.CreateArchive(fileName, fileNames)
}

0 comments on commit d9a2af6

Please sign in to comment.