Skip to content

Commit

Permalink
internal/cli: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
avakarev committed Apr 30, 2023
1 parent 248c803 commit 74fd53d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
15 changes: 3 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@ import (

func main() {
cli.Validate()
for i, f := range cli.Files {
for i, f := range cli.Files() {
if i > 0 {
log.Println("")
}
log.Println(f)
if cli.MD5Flag {
printChecksum(f, cryptoutil.MD5)
}
if cli.SHA1Flag {
printChecksum(f, cryptoutil.SHA1)
}
if cli.SHA256Flag {
printChecksum(f, cryptoutil.SHA256)
}
if cli.SHA512Flag {
printChecksum(f, cryptoutil.SHA512)
for _, h := range cli.HashFlags() {
printChecksum(f, h)
}
}
}
Expand Down
33 changes: 24 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,31 @@ var (
SHA1Flag bool // SHA1Flag represents state of sha1 flag
SHA256Flag bool // SHA256Flag represents state of sha256 flag
SHA512Flag bool // SHA512Flag represents state of sha512 flag

Files []string // Files represents list of files passed as arguments
)

// HashFlags return list of hash algorithms passed as flags
func HashFlags() []string {
flags := make([]string, 0)
if MD5Flag {
flags = append(flags, cryptoutil.MD5)
}
if SHA1Flag {
flags = append(flags, cryptoutil.SHA1)
}
if SHA256Flag {
flags = append(flags, cryptoutil.SHA256)
}
if SHA512Flag {
flags = append(flags, cryptoutil.SHA512)
}
return flags
}

// Files returns list of files passed as arguments
func Files() []string {
return flag.Args()
}

// Validate validates whether passed cli flags and arguments are enough
func Validate() {
args := os.Args[1:]
Expand All @@ -39,12 +60,7 @@ func Validate() {
os.Exit(1)
}

if !MD5Flag && !SHA1Flag && !SHA256Flag && !SHA512Flag {
printHelp()
os.Exit(1)
}

if len(Files) == 0 {
if len(HashFlags()) == 0 || len(Files()) == 0 {
printHelp()
os.Exit(1)
}
Expand Down Expand Up @@ -77,5 +93,4 @@ func init() {
flag.BoolVar(&SHA256Flag, cryptoutil.SHA256, false, "calculate SHA256")
flag.BoolVar(&SHA512Flag, cryptoutil.SHA512, false, "calculate SHA512")
flag.Parse()
Files = flag.Args()
}

0 comments on commit 74fd53d

Please sign in to comment.