From 7e3d2faa073ea1c5d268b1147fce0d27ed4ba506 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 18 Oct 2021 11:55:31 -0500 Subject: [PATCH] Add write_hello_file capability --- builtin/myplugin/host/alwaystrue.go | 12 +++++++++--- builtin/myplugin/host/cap/write_hello.go | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/builtin/myplugin/host/alwaystrue.go b/builtin/myplugin/host/alwaystrue.go index 7265fbc95..815e30bfc 100644 --- a/builtin/myplugin/host/alwaystrue.go +++ b/builtin/myplugin/host/alwaystrue.go @@ -30,7 +30,7 @@ func (h *AlwaysTrueHost) HostDetectFunc() interface{} { } func (h *AlwaysTrueHost) Detect() bool { - return false + return true } // ParentsFunc implements component.Host @@ -39,7 +39,7 @@ func (h *AlwaysTrueHost) ParentsFunc() interface{} { } func (h *AlwaysTrueHost) Parents() []string { - return []string{"darwin", "bsd"} + return []string{} } // HasCapabilityFunc implements component.Host @@ -48,7 +48,7 @@ func (h *AlwaysTrueHost) HasCapabilityFunc() interface{} { } func (h *AlwaysTrueHost) CheckCapability(n *component.NamedCapability) bool { - if n.Capability == "write_hello" { + if n.Capability == "write_hello" || n.Capability == "write_hello_file" { return true } return false @@ -58,6 +58,8 @@ func (h *AlwaysTrueHost) CheckCapability(n *component.NamedCapability) bool { func (h *AlwaysTrueHost) CapabilityFunc(name string) interface{} { if name == "write_hello" { return h.WriteHelloCap + } else if name == "write_hello_file" { + return h.WriteHelloToTempFileCap } return errors.New("Invalid capability requested") } @@ -66,6 +68,10 @@ func (h *AlwaysTrueHost) WriteHelloCap(ui terminal.UI) error { return cap.WriteHello(ui) } +func (h *AlwaysTrueHost) WriteHelloToTempFileCap() error { + return cap.WriteHelloToTempfile() +} + var ( _ component.Host = (*AlwaysTrueHost)(nil) ) diff --git a/builtin/myplugin/host/cap/write_hello.go b/builtin/myplugin/host/cap/write_hello.go index 177914b01..41447e195 100644 --- a/builtin/myplugin/host/cap/write_hello.go +++ b/builtin/myplugin/host/cap/write_hello.go @@ -1,6 +1,8 @@ package cap import ( + "os" + "github.com/hashicorp/vagrant-plugin-sdk/terminal" ) @@ -9,3 +11,12 @@ func WriteHello(ui terminal.UI) error { ui.Output(msg) return nil } + +func WriteHelloToTempfile() error { + msg := []byte("Hello from the write hello capability, compliments of the AlwaysTrue Host") + err := os.WriteFile("/tmp/write_hello", msg, 0644) + if err != nil { + panic(err) + } + return nil +}