Skip to content

Commit

Permalink
Update main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-buss committed Sep 22, 2021
1 parent 07529f0 commit a1aecc0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
11 changes: 11 additions & 0 deletions cmd/openbooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"fmt"
"math/rand"
"os"
"time"

"github.com/brianvoe/gofakeit/v5"
"github.com/spf13/cobra"
)

Expand All @@ -18,3 +21,11 @@ func main() {
os.Exit(1)
}
}

// Generate a random username to avoid IRC name collisions if multiple users are hosting
// at the same time.
func generateUserName() string {
rand.Seed(time.Now().UnixNano())
gofakeit.Seed(int64(rand.Int()))
return fmt.Sprintf("%s-%s", gofakeit.Adjective(), gofakeit.Noun())
}
15 changes: 1 addition & 14 deletions cmd/openbooks/server.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package main

import (
"fmt"
"math/rand"
"os"
"path"
"time"

"github.com/brianvoe/gofakeit/v5"
"github.com/evan-buss/openbooks/server"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(serverCmd)
userName := generateUserName()

serverCmd.Flags().BoolP("log", "l", false, "Save raw IRC logs for each client connection.")
serverCmd.Flags().BoolP("browser", "b", false, "Open the browser on server start.")
serverCmd.Flags().StringP("name", "n", userName, "Use a name that isn't randomly generated. One word only.")
serverCmd.Flags().StringP("name", "n", generateUserName(), "Use a name that isn't randomly generated. One word only.")
serverCmd.Flags().StringP("port", "p", "5228", "Set the local network port for browser mode.")
serverCmd.Flags().StringP("dir", "d", path.Join(os.TempDir(), "openbooks"), "The directory where eBooks are saved when persist enabled.")
serverCmd.Flags().Bool("persist", false, "Persist eBooks in 'dir'. Default is to delete after sending.")
Expand Down Expand Up @@ -64,14 +59,6 @@ var serverCmd = &cobra.Command{
},
}

// Generate a random username to avoid IRC name collisions if multiple users are hosting
// at the same time.
func generateUserName() string {
rand.Seed(time.Now().UnixNano())
gofakeit.Seed(int64(rand.Int()))
return fmt.Sprintf("%s-%s", gofakeit.Adjective(), gofakeit.Noun())
}

func sanitizePath(basepath string) string {
cleaned := path.Clean(basepath)
if cleaned == "/" {
Expand Down

0 comments on commit a1aecc0

Please sign in to comment.