Bladeren bron

Introduce commands and subcommands

Dennis Rodewyk 5 jaren geleden
bovenliggende
commit
87bf281a94
1 gewijzigde bestanden met toevoegingen van 54 en 15 verwijderingen
  1. 54 15
      app.go

+ 54 - 15
app.go

@@ -17,8 +17,15 @@ func contains(arr [3]string, str string) bool {
 	return false
 }
 
-func main() {
+func myUsage() {
+	fmt.Println("usage: passphrase-entropy <command> [<args>]")
+	fmt.Println("Available commands are: ")
+	fmt.Println(" diceware   Calculate entropy of a diceware passphrase")
+	fmt.Println(" random     Calculate entropy of a random string")
+	fmt.Println(" insecure   Calculate entropy of an invented passphrase")
+}
 
+func main() {
 	var charsets map[string]string
 	var usedCharsets[] string
 	var possibleSymbols int = 0
@@ -38,27 +45,52 @@ func main() {
 	flag.IntVar(&words, "words", 0, "How many words")
 	flag.StringVar(&passphrase, "random", "", "a random string password")
 	flag.StringVar(&passphrase, "insecure", "", "a human invented password")
+	dicewareCommand := flag.NewFlagSet("diceware", flag.ExitOnError)
+	passphraseFlag := dicewareCommand.String("passphrase", "", "the passphrase")
+	wordsFlag := dicewareCommand.Int("words", 0, "number of words in passphrase")
+	dictSizeFlag := dicewareCommand.Int("dictSize", 0, "number of words in dictionary")
 
-	flag.Parse()
+	randomCommand := flag.NewFlagSet("random", flag.ExitOnError)
 
-	seen := make(map[string]bool)
-	flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
+	insecureCommand := flag.NewFlagSet("insecure", flag.ExitOnError)
+	insecureWords := insecureCommand.Int("words", 0, "number of words in invented passphrase")
 
-	if len(seen) == 0 {
-		flag.PrintDefaults()
-		os.Exit(2) // the same exit code flag.Parse uses
+	if len(os.Args) == 1 {
+		myUsage()
+		return
 	}
 
-	fmt.Println("\nEvaluated string:", passphrase)
+	switch os.Args[1] {
+	case "diceware":
+		dicewareCommand.Parse(os.Args[2:])
+	case "random":
+		randomCommand.Parse(os.Args[2:])
+	case "insecure":
+		insecureCommand.Parse(os.Args[2:])
+	default:
+		myUsage()
+		fmt.Printf("\n%q is not valid command.\n", os.Args[1])
+		os.Exit(2)
+	}
 
-	if dice := seen["diceware"]; dice {
-		if dice := !seen["words"]; dice {
-			os.Exit(2)
-		} else {
-			entropy := math.Log2(math.Pow(float64(7776), float64(words)))
-			fmt.Println("\nPassphrase entropy:", entropy)
+	if dicewareCommand.Parsed() {
+		if *passphraseFlag == "" {
+			dicewareCommand.Usage()
+			return
+		}
+		if *wordsFlag == 0 {
+			dicewareCommand.Usage()
+			return
+		}
+		if *dictSizeFlag == 0 {
+			dicewareCommand.Usage()
+			return
 		}
-	} else if rnd := seen["random"]; rnd {
+			entropy := math.Log2(math.Pow(float64(*dictSizeFlag), float64(*wordsFlag)))
+			fmt.Println("\nPassphrase entropy:", entropy)
+	}
+
+	if randomCommand.Parsed() {
 		passphraseLength := len(passphrase)
 
 		for key, value := range charsets {
@@ -74,4 +106,11 @@ func main() {
 		fmt.Println("\nUsed character sets:", strings.Join(usedCharsets, ", "))
 		fmt.Println("\nPassphrase entropy:", entropy)
 	}
+
+	if insecureCommand.Parsed() {
+		if *insecureWords != 0 {
+			insecureCommand.Usage()
+			return
+		}
+	}
 }