func (c *pluginInstall) Run(context *cmd.Context, client *cmd.Client) error { pluginsDir := cmd.JoinWithUserDir(".tsuru", "plugins") err := filesystem().MkdirAll(pluginsDir, 0755) if err != nil { return err } pluginName := context.Args[0] pluginUrl := context.Args[1] pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", pluginName) file, err := filesystem().OpenFile(pluginPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) if err != nil { return err } resp, err := http.Get(pluginUrl) if err != nil { return err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return err } n, err := file.Write(data) if err != nil { return err } if n != len(data) { return errors.New("Failed to install plugin.") } fmt.Fprintf(context.Stdout, `Plugin "%s" successfully installed!`+"\n", pluginName) return nil }
func (s *S) TestPluginInstall(c *gocheck.C) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "fakeplugin") })) defer ts.Close() rfs := ftesting.RecordingFs{} fsystem = &rfs defer func() { fsystem = nil }() var stdout bytes.Buffer context := cmd.Context{ Args: []string{"myplugin", ts.URL}, Stdout: &stdout, } client := cmd.NewClient(nil, nil, manager) command := pluginInstall{} err := command.Run(&context, client) c.Assert(err, gocheck.IsNil) pluginsPath := cmd.JoinWithUserDir(".tsuru", "plugins") hasAction := rfs.HasAction(fmt.Sprintf("mkdirall %s with mode 0755", pluginsPath)) c.Assert(hasAction, gocheck.Equals, true) pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", "myplugin") hasAction = rfs.HasAction(fmt.Sprintf("openfile %s with mode 0755", pluginPath)) c.Assert(hasAction, gocheck.Equals, true) f, err := rfs.Open(pluginPath) c.Assert(err, gocheck.IsNil) data, err := ioutil.ReadAll(f) c.Assert(err, gocheck.IsNil) c.Assert("fakeplugin\n", gocheck.Equals, string(data)) expected := `Plugin "myplugin" successfully installed!` + "\n" c.Assert(expected, gocheck.Equals, stdout.String()) }
func (c *pluginList) Run(context *cmd.Context, client *cmd.Client) error { pluginsPath := cmd.JoinWithUserDir(".tsuru", "plugins") plugins, _ := ioutil.ReadDir(pluginsPath) for _, p := range plugins { fmt.Println(p.Name()) } return nil }
func (c *pluginRemove) Run(context *cmd.Context, client *cmd.Client) error { pluginName := context.Args[0] pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", pluginName) err := filesystem().Remove(pluginPath) if err != nil { return err } fmt.Fprintf(context.Stdout, `Plugin "%s" successfully removed!`+"\n", pluginName) return nil }
func (c *plugin) Run(context *cmd.Context, client *cmd.Client) error { pluginName := context.Args[0] pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", pluginName) var b bytes.Buffer err := executor().Execute(pluginPath, context.Args[1:], nil, &b, &b) if err != nil { return err } fmt.Println(b.String()) return nil }
func (s *S) TestPluginLookup(c *gocheck.C) { fexec := etesting.FakeExecutor{} execut = &fexec defer func() { execut = nil }() manager := buildManager("tsuru") manager.Run([]string{"myplugin"}) pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", "myplugin") c.Assert(fexec.ExecutedCmd(pluginPath, []string{}), gocheck.Equals, true) }
func (s *S) TestPluginWithArgs(c *gocheck.C) { fexec := etesting.FakeExecutor{} execut = &fexec defer func() { execut = nil }() context := cmd.Context{ Args: []string{"myplugin", "ble", "bla"}, } client := cmd.NewClient(nil, nil, manager) command := plugin{} err := command.Run(&context, client) c.Assert(err, gocheck.IsNil) pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", "myplugin") c.Assert(fexec.ExecutedCmd(pluginPath, []string{"ble", "bla"}), gocheck.Equals, true) }
func (s *S) TestPluginRemove(c *gocheck.C) { rfs := ftesting.RecordingFs{} fsystem = &rfs defer func() { fsystem = nil }() var stdout bytes.Buffer context := cmd.Context{ Args: []string{"myplugin"}, Stdout: &stdout, } client := cmd.NewClient(nil, nil, manager) command := pluginRemove{} err := command.Run(&context, client) c.Assert(err, gocheck.IsNil) pluginPath := cmd.JoinWithUserDir(".tsuru", "plugins", "myplugin") hasAction := rfs.HasAction(fmt.Sprintf("remove %s", pluginPath)) c.Assert(hasAction, gocheck.Equals, true) expected := `Plugin "myplugin" successfully removed!` + "\n" c.Assert(expected, gocheck.Equals, stdout.String()) }