Skip to content

Commit

Permalink
feat: add aws provider
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Feb 10, 2023
1 parent 180a9a5 commit e177a82
Show file tree
Hide file tree
Showing 146 changed files with 7,058 additions and 1,441 deletions.
24 changes: 12 additions & 12 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func (cmd *DeleteCmd) Run(ctx context.Context, workspace *provider2.Workspace, p

func (cmd *DeleteCmd) destroyWorkspace(ctx context.Context, workspace *provider2.Workspace, provider provider2.WorkspaceProvider) error {
// get instance status
instanceStatus, err := provider.Status(ctx, workspace, provider2.WorkspaceStatusOptions{})
if err != nil {
return err
} else if instanceStatus == provider2.StatusNotFound {
if !cmd.Force {
if !cmd.Force {
instanceStatus, err := provider.Status(ctx, workspace, provider2.WorkspaceStatusOptions{})
if err != nil {
return err
} else if instanceStatus == provider2.StatusNotFound {
return fmt.Errorf("cannot delete instance because it couldn't be found. Run with --force to ignore this error")
}
}

// destroy environment
err = provider.Delete(ctx, workspace, provider2.WorkspaceDeleteOptions{})
err := provider.Delete(ctx, workspace, provider2.WorkspaceDeleteOptions{Force: cmd.Force})
if err != nil {
return err
}
Expand All @@ -90,17 +90,17 @@ func (cmd *DeleteCmd) destroyWorkspace(ctx context.Context, workspace *provider2

func (cmd *DeleteCmd) destroyServer(ctx context.Context, workspace *provider2.Workspace, provider provider2.ServerProvider) error {
// get instance status
instanceStatus, err := provider.Status(ctx, workspace, provider2.StatusOptions{})
if err != nil {
return err
} else if instanceStatus == provider2.StatusNotFound {
if !cmd.Force {
if !cmd.Force {
instanceStatus, err := provider.Status(ctx, workspace, provider2.StatusOptions{})
if err != nil {
return err
} else if instanceStatus == provider2.StatusNotFound {
return fmt.Errorf("cannot delete instance because it couldn't be found. Run with --force to ignore this error")
}
}

// destroy environment
err = provider.Delete(ctx, workspace, provider2.DeleteOptions{})
err := provider.Delete(ctx, workspace, provider2.DeleteOptions{Force: cmd.Force})
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package helper
import (
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/cmd/helper/http"
"github.com/loft-sh/devpod/cmd/helper/json"
"github.com/loft-sh/devpod/cmd/helper/strings"
"github.com/spf13/cobra"
)

Expand All @@ -15,6 +17,8 @@ func NewHelperCmd(flags *flags.GlobalFlags) *cobra.Command {
}

helperCmd.AddCommand(http.NewHTTPCmd(flags))
helperCmd.AddCommand(json.NewJSONCmd(flags))
helperCmd.AddCommand(strings.NewStringsCmd(flags))
helperCmd.AddCommand(NewSSHServerCmd())
helperCmd.AddCommand(NewSSHClientCmd())
return helperCmd
Expand Down
90 changes: 90 additions & 0 deletions cmd/helper/json/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package json

import (
"context"
"encoding/json"
"fmt"
"github.com/PaesslerAG/jsonpath"
"github.com/spf13/cobra"
"io"
"os"
"strings"
)

type GetCmd struct {
File string
Fail bool
}

// NewGetCmd creates a new ssh command
func NewGetCmd() *cobra.Command {
cmd := &GetCmd{}
getCmd := &cobra.Command{
Use: "get",
Short: "Retrieves a JSON value by JSONPath",
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background(), args)
},
}

getCmd.Flags().StringVarP(&cmd.File, "file", "f", "", "Parse this json file instead of STDIN")
getCmd.Flags().BoolVar(&cmd.Fail, "fail", false, "Fail if value is not found")
return getCmd
}

func (cmd *GetCmd) Run(ctx context.Context, args []string) error {
if len(args) == 0 {
return fmt.Errorf("jsonpath expected")
}

if !strings.HasPrefix(args[0], "$") {
if !strings.HasPrefix(args[0], "[") && !strings.HasPrefix(args[0], ".") {
args[0] = "." + args[0]
}

args[0] = "$" + args[0]
}

var jsonBytes []byte
if cmd.File != "" {
var err error
jsonBytes, err = os.ReadFile(cmd.File)
if err != nil {
return err
}
} else {
var err error
jsonBytes, err = io.ReadAll(os.Stdin)
if err != nil {
return err
}
}

v := interface{}(nil)
err := json.Unmarshal(jsonBytes, &v)
if err != nil {
return fmt.Errorf("parse json")
}

val, err := jsonpath.Get(args[0], v)
if err != nil {
if cmd.Fail {
return err
}
return nil
}

switch t := val.(type) {
case bool, int, int64, rune, string:
fmt.Print(t)
return nil
}

out, err := json.MarshalIndent(val, "", " ")
if err != nil {
return err
}

fmt.Print(string(out))
return nil
}
18 changes: 18 additions & 0 deletions cmd/helper/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package json

import (
"github.com/loft-sh/devpod/cmd/flags"
"github.com/spf13/cobra"
)

// NewJSONCmd returns a new command
func NewJSONCmd(flags *flags.GlobalFlags) *cobra.Command {
jsonCmd := &cobra.Command{
Use: "json",
Short: "DevPod JSON Utility Commands",
Hidden: true,
}

jsonCmd.AddCommand(NewGetCmd())
return jsonCmd
}
17 changes: 17 additions & 0 deletions cmd/helper/strings/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package strings

import (
"github.com/loft-sh/devpod/cmd/flags"
"github.com/spf13/cobra"
)

// NewStringsCmd returns a new command
func NewStringsCmd(flags *flags.GlobalFlags) *cobra.Command {
stringsCmd := &cobra.Command{
Use: "strings",
Short: "DevPod String Utility Commands",
Hidden: true,
}

return stringsCmd
}
106 changes: 106 additions & 0 deletions cmd/provider/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package provider

import (
"context"
"fmt"
"github.com/alessio/shellescape"
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/log"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/workspace"
"github.com/spf13/cobra"
"os"
)

// ExecCmd holds the exec cmd flags
type ExecCmd struct {
flags.GlobalFlags
}

// NewExecCmd creates a new command
func NewExecCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd := &ExecCmd{
GlobalFlags: *flags,
}
execCmd := &cobra.Command{
Use: "exec",
Short: "Executes a provider command",
Long: `
Executes a provider command in a given workspace.
E.g.:
devpod provider exec aws status MY_WORKSPACE
devpod provider exec aws create MY_WORKSPACE
`,
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background(), args)
},
}

return execCmd
}

// Run runs the command logic
func (cmd *ExecCmd) Run(ctx context.Context, args []string) error {
if len(args) < 3 {
return fmt.Errorf("expected exactly 3 arguments: PROVIDER COMMAND WORKSPACE")
}

devPodConfig, err := config.LoadConfig(cmd.Context)
if err != nil {
return err
}

devPodConfig.Contexts[devPodConfig.DefaultContext].DefaultProvider = args[0]
workspaceConfig, provider, err := workspace.ResolveWorkspace(ctx, devPodConfig, []string{args[2]}, log.Default)
if err != nil {
return err
}

// case server provider
serverProvider, ok := provider.(provider2.ServerProvider)
if ok {
switch args[1] {
case "create":
err = serverProvider.Create(ctx, workspaceConfig, provider2.CreateOptions{})
if err != nil {
return err
}
case "delete":
err = serverProvider.Delete(ctx, workspaceConfig, provider2.DeleteOptions{})
if err != nil {
return err
}
case "stop":
err = serverProvider.Stop(ctx, workspaceConfig, provider2.StopOptions{})
if err != nil {
return err
}
case "start":
err = serverProvider.Start(ctx, workspaceConfig, provider2.StartOptions{})
if err != nil {
return err
}
case "command":
err = serverProvider.Command(ctx, workspaceConfig, provider2.CommandOptions{
Command: shellescape.QuoteCommand(args[3:]),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
return err
}
case "status":
status, err := serverProvider.Status(ctx, workspaceConfig, provider2.StatusOptions{})
if err != nil {
return err
}

fmt.Println(status)
}
}

return nil
}
1 change: 1 addition & 0 deletions cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ func NewProviderCmd(flags *flags.GlobalFlags) *cobra.Command {
providerCmd.AddCommand(NewListCmd(flags))
providerCmd.AddCommand(NewUseCmd(flags))
providerCmd.AddCommand(NewOptionsCmd(flags))
providerCmd.AddCommand(NewExecCmd(flags))
return providerCmd
}
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/PaesslerAG/jsonpath v0.1.1
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/alessio/shellescape v1.4.1
github.com/blang/semver v3.5.1+incompatible
Expand All @@ -30,7 +31,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/takama/daemon v1.0.0
golang.org/x/crypto v0.2.0
golang.org/x/crypto v0.6.0
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
gopkg.in/natefinch/lumberjack.v2 v2.2.1
Expand All @@ -43,6 +44,7 @@ require (
cloud.google.com/go v0.97.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/containerd v1.6.16-0.20230124210447-1709cfe273d9 // indirect
Expand Down Expand Up @@ -94,12 +96,12 @@ require (
go.opentelemetry.io/otel/trace v1.4.1 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit e177a82

Please sign in to comment.