59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package openwrt
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const FirmwareFileTemplate = "%FIRMWARE%"
|
|
|
|
type FirmwareVersion interface {
|
|
FirmwareVersion(context.Context) (string, error)
|
|
}
|
|
|
|
type SysUpgradeOptions struct {
|
|
Command string
|
|
Args []string
|
|
FirmwareVersion FirmwareVersion
|
|
Client *http.Client
|
|
}
|
|
|
|
func defaultSysUpgradeOptions() *SysUpgradeOptions {
|
|
return &SysUpgradeOptions{
|
|
Command: `echo`,
|
|
Args: []string{`[DUMMY UPGRADE]`, FirmwareFileTemplate},
|
|
Client: &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
},
|
|
FirmwareVersion: NewShellFirmwareVersion(`echo`, "0.0.0-dummy"),
|
|
}
|
|
}
|
|
|
|
type SysUpgradeOptionFunc func(*SysUpgradeOptions)
|
|
|
|
func WithSysUpgradeCommand(command string, args ...string) SysUpgradeOptionFunc {
|
|
return func(opts *SysUpgradeOptions) {
|
|
opts.Command = command
|
|
opts.Args = args
|
|
}
|
|
}
|
|
|
|
func WithSysUpgradeFirmwareVersion(firmwareVersion FirmwareVersion) SysUpgradeOptionFunc {
|
|
return func(opts *SysUpgradeOptions) {
|
|
opts.FirmwareVersion = firmwareVersion
|
|
}
|
|
}
|
|
|
|
func WithSysUpgradeShellFirmwareVersion(command string, args ...string) SysUpgradeOptionFunc {
|
|
return func(opts *SysUpgradeOptions) {
|
|
opts.FirmwareVersion = NewShellFirmwareVersion(command, args...)
|
|
}
|
|
}
|
|
|
|
func WithSysUpgradeClient(client *http.Client) SysUpgradeOptionFunc {
|
|
return func(opts *SysUpgradeOptions) {
|
|
opts.Client = client
|
|
}
|
|
}
|