forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sandbox status & Bash completion (flyteorg#115)
* Sandbox status command Signed-off-by: Ketan Umare <[email protected]> * bash completion Signed-off-by: Ketan Umare <[email protected]> * goimported Signed-off-by: Ketan Umare <[email protected]> * fixed lint Signed-off-by: Ketan Umare <[email protected]> * added unit test for status Signed-off-by: Yuvraj <[email protected]> Co-authored-by: Yuvraj <[email protected]>
- Loading branch information
Showing
8 changed files
with
184 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright © 2021 NAME HERE <EMAIL ADDRESS> | ||
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 cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// completionCmd represents the completion command | ||
var completionCmd = &cobra.Command{ | ||
Use: "completion [bash|zsh|fish|powershell]", | ||
Short: "Generate completion script", | ||
Long: `To load completions: | ||
Bash: | ||
$ source <(flytectl completion bash) | ||
# To load completions for each session, execute once: | ||
# Linux: | ||
$ flytectl completion bash > /etc/bash_completion.d/flytectl | ||
# macOS: | ||
$ flytectl completion bash > /usr/local/etc/bash_completion.d/flytectl | ||
Zsh: | ||
# If shell completion is not already enabled in your environment, | ||
# you will need to enable it. You can execute the following once: | ||
$ echo "autoload -U compinit; compinit" >> ~/.zshrc | ||
# To load completions for each session, execute once: | ||
$ flytectl completion zsh > "${fpath[1]}/_flytectl" | ||
# You will need to start a new shell for this setup to take effect. | ||
fish: | ||
$ flytectl completion fish | source | ||
# To load completions for each session, execute once: | ||
$ flytectl completion fish > ~/.config/fish/completions/flytectl.fish | ||
PowerShell: | ||
PS> flytectl completion powershell | Out-String | Invoke-Expression | ||
# To load completions for every new session, run: | ||
PS> flytectl completion powershell > flytectl.ps1 | ||
# and source this file from your PowerShell profile. | ||
`, | ||
DisableFlagsInUseLine: true, | ||
ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, | ||
Args: cobra.ExactValidArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
switch args[0] { | ||
case "bash": | ||
return cmd.Root().GenBashCompletion(os.Stdout) | ||
case "zsh": | ||
return cmd.Root().GenZshCompletion(os.Stdout) | ||
case "fish": | ||
return cmd.Root().GenFishCompletion(os.Stdout, true) | ||
case "powershell": | ||
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) | ||
} | ||
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
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,43 @@ | ||
package sandbox | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/enescakir/emoji" | ||
cmdCore "github.com/flyteorg/flytectl/cmd/core" | ||
"github.com/flyteorg/flytectl/pkg/docker" | ||
) | ||
|
||
const ( | ||
statusShort = "Get the status of the sandbox environment." | ||
statusLong = ` | ||
Status will retrieve the status of the Sandbox environment. Currently FlyteSandbox runs as a local docker container. | ||
This will return the docker status for this container | ||
Usage | ||
:: | ||
bin/flytectl sandbox status | ||
` | ||
) | ||
|
||
func sandboxClusterStatus(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error { | ||
cli, err := docker.GetDockerClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return printStatus(ctx, cli) | ||
} | ||
|
||
func printStatus(ctx context.Context, cli docker.Docker) error { | ||
c := docker.GetSandbox(ctx, cli) | ||
if c == nil { | ||
fmt.Printf("%v no Sandbox found \n", emoji.StopSign) | ||
return nil | ||
} | ||
fmt.Printf("Flyte local sandbox cluster container image [%s] with status [%s] is in state [%s]", c.Image, c.Status, c.State) | ||
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,43 @@ | ||
package sandbox | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
cmdCore "github.com/flyteorg/flytectl/cmd/core" | ||
"github.com/flyteorg/flytectl/pkg/docker" | ||
"github.com/flyteorg/flytectl/pkg/docker/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSandboxStatus(t *testing.T) { | ||
t.Run("Sandbox status with zero result", func(t *testing.T) { | ||
ctx := context.Background() | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(nil, *mockOutStream) | ||
mockDocker := &mocks.Docker{} | ||
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return([]types.Container{}, nil) | ||
docker.Client = mockDocker | ||
err := sandboxClusterStatus(ctx, []string{}, cmdCtx) | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("Sandbox status with running sandbox", func(t *testing.T) { | ||
ctx := context.Background() | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(nil, *mockOutStream) | ||
mockDocker := &mocks.Docker{} | ||
mockDocker.OnContainerList(ctx, types.ContainerListOptions{All: true}).Return([]types.Container{ | ||
{ | ||
ID: docker.FlyteSandboxClusterName, | ||
Names: []string{ | ||
docker.FlyteSandboxClusterName, | ||
}, | ||
}, | ||
}, nil) | ||
docker.Client = mockDocker | ||
err := sandboxClusterStatus(ctx, []string{}, cmdCtx) | ||
assert.Nil(t, err) | ||
}) | ||
} |
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