diff --git a/resource/addr.go b/resource/addr.go index 2347583e9..18cf0060c 100644 --- a/resource/addr.go +++ b/resource/addr.go @@ -58,7 +58,7 @@ func (a *Addr) Validate(sys *system.System) []TestResult { a.Timeout = 500 } - sysAddr := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress}) + sysAddr, _ := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress}) var results []TestResult results = append(results, ValidateValue(a, "reachable", a.Reachable, sysAddr.Reachable, skip)) diff --git a/resource/command.go b/resource/command.go index 79a1671e7..0c4db8b15 100644 --- a/resource/command.go +++ b/resource/command.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "log" "strings" "time" @@ -13,15 +14,15 @@ import ( ) type Command struct { - Title string `json:"title,omitempty" yaml:"title,omitempty"` - Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"` - id string `json:"-" yaml:"-"` - Exec string `json:"exec,omitempty" yaml:"exec,omitempty"` - ExitStatus matcher `json:"exit-status" yaml:"exit-status"` - Stdout matcher `json:"stdout" yaml:"stdout"` - Stderr matcher `json:"stderr" yaml:"stderr"` - Timeout int `json:"timeout" yaml:"timeout"` - Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"` + Title string `json:"title,omitempty" yaml:"title,omitempty"` + Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"` + id string `json:"-" yaml:"-"` + Exec util.ExecCommand `json:"exec,omitempty" yaml:"exec,omitempty"` + ExitStatus matcher `json:"exit-status" yaml:"exit-status"` + Stdout matcher `json:"stdout" yaml:"stdout"` + Stderr matcher `json:"stderr" yaml:"stderr"` + Timeout int `json:"timeout" yaml:"timeout"` + Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"` } const ( @@ -41,11 +42,14 @@ func (c *Command) TypeName() string { return CommandResourceName } func (c *Command) GetTitle() string { return c.Title } func (c *Command) GetMeta() meta { return c.Meta } -func (c *Command) GetExec() string { - if c.Exec != "" { - return c.Exec +func (c *Command) GetExec() interface{} { + if c.Exec.CmdStr != "" { + return c.Exec.CmdStr + } else if len(c.Exec.CmdSlice) > 0 { + return c.Exec.CmdSlice + } else { + return c.id } - return c.id } func (c *Command) Validate(sys *system.System) []TestResult { @@ -57,24 +61,48 @@ func (c *Command) Validate(sys *system.System) []TestResult { } var results []TestResult - sysCommand := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond}) - - cExitStatus := deprecateAtoI(c.ExitStatus, fmt.Sprintf("%s: command.exit-status", c.ID())) - results = append(results, ValidateValue(c, "exit-status", cExitStatus, sysCommand.ExitStatus, skip)) - if isSet(c.Stdout) { - results = append(results, ValidateValue(c, "stdout", c.Stdout, sysCommand.Stdout, skip)) - } - if isSet(c.Stderr) { - results = append(results, ValidateValue(c, "stderr", c.Stderr, sysCommand.Stderr, skip)) + sysCommand, err := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond}) + if err != nil { + log.Printf("[ERROR] Could not create new command: %v", err) + startTime := time.Now() + results = append( + results, + TestResult{ + Result: FAIL, + ResourceType: "Command", + ResourceId: c.id, + Title: c.Title, + Meta: c.Meta, + Property: "type", + Err: toValidateError(err), + StartTime: startTime, + EndTime: startTime, + Duration: startTime.Sub(startTime), + }, + ) + } else { + cExitStatus := deprecateAtoI(c.ExitStatus, fmt.Sprintf("%s: command.exit-status", c.ID())) + results = append(results, ValidateValue(c, "exit-status", cExitStatus, sysCommand.ExitStatus, skip)) + if isSet(c.Stdout) { + results = append(results, ValidateValue(c, "stdout", c.Stdout, sysCommand.Stdout, skip)) + } + if isSet(c.Stderr) { + results = append(results, ValidateValue(c, "stderr", c.Stderr, sysCommand.Stderr, skip)) + } } return results } func NewCommand(sysCommand system.Command, config util.Config) (*Command, error) { - command := sysCommand.Command() + var id string + if sysCommand.Command().CmdStr != "" { + id = sysCommand.Command().CmdStr + } else { + id = sysCommand.Command().CmdSlice[0] + } exitStatus, err := sysCommand.ExitStatus() c := &Command{ - id: command, + id: id, ExitStatus: exitStatus, Stdout: "", Stderr: "", diff --git a/resource/dns.go b/resource/dns.go index bd3274d99..31fbd9b06 100644 --- a/resource/dns.go +++ b/resource/dns.go @@ -58,7 +58,7 @@ func (d *DNS) Validate(sys *system.System) []TestResult { d.Timeout = 500 } - sysDNS := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server}) + sysDNS, _ := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server}) var results []TestResult // Backwards compatibility hack for now diff --git a/resource/file.go b/resource/file.go index dcb7f5a0e..85a8202f2 100644 --- a/resource/file.go +++ b/resource/file.go @@ -61,7 +61,7 @@ func (f *File) GetPath() string { func (f *File) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", f.ID()) skip := f.Skip - sysFile := sys.NewFile(ctx, f.GetPath(), sys, util.Config{}) + sysFile, _ := sys.NewFile(ctx, f.GetPath(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(f, "exists", f.Exists, sysFile.Exists, skip)) diff --git a/resource/group.go b/resource/group.go index 7ca928108..b391e6a30 100644 --- a/resource/group.go +++ b/resource/group.go @@ -49,7 +49,7 @@ func (g *Group) GetGroupname() string { func (g *Group) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", g.ID()) skip := g.Skip - sysgroup := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{}) + sysgroup, _ := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(g, "exists", g.Exists, sysgroup.Exists, skip)) diff --git a/resource/http.go b/resource/http.go index b9a1006de..00b9a7073 100644 --- a/resource/http.go +++ b/resource/http.go @@ -68,7 +68,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult { if u.Timeout == 0 { u.Timeout = 5000 } - sysHTTP := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{ + sysHTTP, _ := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{ AllowInsecure: u.AllowInsecure, CAFile: u.CAFile, CertFile: u.CertFile, diff --git a/resource/interface.go b/resource/interface.go index 14921d133..c09ad65c3 100644 --- a/resource/interface.go +++ b/resource/interface.go @@ -52,7 +52,7 @@ func (i *Interface) GetName() string { func (i *Interface) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", i.ID()) skip := i.Skip - sysInterface := sys.NewInterface(ctx, i.GetName(), sys, util.Config{}) + sysInterface, _ := sys.NewInterface(ctx, i.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(i, "exists", i.Exists, sysInterface.Exists, skip)) diff --git a/resource/kernel_param.go b/resource/kernel_param.go index 494597a96..f18c12372 100644 --- a/resource/kernel_param.go +++ b/resource/kernel_param.go @@ -52,7 +52,7 @@ func (k *KernelParam) GetName() string { func (k *KernelParam) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", k.ID()) skip := k.Skip - sysKernelParam := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{}) + sysKernelParam, _ := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(k, "value", k.Value, sysKernelParam.Value, skip)) diff --git a/resource/mount.go b/resource/mount.go index 895ebdff0..3de8d5f30 100644 --- a/resource/mount.go +++ b/resource/mount.go @@ -55,7 +55,7 @@ func (m *Mount) GetMountPoint() string { func (m *Mount) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", m.ID()) skip := m.Skip - sysMount := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{}) + sysMount, _ := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(m, "exists", m.Exists, sysMount.Exists, skip)) diff --git a/resource/package.go b/resource/package.go index f8afffcb5..3ae43a5d4 100644 --- a/resource/package.go +++ b/resource/package.go @@ -49,7 +49,7 @@ func (p *Package) GetName() string { func (p *Package) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysPkg := sys.NewPackage(ctx, p.GetName(), sys, util.Config{}) + sysPkg, _ := sys.NewPackage(ctx, p.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "installed", p.Installed, sysPkg.Installed, skip)) diff --git a/resource/port.go b/resource/port.go index 70f72ae93..678688628 100644 --- a/resource/port.go +++ b/resource/port.go @@ -49,7 +49,7 @@ func (p *Port) GetPort() string { func (p *Port) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysPort := sys.NewPort(ctx, p.GetPort(), sys, util.Config{}) + sysPort, _ := sys.NewPort(ctx, p.GetPort(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "listening", p.Listening, sysPort.Listening, skip)) diff --git a/resource/process.go b/resource/process.go index 9012760e9..6ab19e565 100644 --- a/resource/process.go +++ b/resource/process.go @@ -48,7 +48,7 @@ func (p *Process) GetComm() string { func (p *Process) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysProcess := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{}) + sysProcess, _ := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "running", p.Running, sysProcess.Running, skip)) diff --git a/resource/resource_list.go b/resource/resource_list.go index e3aedb633..af66cbd4d 100644 --- a/resource/resource_list.go +++ b/resource/resource_list.go @@ -19,7 +19,7 @@ type AddrMap map[string]*Addr func (r AddrMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Addr, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewAddr(ctx, sr, sys, config) + sysres, _ := sys.NewAddr(ctx, sr, sys, config) res, err := NewAddr(sysres, config) if err != nil { return nil, err @@ -34,7 +34,7 @@ func (r AddrMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r AddrMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Addr, system.Addr, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewAddr(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewAddr(ctx, sr, sys, util.Config{}) res, err := NewAddr(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -120,7 +120,7 @@ type CommandMap map[string]*Command func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Command, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewCommand(ctx, sr, sys, config) + sysres, _ := sys.NewCommand(ctx, sr, sys, config) res, err := NewCommand(sysres, config) if err != nil { return nil, err @@ -135,7 +135,7 @@ func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util func (r CommandMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Command, system.Command, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewCommand(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewCommand(ctx, sr, sys, util.Config{}) res, err := NewCommand(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -221,7 +221,7 @@ type DNSMap map[string]*DNS func (r DNSMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*DNS, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewDNS(ctx, sr, sys, config) + sysres, _ := sys.NewDNS(ctx, sr, sys, config) res, err := NewDNS(sysres, config) if err != nil { return nil, err @@ -236,7 +236,7 @@ func (r DNSMap) AppendSysResource(sr string, sys *system.System, config util.Con func (r DNSMap) AppendSysResourceIfExists(sr string, sys *system.System) (*DNS, system.DNS, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewDNS(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewDNS(ctx, sr, sys, util.Config{}) res, err := NewDNS(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -322,7 +322,7 @@ type FileMap map[string]*File func (r FileMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*File, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewFile(ctx, sr, sys, config) + sysres, _ := sys.NewFile(ctx, sr, sys, config) res, err := NewFile(sysres, config) if err != nil { return nil, err @@ -337,7 +337,7 @@ func (r FileMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r FileMap) AppendSysResourceIfExists(sr string, sys *system.System) (*File, system.File, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewFile(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewFile(ctx, sr, sys, util.Config{}) res, err := NewFile(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -423,7 +423,7 @@ type GossfileMap map[string]*Gossfile func (r GossfileMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Gossfile, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGossfile(ctx, sr, sys, config) + sysres, _ := sys.NewGossfile(ctx, sr, sys, config) res, err := NewGossfile(sysres, config) if err != nil { return nil, err @@ -438,7 +438,7 @@ func (r GossfileMap) AppendSysResource(sr string, sys *system.System, config uti func (r GossfileMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Gossfile, system.Gossfile, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGossfile(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewGossfile(ctx, sr, sys, util.Config{}) res, err := NewGossfile(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -524,7 +524,7 @@ type GroupMap map[string]*Group func (r GroupMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Group, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGroup(ctx, sr, sys, config) + sysres, _ := sys.NewGroup(ctx, sr, sys, config) res, err := NewGroup(sysres, config) if err != nil { return nil, err @@ -539,7 +539,7 @@ func (r GroupMap) AppendSysResource(sr string, sys *system.System, config util.C func (r GroupMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Group, system.Group, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGroup(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewGroup(ctx, sr, sys, util.Config{}) res, err := NewGroup(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -625,7 +625,7 @@ type PackageMap map[string]*Package func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Package, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPackage(ctx, sr, sys, config) + sysres, _ := sys.NewPackage(ctx, sr, sys, config) res, err := NewPackage(sysres, config) if err != nil { return nil, err @@ -640,7 +640,7 @@ func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util func (r PackageMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Package, system.Package, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPackage(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewPackage(ctx, sr, sys, util.Config{}) res, err := NewPackage(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -726,7 +726,7 @@ type PortMap map[string]*Port func (r PortMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Port, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPort(ctx, sr, sys, config) + sysres, _ := sys.NewPort(ctx, sr, sys, config) res, err := NewPort(sysres, config) if err != nil { return nil, err @@ -741,7 +741,7 @@ func (r PortMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r PortMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Port, system.Port, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPort(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewPort(ctx, sr, sys, util.Config{}) res, err := NewPort(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -827,7 +827,7 @@ type ProcessMap map[string]*Process func (r ProcessMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Process, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewProcess(ctx, sr, sys, config) + sysres, _ := sys.NewProcess(ctx, sr, sys, config) res, err := NewProcess(sysres, config) if err != nil { return nil, err @@ -842,7 +842,7 @@ func (r ProcessMap) AppendSysResource(sr string, sys *system.System, config util func (r ProcessMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Process, system.Process, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewProcess(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewProcess(ctx, sr, sys, util.Config{}) res, err := NewProcess(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -928,7 +928,7 @@ type ServiceMap map[string]*Service func (r ServiceMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Service, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewService(ctx, sr, sys, config) + sysres, _ := sys.NewService(ctx, sr, sys, config) res, err := NewService(sysres, config) if err != nil { return nil, err @@ -943,7 +943,7 @@ func (r ServiceMap) AppendSysResource(sr string, sys *system.System, config util func (r ServiceMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Service, system.Service, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewService(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewService(ctx, sr, sys, util.Config{}) res, err := NewService(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1029,7 +1029,7 @@ type UserMap map[string]*User func (r UserMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*User, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewUser(ctx, sr, sys, config) + sysres, _ := sys.NewUser(ctx, sr, sys, config) res, err := NewUser(sysres, config) if err != nil { return nil, err @@ -1044,7 +1044,7 @@ func (r UserMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r UserMap) AppendSysResourceIfExists(sr string, sys *system.System) (*User, system.User, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewUser(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewUser(ctx, sr, sys, util.Config{}) res, err := NewUser(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1130,7 +1130,7 @@ type KernelParamMap map[string]*KernelParam func (r KernelParamMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*KernelParam, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewKernelParam(ctx, sr, sys, config) + sysres, _ := sys.NewKernelParam(ctx, sr, sys, config) res, err := NewKernelParam(sysres, config) if err != nil { return nil, err @@ -1145,7 +1145,7 @@ func (r KernelParamMap) AppendSysResource(sr string, sys *system.System, config func (r KernelParamMap) AppendSysResourceIfExists(sr string, sys *system.System) (*KernelParam, system.KernelParam, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewKernelParam(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewKernelParam(ctx, sr, sys, util.Config{}) res, err := NewKernelParam(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1231,7 +1231,7 @@ type MountMap map[string]*Mount func (r MountMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Mount, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewMount(ctx, sr, sys, config) + sysres, _ := sys.NewMount(ctx, sr, sys, config) res, err := NewMount(sysres, config) if err != nil { return nil, err @@ -1246,7 +1246,7 @@ func (r MountMap) AppendSysResource(sr string, sys *system.System, config util.C func (r MountMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Mount, system.Mount, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewMount(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewMount(ctx, sr, sys, util.Config{}) res, err := NewMount(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1332,7 +1332,7 @@ type InterfaceMap map[string]*Interface func (r InterfaceMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Interface, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewInterface(ctx, sr, sys, config) + sysres, _ := sys.NewInterface(ctx, sr, sys, config) res, err := NewInterface(sysres, config) if err != nil { return nil, err @@ -1347,7 +1347,7 @@ func (r InterfaceMap) AppendSysResource(sr string, sys *system.System, config ut func (r InterfaceMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Interface, system.Interface, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewInterface(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewInterface(ctx, sr, sys, util.Config{}) res, err := NewInterface(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1433,7 +1433,7 @@ type HTTPMap map[string]*HTTP func (r HTTPMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*HTTP, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewHTTP(ctx, sr, sys, config) + sysres, _ := sys.NewHTTP(ctx, sr, sys, config) res, err := NewHTTP(sysres, config) if err != nil { return nil, err @@ -1448,7 +1448,7 @@ func (r HTTPMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r HTTPMap) AppendSysResourceIfExists(sr string, sys *system.System) (*HTTP, system.HTTP, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewHTTP(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewHTTP(ctx, sr, sys, util.Config{}) res, err := NewHTTP(sysres, util.Config{}) if err != nil { return nil, nil, false, err diff --git a/resource/resource_list_genny.go b/resource/resource_list_genny.go index a483bc4ea..3a5178c07 100644 --- a/resource/resource_list_genny.go +++ b/resource/resource_list_genny.go @@ -27,7 +27,7 @@ type ResourceTypeMap map[string]*ResourceType func (r ResourceTypeMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*ResourceType, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewResourceType(ctx, sr, sys, config) + sysres, _ := sys.NewResourceType(ctx, sr, sys, config) res, err := NewResourceType(sysres, config) if err != nil { return nil, err @@ -42,7 +42,7 @@ func (r ResourceTypeMap) AppendSysResource(sr string, sys *system.System, config func (r ResourceTypeMap) AppendSysResourceIfExists(sr string, sys *system.System) (*ResourceType, system.ResourceType, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewResourceType(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewResourceType(ctx, sr, sys, util.Config{}) res, err := NewResourceType(sysres, util.Config{}) if err != nil { return nil, nil, false, err diff --git a/resource/service.go b/resource/service.go index 2285c3e1f..392fc94f8 100644 --- a/resource/service.go +++ b/resource/service.go @@ -50,7 +50,7 @@ func (s *Service) GetName() string { func (s *Service) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", s.ID()) skip := s.Skip - sysservice := sys.NewService(ctx, s.GetName(), sys, util.Config{}) + sysservice, _ := sys.NewService(ctx, s.GetName(), sys, util.Config{}) var results []TestResult if s.Enabled != nil { diff --git a/resource/user.go b/resource/user.go index 3895f8067..7211cd45e 100644 --- a/resource/user.go +++ b/resource/user.go @@ -53,7 +53,7 @@ func (u *User) GetUsername() string { func (u *User) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", u.ID()) skip := u.Skip - sysuser := sys.NewUser(ctx, u.GetUsername(), sys, util.Config{}) + sysuser, _ := sys.NewUser(ctx, u.GetUsername(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(u, "exists", u.Exists, sysuser.Exists, skip)) diff --git a/system/addr.go b/system/addr.go index 35ed063df..8f1f32d82 100644 --- a/system/addr.go +++ b/system/addr.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "net" "strings" "time" @@ -21,7 +22,15 @@ type DefAddr struct { Timeout int } -func NewDefAddr(_ context.Context, address string, system *System, config util.Config) Addr { +func NewDefAddr(_ context.Context, address interface{}, system *System, config util.Config) (Addr, error) { + strAddress, ok := address.(string) + if !ok { + return nil, fmt.Errorf("address must be of type string") + } + return newDefAddr(nil, strAddress, system, config), nil +} + +func newDefAddr(_ context.Context, address string, system *System, config util.Config) Addr { addr := normalizeAddress(address) return &DefAddr{ address: addr, diff --git a/system/command.go b/system/command.go index aea7ade9f..2020a379a 100644 --- a/system/command.go +++ b/system/command.go @@ -12,7 +12,7 @@ import ( ) type Command interface { - Command() string + Command() util.ExecCommand Exists() (bool, error) ExitStatus() (int, error) Stdout() (io.Reader, error) @@ -21,7 +21,7 @@ type Command interface { type DefCommand struct { Ctx context.Context - command string + command util.ExecCommand exitStatus int stdout io.Reader stderr io.Reader @@ -30,10 +30,29 @@ type DefCommand struct { err error } -func NewDefCommand(ctx context.Context, command string, system *System, config util.Config) Command { +func NewDefCommand(ctx context.Context, command interface{}, system *System, config util.Config) (Command, error) { + switch cmd := command.(type) { + case string: + return newDefCommand(ctx, cmd, system, config), nil + case []string: + return newDefExecCommand(ctx, cmd, system, config), nil + default: + return nil, fmt.Errorf("command type must be either string or []string") + } +} + +func newDefCommand(ctx context.Context, command string, system *System, config util.Config) Command { + return &DefCommand{ + Ctx: ctx, + command: util.ExecCommand{CmdStr: command}, + Timeout: config.TimeOutMilliSeconds(), + } +} + +func newDefExecCommand(ctx context.Context, command []string, system *System, config util.Config) Command { return &DefCommand{ Ctx: ctx, - command: command, + command: util.ExecCommand{CmdSlice: command}, Timeout: config.TimeOutMilliSeconds(), } } @@ -44,7 +63,12 @@ func (c *DefCommand) setup() error { } c.loaded = true - cmd := commandWrapper(c.command) + var cmd *util.Command + if c.command.CmdStr != "" { + cmd = commandWrapper(c.command.CmdStr) + } else { + cmd = util.NewCommand(c.command.CmdSlice[0], c.command.CmdSlice[1:]...) + } err := runCommand(cmd, c.Timeout) // We don't care about ExitError since it's covered by status @@ -63,7 +87,7 @@ func (c *DefCommand) setup() error { return c.err } -func (c *DefCommand) Command() string { +func (c *DefCommand) Command() util.ExecCommand { return c.command } diff --git a/system/dns.go b/system/dns.go index a73b4229e..27b49f92c 100644 --- a/system/dns.go +++ b/system/dns.go @@ -34,7 +34,15 @@ type DefDNS struct { qtype string } -func NewDefDNS(_ context.Context, host string, system *System, config util.Config) DNS { +func NewDefDNS(_ context.Context, host interface{}, system *System, config util.Config) (DNS, error) { + strHost, ok := host.(string) + if !ok { + return nil, fmt.Errorf("host must be of type string") + } + return newDefDNS(nil, strHost, system, config), nil +} + +func newDefDNS(_ context.Context, host string, system *System, config util.Config) DNS { var h string var t string diff --git a/system/file.go b/system/file.go index 736c25ee8..3869d3e0b 100644 --- a/system/file.go +++ b/system/file.go @@ -48,7 +48,15 @@ type DefFile struct { err error } -func NewDefFile(_ context.Context, path string, system *System, config util.Config) File { +func NewDefFile(_ context.Context, path interface{}, system *System, config util.Config) (File, error) { + strPath, ok := path.(string) + if !ok { + return nil, fmt.Errorf("path must be of type string") + } + return newDefFile(nil, strPath, system, config), nil +} + +func newDefFile(_ context.Context, path string, system *System, config util.Config) File { var err error if !strings.HasPrefix(path, "~") { path, err = filepath.Abs(path) diff --git a/system/gossfile.go b/system/gossfile.go index 1af67852b..abfa05d59 100644 --- a/system/gossfile.go +++ b/system/gossfile.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/goss-org/goss/util" ) @@ -24,6 +25,14 @@ func (g *DefGossfile) Exists() (bool, error) { return false, nil } -func NewDefGossfile(_ context.Context, path string, system *System, config util.Config) Gossfile { +func NewDefGossfile(_ context.Context, path interface{}, system *System, config util.Config) (Gossfile, error) { + strPath, ok := path.(string) + if !ok { + return nil, fmt.Errorf("path must be of type string") + } + return newDefGossfile(nil, strPath, system, config), nil +} + +func newDefGossfile(_ context.Context, path string, system *System, config util.Config) Gossfile { return &DefGossfile{path: path} } diff --git a/system/group.go b/system/group.go index 9687969f9..cfc1c828a 100644 --- a/system/group.go +++ b/system/group.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "os/user" "strconv" @@ -18,7 +19,15 @@ type DefGroup struct { groupname string } -func NewDefGroup(_ context.Context, groupname string, system *System, config util.Config) Group { +func NewDefGroup(_ context.Context, groupname interface{}, system *System, config util.Config) (Group, error) { + strGroupname, ok := groupname.(string) + if !ok { + return nil, fmt.Errorf("groupname must be of type string") + } + return newDefGroup(nil, strGroupname, system, config), nil +} + +func newDefGroup(_ context.Context, groupname string, system *System, config util.Config) Group { return &DefGroup{groupname: groupname} } diff --git a/system/http.go b/system/http.go index cdbf589cf..96b8f1c21 100644 --- a/system/http.go +++ b/system/http.go @@ -45,7 +45,15 @@ type DefHTTP struct { Proxy string } -func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { +func NewDefHTTP(_ context.Context, httpStr interface{}, system *System, config util.Config) (HTTP, error) { + strHttpStr, ok := httpStr.(string) + if !ok { + return nil, fmt.Errorf("httpStr must be of type string") + } + return newDefHTTP(nil, strHttpStr, system, config), nil +} + +func newDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { headers := http.Header{} for _, r := range config.RequestHeader { str := strings.SplitN(r, ": ", 2) diff --git a/system/interface.go b/system/interface.go index 83585f398..036abc59d 100644 --- a/system/interface.go +++ b/system/interface.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "net" "github.com/goss-org/goss/util" @@ -22,7 +23,15 @@ type DefInterface struct { err error } -func NewDefInterface(_ context.Context, name string, systei *System, config util.Config) Interface { +func NewDefInterface(_ context.Context, name interface{}, system *System, config util.Config) (Interface, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newDefInterface(nil, strName, system, config), nil +} + +func newDefInterface(_ context.Context, name string, systei *System, config util.Config) Interface { return &DefInterface{ name: name, } diff --git a/system/kernel_param.go b/system/kernel_param.go index 867e08f65..c83ffa8a5 100644 --- a/system/kernel_param.go +++ b/system/kernel_param.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/achanda/go-sysctl" "github.com/goss-org/goss/util" @@ -18,7 +19,15 @@ type DefKernelParam struct { value string } -func NewDefKernelParam(_ context.Context, key string, system *System, config util.Config) KernelParam { +func NewDefKernelParam(_ context.Context, key interface{}, system *System, config util.Config) (KernelParam, error) { + strKey, ok := key.(string) + if !ok { + return nil, fmt.Errorf("key must be of type string") + } + return newDefKernelParam(nil, strKey, system, config), nil +} + +func newDefKernelParam(_ context.Context, key string, system *System, config util.Config) KernelParam { return &DefKernelParam{ key: key, } diff --git a/system/mount.go b/system/mount.go index fb8902bb6..ba0d9fe37 100644 --- a/system/mount.go +++ b/system/mount.go @@ -29,7 +29,15 @@ type DefMount struct { err error } -func NewDefMount(_ context.Context, mountPoint string, system *System, config util.Config) Mount { +func NewDefMount(_ context.Context, mountPoint interface{}, system *System, config util.Config) (Mount, error) { + strMountPoint, ok := mountPoint.(string) + if !ok { + return nil, fmt.Errorf("mountPoint must be of type string") + } + return newDefMount(nil, strMountPoint, system, config), nil +} + +func newDefMount(_ context.Context, mountPoint string, system *System, config util.Config) Mount { return &DefMount{ mountPoint: mountPoint, } diff --git a/system/package.go b/system/package.go index 2944a5a25..813c56f21 100644 --- a/system/package.go +++ b/system/package.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "github.com/goss-org/goss/util" ) @@ -20,7 +21,15 @@ type NullPackage struct { name string } -func NewNullPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewNullPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newNullPackage(nil, strName, system, config), nil +} + +func newNullPackage(_ context.Context, name string, system *System, config util.Config) Package { return &NullPackage{name: name} } diff --git a/system/package_alpine.go b/system/package_alpine.go index 9aa883a1f..96119170c 100644 --- a/system/package_alpine.go +++ b/system/package_alpine.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type AlpinePackage struct { installed bool } -func NewAlpinePackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewAlpinePackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newAlpinePackage(nil, strName, system, config), nil +} + +func newAlpinePackage(_ context.Context, name string, system *System, config util.Config) Package { return &AlpinePackage{name: name} } diff --git a/system/package_deb.go b/system/package_deb.go index 9c04fb761..032918c66 100644 --- a/system/package_deb.go +++ b/system/package_deb.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type DebPackage struct { installed bool } -func NewDebPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewDebPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newDebPackage(nil, strName, system, config), nil +} + +func newDebPackage(_ context.Context, name string, system *System, config util.Config) Package { return &DebPackage{name: name} } diff --git a/system/package_pacman.go b/system/package_pacman.go index 459fab82d..b2bc05169 100644 --- a/system/package_pacman.go +++ b/system/package_pacman.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type PacmanPackage struct { installed bool } -func NewPacmanPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewPacmanPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newPacmanPackage(nil, strName, system, config), nil +} + +func newPacmanPackage(_ context.Context, name string, system *System, config util.Config) Package { return &PacmanPackage{name: name} } diff --git a/system/package_rpm.go b/system/package_rpm.go index 6da5a58cf..780f8b98b 100644 --- a/system/package_rpm.go +++ b/system/package_rpm.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type RpmPackage struct { installed bool } -func NewRpmPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewRpmPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newRpmPackage(nil, strName, system, config), nil +} + +func newRpmPackage(_ context.Context, name string, system *System, config util.Config) Package { return &RpmPackage{name: name} } diff --git a/system/port.go b/system/port.go index b5cda8fbc..61b9e9da4 100644 --- a/system/port.go +++ b/system/port.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "strconv" "strings" @@ -21,7 +22,15 @@ type DefPort struct { sysPorts map[string][]GOnetstat.Process } -func NewDefPort(_ context.Context, port string, system *System, config util.Config) Port { +func NewDefPort(_ context.Context, port interface{}, system *System, config util.Config) (Port, error) { + strPort, ok := port.(string) + if !ok { + return nil, fmt.Errorf("port must be of type string") + } + return newDefPort(nil, strPort, system, config), nil +} + +func newDefPort(_ context.Context, port string, system *System, config util.Config) Port { p := normalizePort(port) return &DefPort{ port: p, diff --git a/system/process.go b/system/process.go index 271fac7ad..0279de94f 100644 --- a/system/process.go +++ b/system/process.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/goss-org/go-ps" "github.com/goss-org/goss/util" @@ -20,7 +21,15 @@ type DefProcess struct { err error } -func NewDefProcess(_ context.Context, executable string, system *System, config util.Config) Process { +func NewDefProcess(_ context.Context, executable interface{}, system *System, config util.Config) (Process, error) { + strExecutable, ok := executable.(string) + if !ok { + return nil, fmt.Errorf("executable must be of type string") + } + return newDefProcess(nil, strExecutable, system, config), nil +} + +func newDefProcess(_ context.Context, executable string, system *System, config util.Config) Process { pmap, err := system.ProcMap() return &DefProcess{ executable: executable, diff --git a/system/service_init.go b/system/service_init.go index cf1536b7d..7a3051436 100644 --- a/system/service_init.go +++ b/system/service_init.go @@ -16,11 +16,27 @@ type ServiceInit struct { runlevel string } -func NewServiceInit(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceInit(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceInit(nil, strService, system, config), nil +} + +func newServiceInit(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceInit{service: service} } -func NewAlpineServiceInit(_ context.Context, service string, system *System, config util.Config) Service { +func NewAlpineServiceInit(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newAlpineServiceInit(nil, strService, system, config), nil +} + +func newAlpineServiceInit(_ context.Context, service string, system *System, config util.Config) Service { runlevel := config.RunLevel if runlevel == "" { runlevel = "sysinit" diff --git a/system/service_systemd.go b/system/service_systemd.go index 01d4a244a..e59f599a9 100644 --- a/system/service_systemd.go +++ b/system/service_systemd.go @@ -13,13 +13,29 @@ type ServiceSystemd struct { legacy bool } -func NewServiceSystemd(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceSystemd(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceSystemd(nil, strService, system, config), nil +} + +func newServiceSystemd(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceSystemd{ service: service, } } -func NewServiceSystemdLegacy(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceSystemdLegacy(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceSystemdLegacy(nil, strService, system, config), nil +} + +func newServiceSystemdLegacy(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceSystemd{ service: service, legacy: true, diff --git a/system/service_upstart.go b/system/service_upstart.go index 9c24e62fe..dd1ae3c58 100644 --- a/system/service_upstart.go +++ b/system/service_upstart.go @@ -18,7 +18,15 @@ type ServiceUpstart struct { var upstartEnabled = regexp.MustCompile(`^\s*start on`) var upstartDisabled = regexp.MustCompile(`^manual`) -func NewServiceUpstart(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceUpstart(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceUpstart(nil, strService, system, config), nil +} + +func newServiceUpstart(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceUpstart{service: service} } diff --git a/system/system.go b/system/system.go index 6c6083558..7cc625cde 100644 --- a/system/system.go +++ b/system/system.go @@ -20,21 +20,21 @@ type Resource interface { } type System struct { - NewPackage func(context.Context, string, *System, util2.Config) Package - NewFile func(context.Context, string, *System, util2.Config) File - NewAddr func(context.Context, string, *System, util2.Config) Addr - NewPort func(context.Context, string, *System, util2.Config) Port - NewService func(context.Context, string, *System, util2.Config) Service - NewUser func(context.Context, string, *System, util2.Config) User - NewGroup func(context.Context, string, *System, util2.Config) Group - NewCommand func(context.Context, string, *System, util2.Config) Command - NewDNS func(context.Context, string, *System, util2.Config) DNS - NewProcess func(context.Context, string, *System, util2.Config) Process - NewGossfile func(context.Context, string, *System, util2.Config) Gossfile - NewKernelParam func(context.Context, string, *System, util2.Config) KernelParam - NewMount func(context.Context, string, *System, util2.Config) Mount - NewInterface func(context.Context, string, *System, util2.Config) Interface - NewHTTP func(context.Context, string, *System, util2.Config) HTTP + NewPackage func(context.Context, interface{}, *System, util2.Config) (Package, error) + NewFile func(context.Context, interface{}, *System, util2.Config) (File, error) + NewAddr func(context.Context, interface{}, *System, util2.Config) (Addr, error) + NewPort func(context.Context, interface{}, *System, util2.Config) (Port, error) + NewService func(context.Context, interface{}, *System, util2.Config) (Service, error) + NewUser func(context.Context, interface{}, *System, util2.Config) (User, error) + NewGroup func(context.Context, interface{}, *System, util2.Config) (Group, error) + NewCommand func(context.Context, interface{}, *System, util2.Config) (Command, error) + NewDNS func(context.Context, interface{}, *System, util2.Config) (DNS, error) + NewProcess func(context.Context, interface{}, *System, util2.Config) (Process, error) + NewGossfile func(context.Context, interface{}, *System, util2.Config) (Gossfile, error) + NewKernelParam func(context.Context, interface{}, *System, util2.Config) (KernelParam, error) + NewMount func(context.Context, interface{}, *System, util2.Config) (Mount, error) + NewInterface func(context.Context, interface{}, *System, util2.Config) (Interface, error) + NewHTTP func(context.Context, interface{}, *System, util2.Config) (HTTP, error) ports map[string][]GOnetstat.Process portsOnce sync.Once procMap map[string][]ps.Process diff --git a/system/user.go b/system/user.go index 832715e7a..f0c29a75d 100644 --- a/system/user.go +++ b/system/user.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "os/user" "strconv" @@ -22,7 +23,15 @@ type DefUser struct { username string } -func NewDefUser(_ context.Context, username string, system *System, config util.Config) User { +func NewDefUser(_ context.Context, username interface{}, system *System, config util.Config) (User, error) { + strUsername, ok := username.(string) + if !ok { + return nil, fmt.Errorf("username must be of type string") + } + return newDefUser(nil, strUsername, system, config), nil +} + +func newDefUser(_ context.Context, username string, system *System, config util.Config) User { return &DefUser{username: username} } diff --git a/util/command.go b/util/command.go index 460020356..d95516b85 100644 --- a/util/command.go +++ b/util/command.go @@ -2,12 +2,52 @@ package util import ( "bytes" + "encoding/json" //"fmt" "os/exec" "syscall" ) +// Allows passing a shell style command string +// or an exec style slice of strings. +type ExecCommand struct { + CmdStr string + CmdSlice []string +} + +func (e *ExecCommand) UnmarshalJSON(data []byte) error { + // Try to unmarshal as a string + if err := json.Unmarshal(data, &e.CmdStr); err != nil { + // If string unmarshalling fails, try as a slice + return json.Unmarshal(data, &e.CmdSlice) + } + return nil +} + +func (e *ExecCommand) UnmarshalYAML(unmarshal func(interface{}) error) error { + // Try to unmarshal as a string + if err := unmarshal(&e.CmdStr); err != nil { + // If string unmarshalling fails, try as a slice + return unmarshal(&e.CmdSlice) + } + return nil +} + +func (e ExecCommand) MarshalJSON() ([]byte, error) { + if e.CmdStr != "" { + return json.Marshal(e.CmdStr) + } + return json.Marshal(e.CmdSlice) +} + +func (e ExecCommand) MarshalYAML() (interface{}, error) { + if e.CmdStr != "" { + return e.CmdStr, nil + } + return e.CmdSlice, nil +} + type Command struct { name string Cmd *exec.Cmd