Initial Commit (Read: Dirty AF)

This commit is contained in:
2025-01-29 23:20:44 +01:00
parent fdb22e42ec
commit 14012dabd7
15 changed files with 310 additions and 1 deletions

36
flags/ParseFlags.go Normal file
View File

@ -0,0 +1,36 @@
package flags
import "flag"
func (flags *Flags) ParseFlags() (err error) {
// Define parameters
flag.StringVar(&flags.Vars.Role, "role", "scheduler", "Role of the instance ['scheduler','agent']")
flag.StringVar(&flags.Vars.Address, "address", "127.0.0.1", "Address of the instance to listen to/connect to")
flag.IntVar(&flags.Vars.Port, "port", 8080, "Port of the instance to listen on/connect to")
flag.IntVar(&flags.Vars.NumCPUs, "numcpus", 1, "[agent] Number of CPUs to use")
// Parse the flags
flag.Parse()
// Parse the role parameters
err = flags.parseRole()
if err != nil {
return err
}
/// Parse the number of CPUs parameters
err = flags.parseNumCPUs()
if err != nil {
return err
}
// Parse the address and port parameters
err = flags.parseAddressPort()
if err != nil {
return err
}
// Return nil if no errors
return
}

32
flags/parseAddressPort.go Normal file
View File

@ -0,0 +1,32 @@
package flags
import (
"fmt"
"net"
)
func (flags *Flags) parseAddressPort() (err error) {
// Ensure that address field is not empty
if flags.Vars.Address == "" {
return fmt.Errorf("Address cannot be empty")
}
// Ensure that address field is valid IP address
if net.ParseIP(flags.Vars.Address) == nil {
return fmt.Errorf("GloVars must be a valid IP address")
}
// Ensure that port field is not empty
if flags.Vars.Port == 0 {
return fmt.Errorf("Port cannot be empty")
}
// Ensure that port field is within the valid range
if flags.Vars.Port < 1 || flags.Vars.Port > 65535 {
return fmt.Errorf("Port must be between 1 and 65535")
}
return nil
}

12
flags/parseNumCPUs.go Normal file
View File

@ -0,0 +1,12 @@
package flags
import "fmt"
func (flags *Flags) parseNumCPUs() (err error) {
if flags.Vars.NumCPUs <= 0 {
return fmt.Errorf("Number of CPUs must be greater than 0")
}
return nil
}

18
flags/parseRole.go Normal file
View File

@ -0,0 +1,18 @@
package flags
import "fmt"
func (flags *Flags) parseRole() (err error) {
// Ensure that role field is not empty
if flags.Vars.Role == "" {
return fmt.Errorf("Role cannot be empty")
}
// Ensure that role field is valid
if flags.Vars.Role != "scheduler" && flags.Vars.Role != "agent" {
return fmt.Errorf("Role must be either 'scheduler' or 'agent'")
}
return nil
}

7
flags/types.go Normal file
View File

@ -0,0 +1,7 @@
package flags
import "gitea.ligthert.net/golang/sfcs/vars"
type Flags struct {
Vars *vars.Vars
}