-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180a9a5
commit e177a82
Showing
146 changed files
with
7,058 additions
and
1,441 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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.