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

Commit

Permalink
Merge pull request #2 from opsani/demo
Browse files Browse the repository at this point in the history
Enhancements for Engineering Demo 4/27/2020
  • Loading branch information
blakewatters authored Apr 27, 2020
2 parents 737bdd9 + a398012 commit d012714
Show file tree
Hide file tree
Showing 35 changed files with 2,723 additions and 515 deletions.
26 changes: 23 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM scratch
COPY ./bin/opsani /
ENTRYPOINT ["/opsani"]
FROM golang:alpine

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

# Move to working directory /build
WORKDIR /build

# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Build our binary
COPY . .
RUN go build -o bin/opsani .
WORKDIR /dist
RUN cp /build/bin/opsani .

CMD ["/dist/opsani"]
55 changes: 48 additions & 7 deletions command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@
package command

import (
"fmt"
"log"
"os/exec"
"runtime"

"github.com/spf13/cobra"
)

// NewAppCommand returns a new `opsani app` command instance
func NewAppCommand() *cobra.Command {
func NewAppCommand(baseCmd *BaseCommand) *cobra.Command {
appCmd := &cobra.Command{
Use: "app",
Short: "Manage apps",

// All commands require an initialized client
PersistentPreRunE: InitConfigRunE,
PersistentPreRunE: baseCmd.InitConfigRunE,
}

// Initialize our subcommands
appStartCmd := NewAppStartCommand()
appStopCmd := NewAppStopCommand()
appRestartCmd := NewAppRestartCommand()
appStatusCmd := NewAppStatusCommand()
appConfigCmd := NewAppConfigCommand()
appStartCmd := NewAppStartCommand(baseCmd)
appStopCmd := NewAppStopCommand(baseCmd)
appRestartCmd := NewAppRestartCommand(baseCmd)
appStatusCmd := NewAppStatusCommand(baseCmd)
appConfigCmd := NewAppConfigCommand(baseCmd)

// Lifecycle
appCmd.AddCommand(appStartCmd)
Expand All @@ -44,5 +49,41 @@ func NewAppCommand() *cobra.Command {
// Config
appCmd.AddCommand(appConfigCmd)

appCmd.AddCommand(NewAppConsoleCommand(baseCmd))

return appCmd
}

// NewAppConsoleCommand returns a command that opens the Opsani Console
// in the default browser
func NewAppConsoleCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "console",
Short: "Open the Opsani console in the default web browser",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
org, appID := baseCmd.GetAppComponents()
url := fmt.Sprintf("https://console.opsani.com/accounts/%s/applications/%s", org, appID)
openURLInDefaultBrowser(url)
return nil
},
}
}

func openURLInDefaultBrowser(url string) {
var err error

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
26 changes: 13 additions & 13 deletions command/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func openFileInEditor(filename string, editor string) error {
}

// NewAppConfigEditCommand returns a new Opsani CLI app config edit action
func NewAppConfigEditCommand() *cobra.Command {
func NewAppConfigEditCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "edit [PATH=VALUE ...]",
Short: "Edit app config",
Expand All @@ -64,7 +64,7 @@ func NewAppConfigEditCommand() *cobra.Command {
filename := tempFile.Name()

// Download config to temp
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
resp, err := client.GetConfig()
if err != nil {
return err
Expand Down Expand Up @@ -120,13 +120,13 @@ func NewAppConfigEditCommand() *cobra.Command {
}

// NewAppConfigGetCommand returns a new Opsani CLI `app config get` action
func NewAppConfigGetCommand() *cobra.Command {
func NewAppConfigGetCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "get [PATH ...]",
Short: "Get app config",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
resp, err := client.GetConfig()
if err != nil {
return err
Expand Down Expand Up @@ -193,13 +193,13 @@ func bodyForConfigUpdateWithArgs(args []string) (interface{}, error) {
}

// NewAppConfigSetCommand returns a new Opsani CLI `app config set` action
func NewAppConfigSetCommand() *cobra.Command {
func NewAppConfigSetCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "set [CONFIG]",
Short: "Set app config",
Args: RangeOfValidJSONArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
body, err := bodyForConfigUpdateWithArgs(args)
if err != nil {
return err
Expand All @@ -215,14 +215,14 @@ func NewAppConfigSetCommand() *cobra.Command {
}

// NewAppConfigPatchCommand returns a new Opsani CLI `app config patch` action
func NewAppConfigPatchCommand() *cobra.Command {
func NewAppConfigPatchCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "patch [CONFIG]",
Short: "Patch app config",
Long: "Patch merges the incoming change into the existing configuration.",
Args: RangeOfValidJSONArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
body, err := bodyForConfigUpdateWithArgs(args)
if err != nil {
return err
Expand All @@ -246,16 +246,16 @@ var appConfig = struct {
}{}

// NewAppConfigCommand returns a new Opsani CLI `app config` action
func NewAppConfigCommand() *cobra.Command {
func NewAppConfigCommand(baseCmd *BaseCommand) *cobra.Command {
appConfigCmd := &cobra.Command{
Use: "config",
Short: "Manage app config",
}

appConfigGetCmd := NewAppConfigGetCommand()
appConfigSetCmd := NewAppConfigSetCommand()
appConfigPatchCmd := NewAppConfigPatchCommand()
appConfigEditCmd := NewAppConfigEditCommand()
appConfigGetCmd := NewAppConfigGetCommand(baseCmd)
appConfigSetCmd := NewAppConfigSetCommand(baseCmd)
appConfigPatchCmd := NewAppConfigPatchCommand(baseCmd)
appConfigEditCmd := NewAppConfigEditCommand(baseCmd)

appConfigCmd.AddCommand(appConfigGetCmd)
appConfigCmd.AddCommand(appConfigSetCmd)
Expand Down
2 changes: 0 additions & 2 deletions command/app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

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

Expand All @@ -32,7 +31,6 @@ func TestAppConfigTestSuite(t *testing.T) {
}

func (s *AppConfigTestSuite) SetupTest() {
viper.Reset()
s.SetCommand(command.NewRootCommand())
}

Expand Down
16 changes: 8 additions & 8 deletions command/app_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package command
import "github.com/spf13/cobra"

// NewAppStartCommand returns an Opsani CLI command for starting the app
func NewAppStartCommand() *cobra.Command {
func NewAppStartCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start the app",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
if resp, err := client.StartApp(); err == nil {
return PrettyPrintJSONResponse(resp)
} else {
Expand All @@ -34,13 +34,13 @@ func NewAppStartCommand() *cobra.Command {
}

// NewAppStopCommand returns an Opsani CLI command for stopping the app
func NewAppStopCommand() *cobra.Command {
func NewAppStopCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "stop",
Short: "Stop the app",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
resp, err := client.StopApp()
if err != nil {
return err
Expand All @@ -51,13 +51,13 @@ func NewAppStopCommand() *cobra.Command {
}

// NewAppRestartCommand returns an Opsani CLI command for restarting the app
func NewAppRestartCommand() *cobra.Command {
func NewAppRestartCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "restart",
Short: "Restart the app",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
resp, err := client.RestartApp()
if err != nil {
return err
Expand All @@ -68,13 +68,13 @@ func NewAppRestartCommand() *cobra.Command {
}

// NewAppStatusCommand returns an Opsani CLI command for retrieving status on the app
func NewAppStatusCommand() *cobra.Command {
func NewAppStatusCommand(baseCmd *BaseCommand) *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Check app status",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := NewAPIClientFromConfig()
client := baseCmd.NewAPIClient()
resp, err := client.GetAppStatus()
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions command/app_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

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

Expand All @@ -32,7 +31,6 @@ func TestAppLifecycleTestSuite(t *testing.T) {
}

func (s *AppLifecycleTestSuite) SetupTest() {
viper.Reset()
s.SetCommand(command.NewRootCommand())
}

Expand Down
21 changes: 18 additions & 3 deletions command/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

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

Expand All @@ -32,12 +31,28 @@ func TestAppTestSuite(t *testing.T) {
}

func (s *AppTestSuite) SetupTest() {
viper.Reset()
s.SetCommand(command.NewRootCommand())
}

func (s *AppTestSuite) TestRunningAppStartHelp() {
func (s *AppTestSuite) TestRunningApp() {
output, err := s.Execute("app")
s.Require().NoError(err)
s.Require().Contains(output, "Manage apps")
s.Require().Contains(output, "Usage:")
}

func (s *AppTestSuite) TestRunningAppHelp() {
output, err := s.Execute("app", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Manage apps")
}

func (s *AppTestSuite) TestRunningAppConsoleHelp() {
output, err := s.Execute("app", "--help")
s.Require().NoError(err)
s.Require().Contains(output, "Open the Opsani console")
}

func TestRunningAppConsle(t *testing.T) {
t.Skip("Pending test for launching browser")
}
Loading

0 comments on commit d012714

Please sign in to comment.