From 75931a372339459bcdc278f86dc82671795e3449 Mon Sep 17 00:00:00 2001 From: Bhoopesh Date: Wed, 19 Jun 2024 19:43:24 +0530 Subject: [PATCH] fix: lint, refactor Signed-off-by: Bhoopesh --- sztp-agent/pkg/secureagent/daemon.go | 76 ++++++++++++----------- sztp-agent/pkg/secureagent/daemon_test.go | 8 +-- sztp-agent/pkg/secureagent/utils.go | 5 +- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index b1c66efb..00723b5a 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -56,54 +56,56 @@ func (a *Agent) RunCommandDaemon() error { continue } log.Println("[INFO] Trying interface: ", iface.Name) - err = a.getBootstrapURL(iface.Name) - if err != nil { - log.Println("[ERROR] Failed to get the Bootstrap URL: ", err) - continue - } - err = a.doRequestBootstrapServerOnboardingInfo() - if err != nil { - log.Println("[ERROR] Failed during onboarding info request: ", err) - continue - } - err = a.doHandleBootstrapRedirect() - if err != nil { - log.Println("[ERROR] Failed during bootstrap redirect handling: ", err) - continue - } - err = a.downloadAndValidateImage() - if err != nil { - log.Println("[ERROR] Failed during image download and validation: ", err) - continue - } - err = a.copyConfigurationFile() - if err != nil { - log.Println("[ERROR] Failed during configuration file copy: ", err) - continue - } - err = a.launchScriptsConfiguration(PRE) - if err != nil { - log.Println("[ERROR] Failed during pre-configuration script execution: ", err) - continue - } - err = a.launchScriptsConfiguration(POST) - if err != nil { - log.Println("[ERROR] Failed during post-configuration script execution: ", err) - continue - } - err = a.doReportProgress(ProgressTypeBootstrapComplete) + + err := performBootstrapSequence(a, iface.Name) if err != nil { - log.Println("[ERROR] Failed during report: ", err) log.Println("[INFO] Retrying in 5 seconds") time.Sleep(5 * time.Second) continue } + log.Println("[INFO] Done") return nil } } } +func performBootstrapSequence(a *Agent, ifaceName string) error { + err := a.getBootstrapURL(ifaceName) + if err != nil { + return err + } + err = a.doRequestBootstrapServerOnboardingInfo() + if err != nil { + return err + } + err = a.doHandleBootstrapRedirect() + if err != nil { + return err + } + err = a.downloadAndValidateImage() + if err != nil { + return err + } + err = a.copyConfigurationFile() + if err != nil { + return err + } + err = a.launchScriptsConfiguration(PRE) + if err != nil { + return err + } + err = a.launchScriptsConfiguration(POST) + if err != nil { + return err + } + err = a.doReportProgress(ProgressTypeBootstrapComplete) + if err != nil { + return err + } + return nil +} + func (a *Agent) getBootstrapURL(iface string) error { log.Println("[INFO] Get the Bootstrap URL from DHCP client") var line string diff --git a/sztp-agent/pkg/secureagent/daemon_test.go b/sztp-agent/pkg/secureagent/daemon_test.go index 73a66d62..3963b7d6 100644 --- a/sztp-agent/pkg/secureagent/daemon_test.go +++ b/sztp-agent/pkg/secureagent/daemon_test.go @@ -16,7 +16,7 @@ import ( //nolint:funlen func TestAgent_getBootstrapURL(t *testing.T) { - dhcpTestFileOK := "/tmp/test.dhcp" + const dhcpTestFileOK = "/tmp/test.dhcp" createTempTestFile(dhcpTestFileOK, true) type fields struct { @@ -29,7 +29,7 @@ func TestAgent_getBootstrapURL(t *testing.T) { ContentTypeReq string InputJSONContent string DhcpLeasesFile string - Iface string + Iface string } tests := []struct { name string @@ -48,7 +48,7 @@ func TestAgent_getBootstrapURL(t *testing.T) { ContentTypeReq: CONTENT_TYPE_YANG, InputJSONContent: "", DhcpLeasesFile: dhcpTestFileOK, - Iface: "eth0", + Iface: "eth0", }, wantErr: false, }, @@ -64,7 +64,7 @@ func TestAgent_getBootstrapURL(t *testing.T) { ContentTypeReq: CONTENT_TYPE_YANG, InputJSONContent: "", DhcpLeasesFile: "/kk/kk", - Iface: "eth0", + Iface: "eth0", }, wantErr: true, }, diff --git a/sztp-agent/pkg/secureagent/utils.go b/sztp-agent/pkg/secureagent/utils.go index 3a763881..8fdab835 100644 --- a/sztp-agent/pkg/secureagent/utils.go +++ b/sztp-agent/pkg/secureagent/utils.go @@ -43,11 +43,12 @@ func linesInFileContains(file string, substr string) string { // Auxiliar function to get lines from file matching with the substr and given network interface func linesInFileContainsWithInterface(file string, substr string, iface string) string { + // nolint:gosec f, _ := os.Open(file) scanner := bufio.NewScanner(f) - var res string = "" - var isCorrectInterfaceBlock bool = false + var res string + var isCorrectInterfaceBlock bool for scanner.Scan() { line := scanner.Text()