-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove redundant proc command to list/describe/execute procs
- Resolves #22
- Loading branch information
akshat
committed
Oct 12, 2018
1 parent
dde43e8
commit 11d35ba
Showing
23 changed files
with
670 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package description | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/fatih/color" | ||
"github.com/gojektech/proctor/daemon" | ||
"github.com/gojektech/proctor/io" | ||
"github.com/gojektech/proctor/proc" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "describe", | ||
Short: "Describe a proc, list help for variables and constants", | ||
Long: "In order to execute a proc, you need to provide certain variables. Describe command helps you with those variables and their meanings/convention/usage, etc.", | ||
Example: "proctor describe proc-one\nproctor describe proc-two", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
printer.Println("Incorrect command. See `proctor describe --help` for usage", color.FgRed) | ||
return | ||
} | ||
|
||
procList, err := proctorEngineClient.ListProcs() | ||
if err != nil { | ||
if err.Error() == http.StatusText(http.StatusUnauthorized) { | ||
printer.Println(utility.UnauthorizedError, color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println(utility.GenericDescribeCmdError, color.FgRed) | ||
return | ||
} | ||
|
||
desiredProc := proc.Metadata{} | ||
for _, proc := range procList { | ||
if args[0] == proc.Name { | ||
desiredProc = proc | ||
} | ||
} | ||
if len(desiredProc.Name) == 0 { | ||
printer.Println(fmt.Sprintf("Proctor doesn't support proc: %s", args[0]), color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println(fmt.Sprintf("%-40s %-100s", "Proc Name", desiredProc.Name), color.Reset) | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "Proc Description", desiredProc.Description), color.Reset) | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "Contributors", desiredProc.Contributors), color.Reset) | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "Organization", desiredProc.Organization), color.Reset) | ||
|
||
printer.Println("\nVariables", color.FgMagenta) | ||
for _, arg := range desiredProc.EnvVars.Args { | ||
printer.Println(fmt.Sprintf("%-40s %-100s", arg.Name, arg.Description), color.Reset) | ||
} | ||
|
||
printer.Println("\nConstants", color.FgMagenta) | ||
for _, secret := range desiredProc.EnvVars.Secrets { | ||
printer.Println(fmt.Sprintf("%-40s %-100s", secret.Name, secret.Description), color.Reset) | ||
} | ||
|
||
printer.Println("\nFor executing a proc, run:\nproctor execute <proc_name> <args_name>", color.FgGreen) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package description | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/fatih/color" | ||
"github.com/gojektech/proctor/daemon" | ||
"github.com/gojektech/proctor/io" | ||
"github.com/gojektech/proctor/proc" | ||
"github.com/gojektech/proctor/proc/env" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type DescribeCmdTestSuite struct { | ||
suite.Suite | ||
mockPrinter *io.MockPrinter | ||
mockProctorEngineClient *daemon.MockClient | ||
testDescribeCmd *cobra.Command | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) SetupTest() { | ||
s.mockPrinter = &io.MockPrinter{} | ||
s.mockProctorEngineClient = &daemon.MockClient{} | ||
s.testDescribeCmd = NewCmd(s.mockPrinter, s.mockProctorEngineClient) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdUsage() { | ||
assert.Equal(s.T(), "describe", s.testDescribeCmd.Use) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdHelp() { | ||
assert.Equal(s.T(), "Describe a proc, list help for variables and constants", s.testDescribeCmd.Short) | ||
assert.Equal(s.T(), "In order to execute a proc, you need to provide certain variables. Describe command helps you with those variables and their meanings/convention/usage, etc.", s.testDescribeCmd.Long) | ||
assert.Equal(s.T(), "proctor describe proc-one\nproctor describe proc-two", s.testDescribeCmd.Example) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdRun() { | ||
arg := env.VarMetadata{ | ||
Name: "arg-one", | ||
Description: "arg one description", | ||
} | ||
|
||
secret := env.VarMetadata{ | ||
Name: "secret-one", | ||
Description: "secret one description", | ||
} | ||
|
||
anyProc := proc.Metadata{ | ||
Name: "any-proc", | ||
Description: "does something", | ||
Contributors: "user@example.com", | ||
Organization: "org", | ||
EnvVars: env.Vars{ | ||
Args: []env.VarMetadata{arg}, | ||
Secrets: []env.VarMetadata{secret}, | ||
}, | ||
} | ||
procList := []proc.Metadata{anyProc} | ||
|
||
s.mockProctorEngineClient.On("ListProcs").Return(procList, nil).Once() | ||
|
||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Proc Name", anyProc.Name), color.Reset).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Proc Description", anyProc.Description), color.Reset).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Contributors", anyProc.Contributors), color.Reset).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Organization", anyProc.Organization), color.Reset).Once() | ||
s.mockPrinter.On("Println", "\nVariables", color.FgMagenta).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", arg.Name, arg.Description), color.Reset).Once() | ||
s.mockPrinter.On("Println", "\nConstants", color.FgMagenta).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", secret.Name, secret.Description), color.Reset).Once() | ||
s.mockPrinter.On("Println", "\nFor executing a proc, run:\nproctor execute <proc_name> <args_name>", color.FgGreen).Once() | ||
|
||
s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-proc"}) | ||
|
||
s.mockProctorEngineClient.AssertExpectations(s.T()) | ||
s.mockPrinter.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdForIncorrectUsage() { | ||
s.mockPrinter.On("Println", "Incorrect command. See `proctor describe --help` for usage", color.FgRed).Once() | ||
|
||
s.testDescribeCmd.Run(&cobra.Command{}, []string{}) | ||
|
||
s.mockPrinter.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdRunProctorEngineClientFailure() { | ||
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("error")).Once() | ||
s.mockPrinter.On("Println", utility.GenericDescribeCmdError, color.FgRed).Once() | ||
|
||
s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-proc"}) | ||
|
||
s.mockProctorEngineClient.AssertExpectations(s.T()) | ||
s.mockPrinter.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcNotSupported() { | ||
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, nil).Once() | ||
s.mockPrinter.On("Println", fmt.Sprintf("Proctor doesn't support proc: %s", "any-proc"), color.FgRed).Once() | ||
|
||
s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-proc"}) | ||
|
||
s.mockProctorEngineClient.AssertExpectations(s.T()) | ||
s.mockPrinter.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcForUnauthorizedUser() { | ||
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New(http.StatusText(http.StatusUnauthorized))).Once() | ||
s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once() | ||
|
||
s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-proc"}) | ||
|
||
s.mockProctorEngineClient.AssertExpectations(s.T()) | ||
s.mockPrinter.AssertExpectations(s.T()) | ||
} | ||
|
||
func TestDescribeCmdTestSuite(t *testing.T) { | ||
suite.Run(t, new(DescribeCmdTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/gojektech/proctor/daemon" | ||
"github.com/gojektech/proctor/io" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "execute", | ||
Short: "Execute a proc with arguments given", | ||
Long: "To execute a proc, this command helps communicate with `proctord` and streams to logs of proc in execution", | ||
Example: "proctor execute proc-one SOME_VAR=foo ANOTHER_VAR=bar\nproctor execute proc-two ANY_VAR=baz", | ||
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
printer.Println("Incorrect command. See `proctor execute --help` for usage", color.FgRed) | ||
return | ||
} | ||
|
||
procName := args[0] | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "Executing Proc", procName), color.Reset) | ||
|
||
procArgs := make(map[string]string) | ||
if len(args) > 1 { | ||
printer.Println("With Variables", color.FgMagenta) | ||
for _, v := range args[1:] { | ||
arg := strings.Split(v, "=") | ||
|
||
if len(arg) < 2 { | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "\nIncorrect variable format\n", v), color.FgRed) | ||
continue | ||
} | ||
|
||
combinedArgValue := strings.Join(arg[1:], "=") | ||
procArgs[arg[0]] = combinedArgValue | ||
|
||
printer.Println(fmt.Sprintf("%-40s %-100s", arg[0], combinedArgValue), color.Reset) | ||
} | ||
} else { | ||
printer.Println("With No Variables", color.FgRed) | ||
} | ||
|
||
executedProcName, err := proctorEngineClient.ExecuteProc(procName, procArgs) | ||
if err != nil { | ||
if err.Error() == http.StatusText(http.StatusUnauthorized) { | ||
printer.Println(utility.UnauthorizedError, color.FgRed) | ||
return | ||
} | ||
printer.Println(utility.GenericProcCmdError, color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println("Proc execution successful. \nStreaming logs:", color.FgGreen) | ||
err = proctorEngineClient.StreamProcLogs(executedProcName) | ||
if err != nil { | ||
printer.Println("Error Streaming Logs", color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println("Log stream of proc completed.", color.FgGreen) | ||
}, | ||
} | ||
} |
Oops, something went wrong.