github.com/naphatkrit/deis@v1.12.3/client/pkg/git/git.go (about)

     1  package git
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path"
    10  	"strings"
    11  )
    12  
    13  // CreateRemote adds a git remote in the current directory.
    14  func CreateRemote(host, remote, appID string) error {
    15  	cmd := exec.Command("git", "remote", "add", remote, RemoteURL(host, appID))
    16  	stderr, err := cmd.StderrPipe()
    17  
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	if err = cmd.Start(); err != nil {
    23  		return err
    24  	}
    25  
    26  	output, _ := ioutil.ReadAll(stderr)
    27  	fmt.Print(string(output))
    28  
    29  	if err := cmd.Wait(); err != nil {
    30  		return err
    31  	}
    32  
    33  	fmt.Printf("Git remote %s added\n", remote)
    34  
    35  	return nil
    36  }
    37  
    38  // DeleteRemote removes a git remote in the current directory.
    39  func DeleteRemote(appID string) error {
    40  	name, err := remoteNameFromAppID(appID)
    41  
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	if _, err = exec.Command("git", "remote", "remove", name).Output(); err != nil {
    47  		return err
    48  	}
    49  
    50  	fmt.Printf("Git remote %s removed\n", name)
    51  
    52  	return nil
    53  }
    54  
    55  func remoteNameFromAppID(appID string) (string, error) {
    56  	out, err := exec.Command("git", "remote", "-v").Output()
    57  
    58  	if err != nil {
    59  		return "", err
    60  	}
    61  
    62  	cmd := string(out)
    63  
    64  	for _, line := range strings.Split(cmd, "\n") {
    65  		if strings.Contains(line, appID) {
    66  			return strings.Split(line, "\t")[0], nil
    67  		}
    68  	}
    69  
    70  	return "", errors.New("Could not find remote matching app in 'git remote -v'")
    71  }
    72  
    73  // DetectAppName detects if there is deis remote in git.
    74  func DetectAppName(host string) (string, error) {
    75  	remote, err := findRemote(host)
    76  
    77  	// Don't return an error if remote can't be found, return directory name instead.
    78  	if err != nil {
    79  		dir, err := os.Getwd()
    80  		return strings.ToLower(path.Base(dir)), err
    81  	}
    82  
    83  	ss := strings.Split(remote, "/")
    84  	return strings.Split(ss[len(ss)-1], ".")[0], nil
    85  }
    86  
    87  func findRemote(host string) (string, error) {
    88  	out, err := exec.Command("git", "remote", "-v").Output()
    89  
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  
    94  	cmd := string(out)
    95  
    96  	for _, line := range strings.Split(cmd, "\n") {
    97  		for _, remote := range strings.Split(line, " ") {
    98  			if strings.Contains(remote, host) {
    99  				return strings.Split(remote, "\t")[1], nil
   100  			}
   101  		}
   102  	}
   103  
   104  	return "", errors.New("Could not find deis remote in 'git remote -v'")
   105  }
   106  
   107  // RemoteURL returns the git URL of app.
   108  func RemoteURL(host, appID string) string {
   109  	return fmt.Sprintf("ssh://git@%s:2222/%s.git", host, appID)
   110  }