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

Error coder sh commands if the env requires a rebuild #163

Merged
merged 4 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ type RebuildMessage struct {
Text string `json:"text"`
Required bool `json:"required"`
AutoOffThreshold xjson.MSDuration `json:"auto_off_threshold" tab:"-"`
RebuildMessages []struct {
Text string `json:"text"`
Required bool `json:"required"`
} `json:"rebuild_messages" tab:"-"`
}

// EnvironmentStat represents the state of an environment
Expand Down
1 change: 1 addition & 0 deletions docs/coder_sh.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ coder sh [environment_name] [<command [args...]>] [flags]

```
coder sh backend-env
coder sh front-end-dev cat ~/config.json
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Make() *cobra.Command {
app.AddCommand(
makeLoginCmd(),
makeLogoutCmd(),
makeShellCmd(),
shCmd(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the rename of this particular command? Consistency seems better here, so we should change them all or not change this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah fair point. Just updated all uses of make*

makeUsersCmd(),
makeConfigSSHCmd(),
makeSecretsCmd(),
Expand Down
25 changes: 19 additions & 6 deletions internal/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (

func getEnvsForCompletion(user string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
client, err := newClient()
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
envs, err := getEnvs(context.TODO(), client, user)
envs, err := getEnvs(ctx, client, user)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
Expand All @@ -40,7 +41,7 @@ func getEnvsForCompletion(user string) func(cmd *cobra.Command, args []string, t
}
}

func makeShellCmd() *cobra.Command {
func shCmd() *cobra.Command {
return &cobra.Command{
Use: "sh [environment_name] [<command [args...]>]",
Short: "Open a shell and execute commands in a Coder environment",
Expand All @@ -49,13 +50,13 @@ func makeShellCmd() *cobra.Command {
DisableFlagParsing: true,
ValidArgsFunction: getEnvsForCompletion(coder.Me),
RunE: shell,
Example: "coder sh backend-env",
Example: `coder sh backend-env
coder sh front-end-dev cat ~/config.json`,
}
}

func shell(_ *cobra.Command, cmdArgs []string) error {
ctx := context.Background()

func shell(cmd *cobra.Command, cmdArgs []string) error {
ctx := cmd.Context()
command := "sh"
args := []string{"-c"}
if len(cmdArgs) > 1 {
Expand Down Expand Up @@ -106,6 +107,18 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
return xerrors.Errorf("find environment: %w", err)
}

// check if a rebuild is required before attempting to open a shell
for _, r := range env.RebuildMessages {
// use the first rebuild message that is required
if r.Required {
return clog.Error(
fmt.Sprintf(`environment "%s" requires a rebuild`, env.Name),
clog.Causef(r.Text), clog.BlankLine,
clog.Tipf(`run "coder envs rebuild %s" to rebuild`, env.Name),
)
}
}

termFD := os.Stdout.Fd()

isInteractive := terminal.IsTerminal(int(termFD))
Expand Down