Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add plugin shell content #5

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cmd/vela-kubernetes/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/sirupsen/logrus"
)

const kubectlBin = "/bin/kubectl"

// execCmd is a helper function to
// run the provided command.
func execCmd(e *exec.Cmd) error {
logrus.Tracef("executing cmd %s", strings.Join(e.Args, " "))

// set command stdout to OS stdout
e.Stdout = os.Stdout
// set command stderr to OS stderr
e.Stderr = os.Stderr

// output "trace" string for command
fmt.Println("$", strings.Join(e.Args, " "))

return e.Run()
}
20 changes: 20 additions & 0 deletions cmd/vela-kubernetes/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"os/exec"
"testing"
)

func TestKubernetes_execCmd(t *testing.T) {
// setup types
e := exec.Command("echo", "hello")

err := execCmd(e)
if err != nil {
t.Errorf("execCmd returned err: %v", err)
}
}
12 changes: 11 additions & 1 deletion cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,15 @@ func run(c *cli.Context) error {
"time": time.Now(),
}).Info("Vela Kubernetes Plugin")

return nil
// create the plugin
p := &Plugin{}

// validate the plugin
err := p.Validate()
if err != nil {
return err
}

// execute the plugin
return p.Exec()
}
39 changes: 39 additions & 0 deletions cmd/vela-kubernetes/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"os/exec"

"github.com/sirupsen/logrus"
)

// Plugin represents the configuration loaded for the plugin.
type Plugin struct{}

// Command formats and outputs the command necessary for
// kubectl to manage Kubernetes resources.
func (p *Plugin) Command() *exec.Cmd {
logrus.Debug("creating kubectl command from plugin configuration")

// variable to store flags for command
var flags []string

return exec.Command(kubectlBin, flags...)
}

// Exec formats and runs the commands for managing resources in Kubernetes.
func (p *Plugin) Exec() error {
logrus.Debug("running plugin with provided configuration")

return nil
}

// Validate verifies the plugin is properly configured.
func (p *Plugin) Validate() error {
logrus.Debug("validating plugin configuration")

return nil
}
47 changes: 47 additions & 0 deletions cmd/vela-kubernetes/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"os/exec"
"reflect"
"testing"
)

func TestKubernetes_Plugin_Command(t *testing.T) {
// setup types
p := &Plugin{}

want := exec.Command(
kubectlBin,
)

// run test
got := p.Command()

if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}

func TestKubernetes_Plugin_Exec(t *testing.T) {
// setup types
p := &Plugin{}

err := p.Exec()
if err != nil {
t.Errorf("Exec returned err: %v", err)
}
}

func TestKubernetes_Plugin_Validate(t *testing.T) {
// setup types
p := &Plugin{}

err := p.Validate()
if err != nil {
t.Errorf("Validate returned err: %v", err)
}
}