Skip to content

Commit

Permalink
♻️ Refactor: move some functions out of main.go to specialized file
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscasalar committed Nov 9, 2024
1 parent e158da0 commit 8029794
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 108 deletions.
108 changes: 0 additions & 108 deletions cmd/generate-npc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package main

import (
"fmt"
"github.com/carloscasalar/traveller-npc-generator/internal/name"
"github.com/carloscasalar/traveller-npc-generator/internal/npc"
"github.com/carloscasalar/traveller-npc-generator/internal/ui"
"github.com/jessevdk/go-flags"
"gopkg.in/yaml.v3"
"os"
)

Expand Down Expand Up @@ -70,87 +67,6 @@ func toUICharacteristics(characteristic map[npc.Characteristic]int) map[ui.Chara
}
}

func spawnNameGeneratorOrFail() name.Generator {
//read names config from file
namesConfigFile, err := os.ReadFile("assets/names.yml")
if err != nil {
printErrorf("error reading names config file: %v\n", err)
os.Exit(1)
}

var config NameConfig
err = yaml.Unmarshal(namesConfigFile, &config)
if err != nil {
printErrorf("error reading names config: %v\n", err)
os.Exit(1)
}

if err := config.Validate(); err != nil {
printErrorf("error parsing names config: %v\n", err)
os.Exit(1)
}

return name.NewCatalogSourcedGenerator(config.Surnames, config.NonGenderedNames, config.FemaNames, config.MaleNames)
}

func readCitizenCategory(opts CommandOptions) npc.CitizenCategory {
category := npc.CitizenCategory(opts.CitizenCategory)
if !category.IsACitizenCategory() {
printErrorf("unknown citizen category %v\n", opts.CitizenCategory)
os.Exit(1)
}
return category
}

func readExperience(opts CommandOptions) npc.Experience {
if !npc.Experience(opts.Experience).IsAExperience() {
printErrorf("unknown experience %v\n", opts.Experience)
os.Exit(1)
}

return npc.Experience(opts.Experience)
}

func readRole(opts CommandOptions) npc.Role {
role, err := npc.RoleString(opts.CrewRole)
if err != nil {
printErrorf("unknown role %v\n", opts.CrewRole)
os.Exit(1)
}
return role
}

func readGender(opts CommandOptions) name.Gender {
switch opts.Gender {
case "female":
return name.GenderFemale
case "male":
return name.GenderMale
default:
return name.GenderUnspecified
}
}

func readOptionsOrFail() CommandOptions {
var opts CommandOptions
parser := flags.NewParser(&opts, flags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(flags.ErrorType); ok && flagsErr == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
return opts
}

type CommandOptions struct {
CitizenCategory int `short:"c" default:"1" long:"category" choice:"0" choice:"1" choice:"2" choice:"3" description:"Citizen Category: 0-Below average, 1-Average, 2-Above Average, 3-Exceptional" required:"true"`
Experience int `short:"e" default:"3" long:"experience" choice:"0" choice:"1" choice:"2" choice:"3" choice:"4" choice:"5" description:"Experience: 0-Recruit, 1-Rookie, 2-Intermediate, 3-Regular, 4-Veteran, 5-Elite" required:"true"`
CrewRole string `short:"r" long:"role" required:"true" choice:"pilot" choice:"navigator" choice:"engineer" choice:"steward" choice:"medic" choice:"marine" choice:"gunner" choice:"scout" choice:"technician" choice:"leader" choice:"diplomat" choice:"entertainer" choice:"trader" choice:"thug" description:"Crew role in a starship"`
Gender string `short:"g" long:"gender" default:"unspecified" choice:"female" choice:"male" choice:"unspecified" description:"Gender of the NPC"`
EnableDebug bool `short:"d" long:"debug" description:"Enable debug mode"`
}

func prompt(value string) {
fmt.Println(ui.NewPromptRenderer(value).Render())
}
Expand All @@ -166,27 +82,3 @@ func titleValue[T any](title string, value T) {
func printSheet(sheet *ui.CharacterSheet) {
fmt.Println(sheet.Render())
}

type NameConfig struct {
Surnames []string `yaml:",flow"`
NonGenderedNames []string `yaml:"non_gendered_names,flow"`
FemaNames []string `yaml:"female_names,flow"`
MaleNames []string `yaml:"male_names,flow"`
}

func (c NameConfig) Validate() error {
if len(c.Surnames) == 0 {
return fmt.Errorf("surnames cannot be empty")
}
if len(c.NonGenderedNames) == 0 {
return fmt.Errorf("non_gendered_names cannot be empty")
}
if len(c.FemaNames) == 0 {
return fmt.Errorf("female_names cannot be empty")
}
if len(c.MaleNames) == 0 {
return fmt.Errorf("male_names cannot be empty")
}

return nil
}
54 changes: 54 additions & 0 deletions cmd/generate-npc/name_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"github.com/carloscasalar/traveller-npc-generator/internal/name"
"gopkg.in/yaml.v3"
"os"
)

type NameConfig struct {
Surnames []string `yaml:",flow"`
NonGenderedNames []string `yaml:"non_gendered_names,flow"`
FemaNames []string `yaml:"female_names,flow"`
MaleNames []string `yaml:"male_names,flow"`
}

func (c NameConfig) Validate() error {
if len(c.Surnames) == 0 {
return fmt.Errorf("surnames cannot be empty")
}
if len(c.NonGenderedNames) == 0 {
return fmt.Errorf("non_gendered_names cannot be empty")
}
if len(c.FemaNames) == 0 {
return fmt.Errorf("female_names cannot be empty")
}
if len(c.MaleNames) == 0 {
return fmt.Errorf("male_names cannot be empty")
}

return nil
}

func spawnNameGeneratorOrFail() name.Generator {
namesConfigFile, err := os.ReadFile("assets/names.yml")
if err != nil {
printErrorf("error reading names config file: %v\n", err)
os.Exit(1)
}

var config NameConfig
err = yaml.Unmarshal(namesConfigFile, &config)
if err != nil {
printErrorf("error reading names config: %v\n", err)
os.Exit(1)
}

if err := config.Validate(); err != nil {
printErrorf("error parsing names config: %v\n", err)
os.Exit(1)
}

return name.NewCatalogSourcedGenerator(config.Surnames, config.NonGenderedNames, config.FemaNames, config.MaleNames)
}
66 changes: 66 additions & 0 deletions cmd/generate-npc/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"github.com/carloscasalar/traveller-npc-generator/internal/name"
"github.com/carloscasalar/traveller-npc-generator/internal/npc"
"github.com/jessevdk/go-flags"
"os"
)

type CommandOptions struct {
CitizenCategory int `short:"c" default:"1" long:"category" choice:"0" choice:"1" choice:"2" choice:"3" description:"Citizen Category: 0-Below average, 1-Average, 2-Above Average, 3-Exceptional" required:"true"`
Experience int `short:"e" default:"3" long:"experience" choice:"0" choice:"1" choice:"2" choice:"3" choice:"4" choice:"5" description:"Experience: 0-Recruit, 1-Rookie, 2-Intermediate, 3-Regular, 4-Veteran, 5-Elite" required:"true"`
CrewRole string `short:"r" long:"role" required:"true" choice:"pilot" choice:"navigator" choice:"engineer" choice:"steward" choice:"medic" choice:"marine" choice:"gunner" choice:"scout" choice:"technician" choice:"leader" choice:"diplomat" choice:"entertainer" choice:"trader" choice:"thug" description:"Crew role in a starship"`
Gender string `short:"g" long:"gender" default:"unspecified" choice:"female" choice:"male" choice:"unspecified" description:"Gender of the NPC"`
EnableDebug bool `short:"d" long:"debug" description:"Enable debug mode"`
}

func readCitizenCategory(opts CommandOptions) npc.CitizenCategory {
category := npc.CitizenCategory(opts.CitizenCategory)
if !category.IsACitizenCategory() {
printErrorf("unknown citizen category %v\n", opts.CitizenCategory)
os.Exit(1)
}
return category
}

func readExperience(opts CommandOptions) npc.Experience {
if !npc.Experience(opts.Experience).IsAExperience() {
printErrorf("unknown experience %v\n", opts.Experience)
os.Exit(1)
}

return npc.Experience(opts.Experience)
}

func readRole(opts CommandOptions) npc.Role {
role, err := npc.RoleString(opts.CrewRole)
if err != nil {
printErrorf("unknown role %v\n", opts.CrewRole)
os.Exit(1)
}
return role
}

func readGender(opts CommandOptions) name.Gender {
switch opts.Gender {
case "female":
return name.GenderFemale
case "male":
return name.GenderMale
default:
return name.GenderUnspecified
}
}

func readOptionsOrFail() CommandOptions {
var opts CommandOptions
parser := flags.NewParser(&opts, flags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(flags.ErrorType); ok && flagsErr == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
return opts
}

0 comments on commit 8029794

Please sign in to comment.