Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell #17

Open
asu126 opened this issue Jan 18, 2019 · 2 comments
Open

shell #17

asu126 opened this issue Jan 18, 2019 · 2 comments

Comments

@asu126
Copy link
Owner

asu126 commented Jan 18, 2019

shell

package main

import (
	"bytes"
	"fmt"
	//"github.com/gliderlabs/ssh"
	"io"
	// "log"
	"os"
	"os/exec"
	"sync"
)

// func (l *loggingResponseWriter) Write(data []byte) (n int, err error) {
// 	if l.status == 0 {
// 		l.WriteHeader(http.StatusOK)
// 	}
// 	n, err = l.rw.Write(data)
// 	l.written += int64(n)
// 	return n, err
// }

// var execCommand = exec.Command

// // Git subprocess helpers
// func gitCommand(gl_id string, name string, RepoPath string, args ...string) *exec.Cmd {
// 	cmd := execCommand(name, args...)
// 	// Start the command in its own process group (nice for signalling)
// 	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
// 	// Explicitly set the environment for the Git command
// 	cmd.Env = []string{
// 		fmt.Sprintf("HOME=%s", os.Getenv("HOME")),
// 		fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
// 		fmt.Sprintf("LD_LIBRARY_PATH=%s", os.Getenv("LD_LIBRARY_PATH")),
// 		fmt.Sprintf("GL_ID=%s", gl_id),
// 		fmt.Sprintf("GL_PROTOCOL=http"),
// 		fmt.Sprintf("REPO_PATH=%s", RepoPath),
// 	}
// 	// If we don't do something with cmd.Stderr, Git errors will be lost
// 	cmd.Stderr = os.Stderr
// 	return cmd
// }

// func main() {
// 	var stdout bytes.Buffer
// 	cmd := exec.Command("date")
// 	cmd.Stdout = &stdout

// 	cmd.Stdout = &stdout
// 	if err := cmd.Run(); err != nil {
// 		fmt.Fprintln(os.Stderr)
// 		os.Exit(1)
// 	}

// 	// Len returns the number of bytes of the unread portion of the buffer; b.Len() == len(b.Bytes()).
// 	fmt.Println(stdout.Len())
// 	stdout.WriteTo(os.Stdout)
// 	//return nil
// 	os.Exit(0)
// }

func main() {
	var gitCmd *exec.Cmd
	repoPath := "/home/git/gitlab-development-kit/repositories/beego/beego.git"
	gitCmd = exec.Command("git-upload-pack", repoPath)
	gitCmd.Env = os.Environ()
	var output bytes.Buffer
	gitCmd.Env = append(gitCmd.Env, "GL_PROTOCOL=ssh")
	gitCmd.Env = append(gitCmd.Env, "GL_PROTOCOL=/home/git/gitlab-development-kit/repositories/beego/beego.git")
	gitCmd.Dir = repoPath
	gitCmd.Stdin = os.Stdin
	// gitCmd.Stdout = os.Stdout
	stdout, _ := gitCmd.StdoutPipe()
	writer := io.MultiWriter(os.Stdout, &output)

	var waitGroup sync.WaitGroup
	waitGroup.Add(1)
	go func() {
		defer waitGroup.Done()
		io.Copy(writer, stdout)
	}()

	gitCmd.Stderr = os.Stderr
	if err := gitCmd.Run(); err != nil {
		fmt.Fprintln(os.Stderr)
		os.Exit(1)
	}
	waitGroup.Wait()

	//	fmt.Println(stdout.Len())
	//	stdout.WriteTo(os.Stdout)
	//return nil
	os.Exit(0)

}

@asu126
Copy link
Owner Author

asu126 commented Jan 21, 2019

v0

func main() {
        var gitCmd *exec.Cmd
        repoPath := "/home/git/gitlab-development-kit/repositories/beego/beego.git"
        gitCmd = exec.Command("git-upload-pack", repoPath)
        gitCmd.Env = os.Environ()
        gitCmd.Env = append(gitCmd.Env, "GL_PROTOCOL=ssh")
        gitCmd.Env = append(gitCmd.Env, "GL_PROTOCOL=/home/git/gitlab-development-kit/repositories/beego/beego.git")
        gitCmd.Dir = repoPath
        gitCmd.Stdin = os.Stdin
        gitCmd.Stdout = os.Stdout
        gitCmd.Stderr = os.Stderr
        if err := gitCmd.Run(); err != nil {
                fmt.Fprintln(os.Stderr)
                os.Exit(1)
        }

        //return nil
        os.Exit(0)

}

@asu126
Copy link
Owner Author

asu126 commented Sep 5, 2019

  • v2
package main

import (
	"io"
	"os"
	"os/exec"

	"gitlab.com/gitlab-org/gitlab-shell/go/internal/handler"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/logger"
)

func init() {
	logger.ProgName = "go-git-upload-pack"
}

type GitSSHResponseWriter struct {
	write      io.Writer
	writeBytes int64
}

func NewGitSSHResponseWriter(w io.Writer) *GitSSHResponseWriter {
	return &GitSSHResponseWriter{
		write: w,
	}
}

// Write(p []byte) (n int, err error)
func (w *GitSSHResponseWriter) Write(data []byte) (n int, err error) {
	n, err = w.write.Write(data)
	w.writeBytes += int64(n)
	return n, err
}

func main() {
	var timestamp int64 = logger.MakeTimestamp()
	if err := handler.Prepare(); err != nil {
		logger.Fatal("preparation failed", err)
	}

	repoPath := os.Args[1]
	var gitCmd *exec.Cmd
	gitCmd = exec.Command("git-upload-pack", repoPath)
	gitCmd.Dir = repoPath
	gitCmd.Env = os.Environ()

	gitCmd.Stdin = os.Stdin
	gitCmd.Stderr = os.Stderr
	// gitCmd.Stdout = os.Stdout
	var writer *GitSSHResponseWriter = NewGitSSHResponseWriter(os.Stdout)
	gitCmd.Stdout = writer

	// stdout, err := gitCmd.StdoutPipe()
	// if err != nil {
	// 	logger.Fatal("gitCmd StdoutPipe error", err)
	// }

	// writer := io.MultiWriter(os.Stdout, &output)

	if err := gitCmd.Start(); err != nil {
		logger.Fatal("gitCmd Start error", err)
	}

	// writeBytes, err := io.Copy(writer, stdout)
	// if err != nil {
	// 	logger.Fatal("gitCmd io Copy error", err)
	// }

	if err := gitCmd.Wait(); err != nil {
		logger.Fatal("gitCmd Wait error", err)
	}

	logger.FlowRecord(os.Getenv("REPO_NAME"), os.Getenv("REMOTE_IP"), os.Getenv("REMOTE_USER"), timestamp, writer.writeBytes)
	os.Exit(0)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant