Skip to content

Commit 9c6b1f9

Browse files
committed
refactor: default constants and validation
1 parent 22deb42 commit 9c6b1f9

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

cmd/agent_smith/config.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ type fetchConfigurationResponse struct {
2222
}
2323

2424
func validateConfiguration(device agent.Device) error {
25-
required := map[string]string{
26-
"device_id": device.DeviceId,
27-
"rewst_engine_host": device.RewstEngineHost,
28-
"shared_access_key": device.SharedAccessKey,
29-
"azure_iot_hub_host": device.AzureIotHubHost,
30-
}
31-
for field, value := range required {
32-
if value == "" {
33-
return fmt.Errorf("missing required field: %s", field)
34-
}
25+
if device.DeviceId == "" {
26+
return fmt.Errorf("missing required field: device_id")
27+
}
28+
if device.RewstEngineHost == "" {
29+
return fmt.Errorf("missing required field: rewst_engine_host")
30+
}
31+
if device.SharedAccessKey == "" {
32+
return fmt.Errorf("missing required field: shared_access_key")
33+
}
34+
if device.AzureIotHubHost == "" {
35+
return fmt.Errorf("missing required field: azure_iot_hub_host")
3536
}
3637
return nil
3738
}

cmd/agent_smith/service.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ func (svc *serviceContext) Execute(
112112
},
113113
)
114114
runner := agent.NewAutoUpdateRunner(
115-
logger, updater, agent.DefaultUpdateInterval(), agent.DefaultMaxRetries(), agent.DefaultBaseBackoff(),
115+
logger,
116+
updater,
117+
agent.DefaultUpdateInterval(),
118+
agent.DefaultMaxRetries(),
119+
agent.DefaultBaseBackoff(),
116120
)
117121
runner.Start()
118122
defer runner.Stop()

internal/agent/updater.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ var baseBackoffStr = ""
4444
// Example: -ldflags "-X github.com/RewstApp/agent-smith-go/internal/agent.maxRetriesStr=3"
4545
var maxRetriesStr = ""
4646

47+
const (
48+
defaultUpdateInterval = 48 * time.Hour
49+
defaultBaseBackoff = 5 * time.Minute
50+
defaultMaxRetries = 5
51+
)
52+
4753
// DefaultUpdateInterval returns the auto-update check interval.
4854
// Uses updateIntervalStr if set via ldflags, otherwise defaults to 48 hours.
4955
func DefaultUpdateInterval() time.Duration {
@@ -52,7 +58,7 @@ func DefaultUpdateInterval() time.Duration {
5258
return d
5359
}
5460
}
55-
return 48 * time.Hour
61+
return defaultUpdateInterval
5662
}
5763

5864
// DefaultBaseBackoff returns the base backoff duration for update retries.
@@ -63,7 +69,7 @@ func DefaultBaseBackoff() time.Duration {
6369
return d
6470
}
6571
}
66-
return 5 * time.Minute
72+
return defaultBaseBackoff
6773
}
6874

6975
// DefaultMaxRetries returns the maximum number of update retry attempts.
@@ -75,7 +81,7 @@ func DefaultMaxRetries() int {
7581
return n
7682
}
7783
}
78-
return 5
84+
return defaultMaxRetries
7985
}
8086

8187
type RunCommandFunc = func(path string, args []string) error

0 commit comments

Comments
 (0)