Skip to content

Commit

Permalink
fix: 修改windows停止进程时的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Lack30 committed Oct 25, 2021
1 parent 16d6267 commit 22a1b7c
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 119 deletions.
4 changes: 1 addition & 3 deletions api/service/gpm/v1/gpm.pb.vine.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

194 changes: 99 additions & 95 deletions api/types/gpm/v1/gpm.pb.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/types/gpm/v1/gpm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ message ExecIn {
}

message ExecResult {
string result = 1;
bytes result = 1;
}

message PullResult {
Expand Down Expand Up @@ -258,6 +258,6 @@ message TerminalIn {
message TerminalResult {
bytes stdout = 1;
bytes stderr = 2;
string error = 3;
bytes error = 3;
bool isOk = 4;
}
2 changes: 1 addition & 1 deletion cmd/gpm/pkg/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func execBash(c *cli.Context) error {
return fmt.Errorf("%v", verrs.FromErr(err).Detail)
}

fmt.Fprintln(outE, result.Result)
fmt.Fprintln(outE, string(result.Result))

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gpm/pkg/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func terminalBash(c *cli.Context) error {
if err != nil {
return errors.New(status.Convert(err).Message())
}
if b.Error != "" {
fmt.Fprintf(outE, b.Error)
if text := string(b.Error); text != "" {
fmt.Fprintf(outE, text)
}
if b.Stderr != nil {
fmt.Fprintf(outE, string(b.Stderr))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/vine-io/pkg/unit v0.1.0
github.com/vine-io/plugins/logger/zap v1.1.0
github.com/vine-io/vine v1.3.2
golang.org/x/text v0.3.5
google.golang.org/grpc v1.39.0
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
2 changes: 1 addition & 1 deletion pkg/biz/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (s *sftp) Exec(ctx context.Context, in *gpmv1.ExecIn) (*gpmv1.ExecResult, e

b, err := cmd.CombinedOutput()
if err != nil {
return nil, verrs.InternalServerError(s.S.Name(), "exec %w: %v", err, beauty(b))
return nil, verrs.InternalServerError(s.S.Name(), "exec %v: %v", err, beauty(b))
}

out := &gpmv1.ExecResult{
Expand Down
6 changes: 3 additions & 3 deletions pkg/biz/gpm_drawin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
package biz

import (
"bytes"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"

gpmv1 "github.com/vine-io/gpm/api/types/gpm/v1"
Expand Down Expand Up @@ -170,6 +170,6 @@ func startTerminal(in *gpmv1.TerminalIn) *exec.Cmd {
return cmd
}

func beauty(b []byte) string {
return strings.TrimSuffix(string(b), "\n")
func beauty(b []byte) []byte {
return bytes.TrimSuffix(b, []byte("\n"))
}
6 changes: 3 additions & 3 deletions pkg/biz/gpm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
package biz

import (
"bytes"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"

gpmv1 "github.com/vine-io/gpm/api/types/gpm/v1"
Expand Down Expand Up @@ -167,6 +167,6 @@ func startTerminal(in *gpmv1.TerminalIn) *exec.Cmd {
return cmd
}

func beauty(b []byte) string {
return strings.TrimSuffix(string(b), "\n")
func beauty(b []byte) []byte {
return bytes.TrimSuffix(b, []byte("\n"))
}
12 changes: 9 additions & 3 deletions pkg/biz/gpm_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//go:build windows
// +build windows

package biz

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"

gpmv1 "github.com/vine-io/gpm/api/types/gpm/v1"
"golang.org/x/text/transform"
"golang.org/x/text/encoding/simplifiedchinese"
)

func fillService(service *gpmv1.Service) error {
Expand Down Expand Up @@ -89,6 +93,8 @@ func startTerminal(in *gpmv1.TerminalIn) *exec.Cmd {
return cmd
}

func beauty(b []byte) string {
return strings.TrimSuffix(string(b), "\r\n")
func beauty(b []byte) []byte {
reader := transform.NewReader(bytes.NewReader(bytes.TrimSuffix(b, []byte("\r\n"))), simplifiedchinese.GBK.NewEncoder())
d, _ := ioutil.ReadAll(reader)
return d
}
21 changes: 15 additions & 6 deletions pkg/biz/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,18 @@ func (p *Process) Stop() error {
return ErrProcessNotFound
}

close(p.done)
go func() {
close(p.done)
defer func() {
if e := recover(); e != nil {
log.Errorf("close %s channel", p.Name)
}
}()
}()

if runtime.GOOS == "windows" {
return p.kill()
}

return p.stop()
}
Expand All @@ -300,11 +311,9 @@ func (p *Process) stop() error {
if err != nil {
return err
}
if runtime.GOOS == "windows" {
err = pr.Kill()
} else {
err = pr.Signal(syscall.SIGINT)
}

err = pr.Signal(syscall.SIGINT)

if err != nil {
return err
}
Expand Down

0 comments on commit 22a1b7c

Please sign in to comment.