Browse Source

Seed random with nanosecond time

It's possible that users will run commands more than once a second.
Thus, seeding the random number generator with the current time in
seconds could produce results like the same container name in subsequent
commands.

Seeding with the current time in nanoseconds reduces the probability of
this.

Signed-off-by: Christopher Crone <[email protected]>
Christopher Crone 5 years ago
parent
commit
88ba591fc3
2 changed files with 4 additions and 5 deletions
  1. 0 5
      azure/login/login.go
  2. 4 0
      cli/main.go

+ 0 - 5
azure/login/login.go

@@ -5,7 +5,6 @@ import (
 	"encoding/json"
 	"fmt"
 	"log"
-	"math/rand"
 	"net/url"
 	"os/exec"
 	"path/filepath"
@@ -24,10 +23,6 @@ import (
 	"github.com/pkg/errors"
 )
 
-func init() {
-	rand.Seed(time.Now().Unix())
-}
-
 //go login process, derived from code sample provided by MS at https://github.com/devigned/go-az-cli-stuff
 const (
 	authorizeFormat  = "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&prompt=select_account&response_mode=query&scope=%s"

+ 4 - 0
cli/main.go

@@ -30,11 +30,13 @@ package main
 import (
 	"context"
 	"fmt"
+	"math/rand"
 	"os"
 	"os/exec"
 	"os/signal"
 	"path/filepath"
 	"syscall"
+	"time"
 
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
@@ -69,6 +71,8 @@ func init() {
 	if err := os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), path)); err != nil {
 		panic(err)
 	}
+	// Seed random
+	rand.Seed(time.Now().UnixNano())
 }
 
 func isOwnCommand(cmd *cobra.Command) bool {