A Small Program to extract Archives

in Programming & Dev2 months ago

a small program I wrote to extract archives, it is written in go

package and imports

package main

import (
    "flag"
    "fmt"
    "os/exec"
    "regexp"
)

This block imports all the different packages I need from the standard library.
The packages are simple enough, flag is used to parse the arguments to the program
like unpack -file somefile.rar all those flags starting with a dash
(windows tend to use /, but I am on a unix style system).
fmt is used to print things to the console, and I could in fact remove this, it
was more a debugging thing to check things while I wrote the program, it does in fact not
supply any useful information at this point, but I have a feeling I might need to add a few things later
so this is still in the program.
os/exec this is exec in the source code, and this is a way to run external commands
I did not write code to do the extraction, so I just shell out to other utils.
regexp regular expressions, I needed to get the file end, and some form of number before that,
and I thought it was easier to just do a regex rather than writting a parser.

Now for the code.

main function

func main() {
    filename_ptr := flag.String("file", "", "the file to unpack")
    path_ptr := flag.String("path", "", "directory in which the file is placed")
    flag.Parse()

These lines setup the flags I need, I did not research a way to get the working dir from running the program,
so I added a flag for that, this might not be the best way. flag parse is important if that is not called the flags don't
do anything.

    fmt.Println(*filename_ptr, *path_ptr)
    rep, reer := regexp.Compile(`([-a-zA-Z0-9]+)(\d+).([a-zA-Z0-9]+)`)
    if reer != nil {
        panic(reer)
    }

the println is not necessary.
the next line sets up a regex rep is the regex in a variable, reer is the error, if there is something
wrong checked in the if statement the program will simply panic, as I don't see any good way to do anything at that
point.

func runCommand(cmd *exec.Cmd, dir string) {
    cmd.Dir = dir
    cmd.Run()
}

let me just add this block here before the final block, since it will be clearer.
This just adds a few things to the command before running it.
It is necessary to tell the command in which dir (directory / folder) to run the command in
if this is not set it defaults to the current directory, that is likely a problem, so just set this. I think that is a good habit.

    var cmd *exec.Cmd
    file_matches := rep.FindAllStringSubmatch(*filename_ptr, -1)
    switch file_matches[0][3] {
    case "rar":
        cmd = exec.Command("unrar", "x", *filename_ptr)
        runCommand(cmd, *path_ptr)
    case "zip":
        cmd = exec.Command("unzip", *filename_ptr)
        runCommand(cmd, *path_ptr)
    case "tar.gz":
        cmd = exec.Command("tar", "xpf", *filename_ptr)
        runCommand(cmd, *path_ptr)
    }
}

This is a bigger block, but it is fairly simple. the var cmd is just setting up a variable for later, the next line the regex is
used to split up the input from one of the flags (file flag) into several parts so I can handle it. Thinking about it now the way the program is used
this feels over complicated, I should probably add the extra functionality to this or this functionality is much more complex than is strictly necessary.
The thing I want this program to do is given a folder find all archives to be extracted, extract each one (but only do the one called part1 or something with the 1. archive extension)
then delete the archive since the file is extracted.