Skip to content

Commit

Permalink
refactor(utils): creates type for return codes and makes variable nam…
Browse files Browse the repository at this point in the history
…es consistent
  • Loading branch information
Craig-Spencer-12 committed Sep 7, 2023
1 parent 06b67ec commit 874514b
Show file tree
Hide file tree
Showing 32 changed files with 570 additions and 554 deletions.
14 changes: 7 additions & 7 deletions cmd/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import (

//export rpcCheckAccess
func rpcCheckAccess() int {
status, err := checkAccess()
rc, err := checkAccess()
if err != nil {
log.Error(err.Error())
}
return status
return int(rc)
}

//export rpcExec
func rpcExec(Input *C.char, Output **C.char) int {
if accessStatus := rpcCheckAccess(); accessStatus != utils.Success {
if accessStatus := rpcCheckAccess(); accessStatus != int(utils.Success) {
*Output = C.CString(AccessErrMsg)
return accessStatus
}
Expand All @@ -41,12 +41,12 @@ func rpcExec(Input *C.char, Output **C.char) int {
args, err := r.Read()
if err != nil {
log.Error(err.Error())
return utils.InvalidParameterCombination
return int(utils.InvalidParameterCombination)
}
args = append([]string{"rpc"}, args...)
runStatus := runRPC(args)
if runStatus != utils.Success {
rc := runRPC(args)
if rc != utils.Success {
*Output = C.CString("rpcExec failed: " + inputString)
}
return runStatus
return int(rc)
}
36 changes: 18 additions & 18 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ const AccessErrMsg = "Failed to execute due to access issues. " +
"the MEI driver is installed, " +
"and the runtime has administrator or root privileges."

func checkAccess() (int, error) {
func checkAccess() (utils.ReturnCode, error) {
amtCommand := amt.NewAMTCommand()
result, err := amtCommand.Initialize()
if result != utils.Success || err != nil {
rc, err := amtCommand.Initialize()
if rc != utils.Success || err != nil {
return utils.AmtNotDetected, err
}
return utils.Success, nil
}

func runRPC(args []string) int {
flags, resultCode := parseCommandLine(args)
if resultCode != utils.Success {
return resultCode
func runRPC(args []string) utils.ReturnCode {
flags, rc := parseCommandLine(args)
if rc != utils.Success {
return rc
}
if flags.Local {
resultCode = local.ExecuteCommand(flags)
rc = local.ExecuteCommand(flags)
} else {
resultCode = rps.ExecuteCommand(flags)
rc = rps.ExecuteCommand(flags)
}
return resultCode
return rc
}

func parseCommandLine(args []string) (*flags.Flags, int) {
func parseCommandLine(args []string) (*flags.Flags, utils.ReturnCode) {
//process flags
flags := flags.NewFlags(args)
resultCode := flags.ParseFlags()
rc := flags.ParseFlags()

if flags.Verbose {
log.SetLevel(log.TraceLevel)
Expand All @@ -67,21 +67,21 @@ func parseCommandLine(args []string) (*flags.Flags, int) {
FullTimestamp: true,
})
}
return flags, resultCode
return flags, rc
}

func main() {
status, err := checkAccess()
if status != utils.Success {
rc, err := checkAccess()
if rc != utils.Success {
if err != nil {
log.Error(err.Error())
}
log.Error(AccessErrMsg)
os.Exit(status)
os.Exit(int(rc))
}
status = runRPC(os.Args)
rc = runRPC(os.Args)
if err != nil {
log.Error(err.Error())
}
os.Exit(status)
os.Exit(int(rc))
}
4 changes: 2 additions & 2 deletions internal/amt/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type LocalSystemAccount struct {
}

type Interface interface {
Initialize() (int, error)
Initialize() (utils.ReturnCode, error)
GetVersionDataFromME(key string, amtTimeout time.Duration) (string, error)
GetUUID() (string, error)
GetControlMode() (int, error)
Expand Down Expand Up @@ -102,7 +102,7 @@ func NewAMTCommand() AMTCommand {
}

// Initialize determines if rpc is able to initialize the heci driver
func (amt AMTCommand) Initialize() (int, error) {
func (amt AMTCommand) Initialize() (utils.ReturnCode, error) {
// initialize HECI interface
err := amt.PTHI.Open(false)

Expand Down
27 changes: 14 additions & 13 deletions internal/flags/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package flags

import (
"fmt"
log "github.com/sirupsen/logrus"
"reflect"
"regexp"
"rpc/pkg/utils"

log "github.com/sirupsen/logrus"
)

func (f *Flags) handleActivateCommand() int {
func (f *Flags) handleActivateCommand() utils.ReturnCode {
f.amtActivateCommand.StringVar(&f.DNS, "d", f.lookupEnvOrString("DNS_SUFFIX", ""), "dns suffix override")
f.amtActivateCommand.StringVar(&f.Hostname, "h", f.lookupEnvOrString("HOSTNAME", ""), "hostname override")
f.amtActivateCommand.StringVar(&f.Profile, "profile", f.lookupEnvOrString("PROFILE", ""), "name of the profile to use")
Expand All @@ -32,20 +33,20 @@ func (f *Flags) handleActivateCommand() int {
}
if err := f.amtActivateCommand.Parse(f.commandLineArgs[2:]); err != nil {
re := regexp.MustCompile(`: .*`)
var errCode = utils.IncorrectCommandLineParameters
var rc = utils.IncorrectCommandLineParameters
switch re.FindString(err.Error()) {
case ": -d":
errCode = utils.MissingDNSSuffix
rc = utils.MissingDNSSuffix
case ": -p":
errCode = utils.MissingProxyAddressAndPort
rc = utils.MissingProxyAddressAndPort
case ": -h":
errCode = utils.MissingHostname
rc = utils.MissingHostname
case ": -profile":
errCode = utils.MissingOrIncorrectProfile
rc = utils.MissingOrIncorrectProfile
default:
errCode = utils.IncorrectCommandLineParameters
rc = utils.IncorrectCommandLineParameters
}
return errCode
return rc
}
if f.Local && f.URL != "" {
fmt.Println("provide either a 'url' or a 'local', but not both")
Expand All @@ -70,9 +71,9 @@ func (f *Flags) handleActivateCommand() int {
}

if f.UseACM {
resultCode := f.handleLocalConfig()
if resultCode != utils.Success {
return resultCode
rc := f.handleLocalConfig()
if rc != utils.Success {
return rc
}
// Check if all fields are filled
v := reflect.ValueOf(f.LocalConfig.ACMSettings)
Expand All @@ -87,7 +88,7 @@ func (f *Flags) handleActivateCommand() int {

// Only for CCM it asks for password.
if !f.UseACM && f.Password == "" {
if _, errCode := f.ReadPasswordFromUser(); errCode != 0 {
if _, rc := f.ReadPasswordFromUser(); rc != utils.Success {
return utils.MissingOrIncorrectPassword
}
}
Expand Down
50 changes: 25 additions & 25 deletions internal/flags/activate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestHandleActivateCommand(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-profile", "profileName", "-password", "Password"}
flags := NewFlags(args)
var AMTTimeoutDuration time.Duration = 120000000000
resultCode := flags.ParseFlags()
assert.Equal(t, utils.Success, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.Success, rc)
assert.Equal(t, "wss://localhost", flags.URL)
assert.Equal(t, "profileName", flags.Profile)
assert.Equal(t, utils.CommandActivate, flags.Command)
Expand All @@ -37,8 +37,8 @@ func TestHandleActivateCommandWithTimeOut(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-profile", "profileName", "-password", "Password", "-t", "2s"}
flags := NewFlags(args)
var AMTTimeoutDuration time.Duration = 2000000000
resultCode := flags.ParseFlags()
assert.Equal(t, utils.Success, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.Success, rc)
assert.Equal(t, "wss://localhost", flags.URL)
assert.Equal(t, "profileName", flags.Profile)
assert.Equal(t, utils.CommandActivate, flags.Command)
Expand All @@ -50,8 +50,8 @@ func TestHandleActivateCommandWithTimeOut(t *testing.T) {
func TestHandleActivateCommandWithLMS(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-profile", "profileName", "-lmsaddress", "1.1.1.1", "-lmsport", "99"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.Success, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.Success, rc)
assert.Equal(t, "wss://localhost", flags.URL)
assert.Equal(t, "profileName", flags.Profile)
assert.Equal(t, utils.CommandActivate, flags.Command)
Expand All @@ -61,8 +61,8 @@ func TestHandleActivateCommandWithLMS(t *testing.T) {
func TestHandleActivateCommandWithFriendlyName(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-profile", "profileName", "-name", "friendlyName"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.Success, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.Success, rc)
assert.Equal(t, "wss://localhost", flags.URL)
assert.Equal(t, "profileName", flags.Profile)
assert.Equal(t, utils.CommandActivate, flags.Command)
Expand All @@ -85,8 +85,8 @@ func TestHandleActivateCommandWithENV(t *testing.T) {

args := []string{"./rpc", "activate", "-u", "wss://localhost"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.Success, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.Success, rc)
assert.Equal(t, "wss://localhost", flags.URL)
assert.Equal(t, "envprofile", flags.Profile)
assert.Equal(t, utils.CommandActivate, flags.Command)
Expand All @@ -97,47 +97,47 @@ func TestHandleActivateCommandWithENV(t *testing.T) {
func TestHandleActivateCommandIncorrectCommandLineParameters(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-x"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.IncorrectCommandLineParameters, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.IncorrectCommandLineParameters, rc)
}

func TestHandleActivateCommandNoProfile(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectProfile, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectProfile, rc)
assert.Equal(t, "wss://localhost", flags.URL)
}

func TestHandleActivateCommandNoProxy(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-p"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingProxyAddressAndPort, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingProxyAddressAndPort, rc)
assert.Equal(t, "wss://localhost", flags.URL)
}

func TestHandleActivateCommandNoHostname(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-h"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingHostname, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingHostname, rc)
assert.Equal(t, "wss://localhost", flags.URL)
}

func TestHandleActivateCommandNoDNSSuffix(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-d"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingDNSSuffix, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingDNSSuffix, rc)
assert.Equal(t, "wss://localhost", flags.URL)
}

func TestHandleActivateCommandMissingProfile(t *testing.T) {
args := []string{"./rpc", "activate", "-u", "wss://localhost", "-profile"}
flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectProfile, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectProfile, rc)
assert.Equal(t, "wss://localhost", flags.URL)
}

Expand All @@ -152,16 +152,16 @@ func TestHandleActivateCommandNoURL(t *testing.T) {
args := []string{"./rpc", "activate", "-profile", "profileName"}

flags := NewFlags(args)
resultCode := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectURL, resultCode)
rc := flags.ParseFlags()
assert.Equal(t, utils.MissingOrIncorrectURL, rc)
assert.Equal(t, "profileName", flags.Profile)
}

func TestHandleActivateCommandLocal(t *testing.T) {

tests := map[string]struct {
cmdLine string
wantResult int
wantResult utils.ReturnCode
}{
"should fail with both URL and local": {
cmdLine: "./rpc activate -local -u wss://localhost",
Expand Down
Loading

0 comments on commit 874514b

Please sign in to comment.