Skip to content

Commit

Permalink
fix: add revive and address indent-error-flow issues (#1480)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhizhen He <hezhizhen.yi@gmail.com>
  • Loading branch information
hezhizhen committed Feb 16, 2023
1 parent 083e3eb commit 7d8fc46
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 49 deletions.
9 changes: 7 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
linters:
enable:
- gofmt
- bodyclose
- errcheck
- goimports
- errorlint
- exportloopref
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- misspell
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
Expand All @@ -20,3 +21,7 @@ linters:
linters-settings:
goimports:
local-prefixes: github.com/tensorchord/envd/
revive:
rules:
# available rules: https://github.com/mgechev/revive#available-rules
- name: indent-error-flow
11 changes: 6 additions & 5 deletions cmd/envd-sshd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func sshServer(c *cli.Context) error {
port := c.Int(flagPort)
if port == 0 {
return errors.New("port must be set")
} else if port <= 1024 {
}
if port <= 1024 {
return errors.New("failed to parse port: port is reserved")
}

Expand Down Expand Up @@ -141,12 +142,12 @@ func sshServer(c *cli.Context) error {
return errors.Wrapf(
err, "reading private key %s failed", c.String(flagHostKey))
}
if privateKey, err := rawssh.ParsePrivateKey(pemBytes); err != nil {
privateKey, err := rawssh.ParsePrivateKey(pemBytes)
if err != nil {
return err
} else {
logrus.Debugf("load host key from %s", c.String(flagHostKey))
hostKey = privateKey
}
logrus.Debugf("load host key from %s", c.String(flagHostKey))
hostKey = privateKey
}

srv := sshd.Server{
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func exec(clicontext *cli.Context) error {
}

logrus.Debugf("runtime commands: %s", rg.RuntimeCommands)
if cmd, ok := rg.RuntimeCommands[command]; !ok {
cmd, ok := rg.RuntimeCommands[command]
if !ok {
return errors.Newf("command %s does not exist", command)
} else {
resultCommand = cmd
}
resultCommand = cmd
}

opt, err := ssh.GetOptions(name)
Expand All @@ -128,12 +128,12 @@ func exec(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to get the ssh client")
}
if bytes, err := sshClient.ExecWithOutput(resultCommand); err != nil {
bytes, err := sshClient.ExecWithOutput(resultCommand)
if err != nil {
fmt.Fprintln(clicontext.App.Writer, string(bytes))
return errors.Wrapf(err,
"failed to execute the command `%s`", resultCommand)
} else {
fmt.Fprint(clicontext.App.Writer, string(bytes))
}
fmt.Fprint(clicontext.App.Writer, string(bytes))
return nil
}
3 changes: 1 addition & 2 deletions pkg/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,8 @@ func (b generalBuilder) build(ctx context.Context, pw progresswriter.Writer) err
// Close the pipe on cancels, otherwise the whole thing hangs.
pipeR.Close()
return errors.Wrap(err, "build cancelled")
} else {
return errors.Wrap(err, "failed to wait error group")
}
return errors.Wrap(err, "failed to wait error group")
}
b.logger.Debug("build successfully")
return nil
Expand Down
17 changes: 8 additions & 9 deletions pkg/envd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ func New(ctx context.Context, opt Options) (Engine, error) {
Client: cli,
Loginname: ac.Name,
}, nil
} else {
cli, err := client.NewClientWithOpts(
client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, errors.Wrap(err, "failed to create the docker client")
}
return &dockerEngine{
Client: cli,
}, nil
}
cli, err := client.NewClientWithOpts(
client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, errors.Wrap(err, "failed to create the docker client")
}
return &dockerEngine{
Client: cli,
}, nil
}
6 changes: 3 additions & 3 deletions pkg/lang/ir/v0/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (g generalGraph) compileVSCode() (*llb.State, error) {
return nil, errors.Wrap(err, "failed to create vscode client")
}
g.Writer.LogVSCodePlugin(p, compileui.ActionStart, false)
if cached, err := vscodeClient.DownloadOrCache(p); err != nil {
cached, err := vscodeClient.DownloadOrCache(p)
if err != nil {
return nil, err
} else {
g.Writer.LogVSCodePlugin(p, compileui.ActionEnd, cached)
}
g.Writer.LogVSCodePlugin(p, compileui.ActionEnd, cached)
ext := llb.Scratch().File(llb.Copy(llb.Local(flag.FlagCacheDir),
vscodeClient.PluginPath(p),
fileutil.EnvdHomeDir(".vscode-server", "extensions", p.String()),
Expand Down
6 changes: 3 additions & 3 deletions pkg/lang/ir/v0/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func (g generalGraph) compileZSH(root llb.State) (llb.State, error) {
ohMyZSHPath := fileutil.EnvdHomeDir(".oh-my-zsh")
m := shell.NewManager()
g.Writer.LogZSH(compileui.ActionStart, false)
if cached, err := m.DownloadOrCache(); err != nil {
cached, err := m.DownloadOrCache()
if err != nil {
return llb.State{}, errors.Wrap(err, "failed to download oh-my-zsh")
} else {
g.Writer.LogZSH(compileui.ActionEnd, cached)
}
g.Writer.LogZSH(compileui.ActionEnd, cached)
zshStage := root.
File(llb.Copy(llb.Local(flag.FlagCacheDir), "oh-my-zsh", ohMyZSHPath,
&llb.CopyInfo{CreateDestPath: true}, llb.WithUIDGID(g.uid, g.gid))).
Expand Down
6 changes: 3 additions & 3 deletions pkg/lang/ir/v1/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (g generalGraph) compileVSCode() (llb.State, error) {
return llb.State{}, errors.Wrap(err, "failed to create vscode client")
}
g.Writer.LogVSCodePlugin(p, compileui.ActionStart, false)
if cached, err := vscodeClient.DownloadOrCache(p); err != nil {
cached, err := vscodeClient.DownloadOrCache(p)
if err != nil {
return llb.State{}, err
} else {
g.Writer.LogVSCodePlugin(p, compileui.ActionEnd, cached)
}
g.Writer.LogVSCodePlugin(p, compileui.ActionEnd, cached)
ext := llb.Scratch().File(llb.Copy(llb.Local(flag.FlagCacheDir),
vscodeClient.PluginPath(p),
fileutil.EnvdHomeDir(".vscode-server", "extensions", p.String()),
Expand Down
6 changes: 3 additions & 3 deletions pkg/lang/ir/v1/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func (g generalGraph) compileZSH(root llb.State) (llb.State, error) {
ohMyZSHPath := fileutil.EnvdHomeDir(".oh-my-zsh")
m := shell.NewManager()
g.Writer.LogZSH(compileui.ActionStart, false)
if cached, err := m.DownloadOrCache(); err != nil {
cached, err := m.DownloadOrCache()
if err != nil {
return llb.State{}, errors.Wrap(err, "failed to download oh-my-zsh")
} else {
g.Writer.LogZSH(compileui.ActionEnd, cached)
}
g.Writer.LogZSH(compileui.ActionEnd, cached)
zshStage := root.
File(llb.Copy(llb.Local(flag.FlagCacheDir), "oh-my-zsh", ohMyZSHPath,
&llb.CopyInfo{CreateDestPath: true})).
Expand Down
9 changes: 3 additions & 6 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,20 @@ func parsePemBlock(block *pem.Block) (interface{}, error) {
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, errors.Newf("Parsing PKCS private key failed %w", err)
} else {
return key, nil
}
return key, nil
case "EC PRIVATE KEY":
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, errors.Newf("Parsing EC private key failed %w", err)
} else {
return key, nil
}
return key, nil
case "DSA PRIVATE KEY":
key, err := ssh.ParseDSAPrivateKey(block.Bytes)
if err != nil {
return nil, errors.Newf("Parsing DSA private key failed %w", err)
} else {
return key, nil
}
return key, nil
default:
return nil, errors.Newf("Parsing private key failed, unsupported key type %q", block.Type)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/util/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func GetFreePort() (int, error) {

// GetHost get the IP address from the address.
func GetHost(addr string) (string, error) {
if u, err := url.Parse(addr); err != nil {
u, err := url.Parse(addr)
if err != nil {
return "", err
} else {
h := u.Hostname()
if h == "" {
return "", fmt.Errorf("failed to get the hostname from %s", addr)
}
return h, nil
}
h := u.Hostname()
if h == "" {
return "", fmt.Errorf("failed to get the hostname from %s", addr)
}
return h, nil
}

0 comments on commit 7d8fc46

Please sign in to comment.