Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Scaffold everything but Vital
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Watters committed Apr 26, 2020
1 parent 84a38cc commit 48fc871
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 72 deletions.
90 changes: 90 additions & 0 deletions command/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2020 Opsani
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package command

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
)

type authCommand struct {
*BaseCommand
}

// NewAuthCommand returns a new `opani login` command instance
func NewAuthCommand(baseCmd *BaseCommand) *cobra.Command {
authCommand := authCommand{BaseCommand: baseCmd}

authCobra := &cobra.Command{
Use: "auth",
Short: "Manage authentication",
Args: cobra.NoArgs,
PersistentPreRunE: ReduceRunEFuncs(
baseCmd.InitConfigRunE,
baseCmd.RequireConfigFileFlagToExistRunE,
baseCmd.RequireInitRunE,
),
}

loginCobra := &cobra.Command{
Use: "login",
Short: "Login to Opsani",
Args: cobra.NoArgs,
RunE: authCommand.runLoginCommand,
}

loginCobra.Flags().StringP("username", "u", "", "Opsani Username")
loginCobra.Flags().StringP("password", "p", "", "Password")
authCobra.AddCommand(loginCobra)

authCobra.AddCommand(&cobra.Command{
Use: "logout",
Short: "Logout from Opsani",
Args: cobra.NoArgs,
})

return authCobra
}

func (cmd *authCommand) runLoginCommand(c *cobra.Command, args []string) error {
fmt.Println("Logging into", cmd.GetBaseURLHostnameAndPort())

whiteBold := ansi.ColorCode("white+b")
username, _ := c.Flags().GetString("username")
if username == "" {
err := survey.AskOne(&survey.Input{
Message: "Username:",
}, &username, survey.WithValidator(survey.Required))
if err != nil {
return err
}
} else {
fmt.Printf("%si %sUsername: %s%s%s%s\n", ansi.Blue, whiteBold, ansi.Reset, ansi.LightCyan, username, ansi.Reset)
}

password, _ := c.Flags().GetString("password")
if password == "" {
err := survey.AskOne(&survey.Password{
Message: "Password:",
}, &password, survey.WithValidator(survey.Required))
if err != nil {
return err
}
}
return nil
}
20 changes: 13 additions & 7 deletions command/login_test.go → command/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ import (
"github.com/stretchr/testify/suite"
)

type LoginTestSuite struct {
type AuthTestSuite struct {
test.Suite
}

func TestLoginTestSuite(t *testing.T) {
suite.Run(t, new(LoginTestSuite))
func TestAuthTestSuite(t *testing.T) {
suite.Run(t, new(AuthTestSuite))
}

func (s *LoginTestSuite) SetupTest() {
func (s *AuthTestSuite) SetupTest() {
s.SetCommand(command.NewRootCommand())
}

func (s *LoginTestSuite) TestRunningInitHelp() {
output, err := s.Execute("login", "--help")
func (s *AuthTestSuite) TestLoginHelp() {
output, err := s.Execute("auth", "login", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Login to the Opsani API")
s.Require().Contains(output, "Login to Opsani")
}

func (s *AuthTestSuite) TestLogoutHelp() {
output, err := s.Execute("auth", "logout", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Logout from Opsani")
}
6 changes: 0 additions & 6 deletions command/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,3 @@ func (s *DiscoverTestSuite) TestRunningIMBHelp() {
s.Require().NoError(err)
s.Require().Contains(output, "Run the intelligent manifest builder")
}

func (s *DiscoverTestSuite) TestRunningPullHelp() {
output, err := s.Execute("pull", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Pull a Docker image")
}
58 changes: 0 additions & 58 deletions command/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,3 @@
// limitations under the License.

package command

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
)

type loginCommand struct {
*BaseCommand

username string
password string
}

func (loginCmd *loginCommand) runLoginCommand(_ *cobra.Command, args []string) error {
fmt.Println("Logging into", loginCmd.GetBaseURLHostnameAndPort())

whiteBold := ansi.ColorCode("white+b")
if loginCmd.username == "" {
err := survey.AskOne(&survey.Input{
Message: "Username:",
}, &loginCmd.username, survey.WithValidator(survey.Required))
if err != nil {
return err
}
} else {
fmt.Printf("%si %sUsername: %s%s%s%s\n", ansi.Blue, whiteBold, ansi.Reset, ansi.LightCyan, loginCmd.username, ansi.Reset)
}

if loginCmd.password == "" {
err := survey.AskOne(&survey.Password{
Message: "Password:",
}, &loginCmd.password, survey.WithValidator(survey.Required))
if err != nil {
return err
}
}
return nil
}

// NewLoginCommand returns a new `opani login` command instance
func NewLoginCommand(baseCmd *BaseCommand) *cobra.Command {
loginCmd := loginCommand{BaseCommand: baseCmd}
c := &cobra.Command{
Use: "login",
Short: "Login to the Opsani API",
Long: `Login to the Opsani API and persist access credentials.`,
Args: cobra.NoArgs,
RunE: loginCmd.runLoginCommand,
}

c.Flags().StringVarP(&loginCmd.username, "username", "u", "", "Opsani Username")
c.Flags().StringVarP(&loginCmd.password, "password", "p", "", "Password")

return c
}
2 changes: 1 addition & 1 deletion command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ We'd love to hear your feedback at <https://github.com/opsani/cli>`,
// Add all sub-commands
cobraCmd.AddCommand(NewInitCommand(rootCmd))
cobraCmd.AddCommand(NewAppCommand(rootCmd))
cobraCmd.AddCommand(NewLoginCommand(rootCmd))
cobraCmd.AddCommand(NewAuthCommand(rootCmd))

cobraCmd.AddCommand(newDiscoverCommand(rootCmd))
cobraCmd.AddCommand(newIMBCommand(rootCmd))
Expand Down
2 changes: 2 additions & 0 deletions command/servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func NewServoCommand(baseCmd *BaseCommand) *cobra.Command {

// Add nested children
servoCmd.AddCommand(NewServoImageCommand(baseCmd))
servoCmd.AddCommand(NewServoAssemblyCommand(baseCmd))
servoCmd.AddCommand(NewServoPluginCommand(baseCmd))

return servoCmd
}
Expand Down
103 changes: 103 additions & 0 deletions command/servo_assembly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2020 Opsani
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package command

import (
"github.com/spf13/cobra"
)

type servoAssemblyCommand struct {
*BaseCommand
}

// NewServoAssemblyCommand returns a new instance of the servo image command
func NewServoAssemblyCommand(baseCmd *BaseCommand) *cobra.Command {
servoAssemblyCommand := servoAssemblyCommand{BaseCommand: baseCmd}

servoAssemblyCobra := &cobra.Command{
Use: "assembly",
Short: "Manage Servo Assemblies",
Args: cobra.NoArgs,
PersistentPreRunE: ReduceRunEFuncs(
baseCmd.InitConfigRunE,
baseCmd.RequireConfigFileFlagToExistRunE,
baseCmd.RequireInitRunE,
),
}

servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List Servo assemblies",
Args: cobra.NoArgs,
RunE: servoAssemblyCommand.RunList,
})
servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "search",
Short: "Search for Servo assemblies",
Args: cobra.ExactArgs(1),
RunE: servoAssemblyCommand.RunSearch,
})
servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "info",
Short: "Get info about a Servo assembly",
Args: cobra.ExactArgs(1),
RunE: servoAssemblyCommand.RunInfo,
})
servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "clone",
Short: "Clone a Servo assembly with Git",
Args: cobra.ExactArgs(1),
RunE: servoAssemblyCommand.RunClone,
})
servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "fork",
Short: "Fork a Servo assembly on GitHub",
Args: cobra.ExactArgs(1),
RunE: servoAssemblyCommand.RunFork,
})
servoAssemblyCobra.AddCommand(&cobra.Command{
Use: "create",
Short: "Create a new Servo assembly",
Args: cobra.ExactArgs(1),
RunE: servoAssemblyCommand.RunCreate,
})

return servoAssemblyCobra
}

func (cmd *servoAssemblyCommand) RunList(_ *cobra.Command, args []string) error {
return nil
}

func (cmd *servoAssemblyCommand) RunSearch(_ *cobra.Command, args []string) error {
return nil
}

func (cmd *servoAssemblyCommand) RunInfo(_ *cobra.Command, args []string) error {
return nil
}

func (cmd *servoAssemblyCommand) RunClone(c *cobra.Command, args []string) error {
return nil
}

func (cmd *servoAssemblyCommand) RunFork(c *cobra.Command, args []string) error {
return nil
}

func (cmd *servoAssemblyCommand) RunCreate(c *cobra.Command, args []string) error {
return nil
}
77 changes: 77 additions & 0 deletions command/servo_assembly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020 Opsani
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package command_test

import (
"testing"

"github.com/opsani/cli/command"
"github.com/opsani/cli/test"
"github.com/stretchr/testify/suite"
)

type ServoAssemblyTestSuite struct {
test.Suite
}

func TestServoAssemblyTestSuite(t *testing.T) {
suite.Run(t, new(ServoAssemblyTestSuite))
}

func (s *ServoAssemblyTestSuite) SetupTest() {
s.SetCommand(command.NewRootCommand())
}

func (s *ServoAssemblyTestSuite) TestRootHelp() {
output, err := s.Execute("servo", "assembly", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Manage Servo Assemblies")
}

func (s *ServoAssemblyTestSuite) TestListtHelp() {
output, err := s.Execute("servo", "assembly", "list", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "List Servo assemblies")
}

func (s *ServoAssemblyTestSuite) TestSearchHelp() {
output, err := s.Execute("servo", "assembly", "search", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Search for Servo assemblies")
}

func (s *ServoAssemblyTestSuite) TestInfoHelp() {
output, err := s.Execute("servo", "assembly", "info", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Get info about a Servo assembly")
}

func (s *ServoAssemblyTestSuite) TestCloneHelp() {
output, err := s.Execute("servo", "assembly", "clone", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Clone a Servo assembly with Git")
}

func (s *ServoAssemblyTestSuite) TestForkHelp() {
output, err := s.Execute("servo", "assembly", "fork", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Fork a Servo assembly on GitHub")
}

func (s *ServoAssemblyTestSuite) TestCreateHelp() {
output, err := s.Execute("servo", "assembly", "create", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Create a new Servo assembly")
}
Loading

0 comments on commit 48fc871

Please sign in to comment.