github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/test/cmd/lfs-ssh-echo.go (about)

     1  // +build testtools
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  type sshResponse struct {
    14  	Href      string            `json:"href"`
    15  	Header    map[string]string `json:"header"`
    16  	ExpiresAt time.Time         `json:"expires_at,omitempty"`
    17  	ExpiresIn int               `json:"expires_in,omitempty"`
    18  }
    19  
    20  func main() {
    21  	// expect args:
    22  	//   lfs-ssh-echo -p PORT -- git@127.0.0.1 git-lfs-authenticate REPO OPERATION
    23  	if len(os.Args) != 6 {
    24  		fmt.Fprintf(os.Stderr, "got %d args: %v", len(os.Args), os.Args)
    25  		os.Exit(1)
    26  	}
    27  
    28  	if os.Args[1] != "-p" {
    29  		fmt.Fprintf(os.Stderr, "$1 expected \"-p\", got %q", os.Args[1])
    30  		os.Exit(1)
    31  	}
    32  
    33  	if os.Args[3] != "--" {
    34  		fmt.Fprintf(os.Stderr, "$3 expected \"--\", got %q", os.Args[3])
    35  		os.Exit(1)
    36  	}
    37  
    38  	if os.Args[4] != "git@127.0.0.1" {
    39  		fmt.Fprintf(os.Stderr, "$4 expected \"git@127.0.0.1\", got %q", os.Args[4])
    40  		os.Exit(1)
    41  	}
    42  
    43  	// just "git-lfs-authenticate REPO OPERATION"
    44  	authLine := strings.Split(os.Args[5], " ")
    45  	if len(authLine) < 13 {
    46  		fmt.Fprintf(os.Stderr, "bad git-lfs-authenticate line: %s\nargs: %v", authLine, os.Args)
    47  	}
    48  
    49  	repo := authLine[1]
    50  
    51  	r := &sshResponse{
    52  		Href: fmt.Sprintf("http://127.0.0.1:%s/%s.git/info/lfs", os.Args[2], repo),
    53  	}
    54  	switch repo {
    55  	case "ssh-expired-absolute":
    56  		r.ExpiresAt = time.Now().Add(-5 * time.Minute)
    57  	case "ssh-expired-relative":
    58  		r.ExpiresIn = -5
    59  	case "ssh-expired-both":
    60  		r.ExpiresAt = time.Now().Add(-5 * time.Minute)
    61  		r.ExpiresIn = -5
    62  	}
    63  
    64  	json.NewEncoder(os.Stdout).Encode(r)
    65  }