Skip to content

Commit

Permalink
feat: add completion command (#1258)
Browse files Browse the repository at this point in the history
* feat: add completion command

Signed-off-by: tison <wander4096@gmail.com>

* errors already handled at the caller side

Signed-off-by: tison <wander4096@gmail.com>

* implement and refactor

Signed-off-by: tison <wander4096@gmail.com>

* fix: substr over full equality

Signed-off-by: tison <wander4096@gmail.com>

* fix: lint

Signed-off-by: tison <wander4096@gmail.com>

Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Dec 2, 2022
1 parent 0eb4adf commit 0042b24
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,6 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tensorchord/envd-server v0.0.10 h1:ZBcp8LYA6MTdGh+1aShUHInI6HKNoaGn6+pi+Itf4QQ=
github.com/tensorchord/envd-server v0.0.10/go.mod h1:aWTDKCU3B+8gwAKZn7TTpS/QLcnnrtP9fA2/FwDco3M=
github.com/tensorchord/envd-server v0.0.11 h1:pFmfiTcWzdxc9eF5TS+BPtjC03AZq48AGQrFozmlc/4=
github.com/tensorchord/envd-server v0.0.11/go.mod h1:UnN42ypM9RzXcjRX54GuiAtK1rECUxm5TTWga+cy1Wg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
1 change: 1 addition & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func New() EnvdApp {
internalApp.Commands = []*cli.Command{
CommandBootstrap,
CommandCreate,
CommandCompletion,
CommandContext,
CommandBuild,
CommandDestroy,
Expand Down
27 changes: 17 additions & 10 deletions pkg/app/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/cockroachdb/errors"
"github.com/docker/docker/pkg/namesgenerator"
Expand Down Expand Up @@ -170,16 +171,22 @@ func autocomplete(clicontext *cli.Context) error {
return nil
}

// Because this requires sudo, it should warn and not fail the rest of it.
err := ac.InsertBashCompleteEntry()
if err != nil {
logrus.Warnf("Warning: %s\n", err.Error())
err = nil
}
err = ac.InsertZSHCompleteEntry()
if err != nil {
logrus.Warnf("Warning: %s\n", err.Error())
err = nil
shell := os.Getenv("SHELL")
if strings.Contains(shell, "zsh") {
logrus.Infof("Install zsh autocompletion")
if err := ac.InsertZSHCompleteEntry(); err != nil {
logrus.Warnf("Warning: %s\n", err.Error())
}
} else if strings.Contains(shell, "bash") {
logrus.Infof("Install bash autocompletion")
if err := ac.InsertBashCompleteEntry(); err != nil {
logrus.Warnf("Warning: %s\n", err.Error())
}
} else {
logrus.Infof("Install bash autocompletion (fallback from \"%s\")", shell)
if err := ac.InsertBashCompleteEntry(); err != nil {
logrus.Warnf("Warning: %s\n", err.Error())
}
}

logrus.Info("You may have to restart your shell for autocomplete to get initialized (e.g. run \"exec $SHELL\")\n")
Expand Down
65 changes: 65 additions & 0 deletions pkg/app/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 The envd Authors
//
// 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 app

import (
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

ac "github.com/tensorchord/envd/pkg/autocomplete"
)

var CommandCompletion = &cli.Command{
Name: "completion",
Category: CategoryManagement,
Usage: "Install shell completion scripts for envd",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "shell",
Usage: "Shell type to install completion",
Aliases: []string{"s"},
},
},

Action: completion,
}

func completion(clicontext *cli.Context) error {
shellList := clicontext.StringSlice("shell")

n := len(shellList)
if n == 0 {
return errors.Errorf("at least one specified shell type")
}

for i := 0; i < n; i++ {
logrus.Infof("[%d/%d] Add completion %s", i+1, n, shellList[i])
switch shellList[i] {
case "zsh":
if err := ac.InsertZSHCompleteEntry(); err != nil {
return err
}
case "bash":
if err := ac.InsertBashCompleteEntry(); err != nil {
return err
}
default:
return errors.Errorf("unknown shell type %s (support type: {bash|zsh})", shellList[i])
}
}

return nil
}
7 changes: 2 additions & 5 deletions pkg/autocomplete/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package autocomplete

import (
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -63,8 +62,7 @@ func InsertBashCompleteEntry() error {
return errors.Wrapf(err, "failed checking if %s exists", dirPath)
}
if !dirPathExists {
fmt.Fprintf(os.Stderr, "Warning: unable to enable bash-completion: %s does not exist\n", dirPath)
return nil // bash-completion isn't available, silently fail.
return errors.Errorf("unable to enable bash-completion: %s does not exist", dirPath)
}

pathExists, err := fileutil.FileExists(path)
Expand All @@ -84,8 +82,7 @@ func InsertBashCompleteEntry() error {

bashEntry, err := bashCompleteEntry()
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: unable to enable bash-completion: %s\n", err)
return nil // bash-completion isn't available, silently fail.
return errors.Wrapf(err, "unable to enable bash-completion")
}

_, err = f.Write([]byte(bashEntry))
Expand Down
6 changes: 2 additions & 4 deletions pkg/autocomplete/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func InsertZSHCompleteEntry() error {
// check the system has zsh
_, err := exec.LookPath("zsh")
if err != nil {
log.L.Debugf("can't find zsh in this system, stop setting the zsh-completion.")
return nil
return errors.Errorf("can't find zsh in this system, stop setting the zsh-completion.")
}

// should be the same on linux and macOS
Expand Down Expand Up @@ -114,8 +113,7 @@ func InsertZSHCompleteEntry() error {

compEntry, err := zshCompleteEntry()
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: unable to enable zsh-completion: %s\n", err)
return nil // zsh-completion isn't available, silently fail.
return errors.Wrapf(err, "Warning: unable to enable zsh-completion")
}

_, err = f.Write([]byte(compEntry))
Expand Down

0 comments on commit 0042b24

Please sign in to comment.