1// Copyright 2019 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Package gapicgen provides some helpers for gapicgen binaries. 16package gapicgen 17 18import ( 19 "fmt" 20 "os" 21 "os/exec" 22) 23 24// SetGitCreds sets credentials for gerrit. 25func SetGitCreds(githubName, githubEmail, gerritCookieName, gerritCookieValue string) error { 26 c := exec.Command("git", "config", "--global", "user.name", githubName) 27 c.Stdout = os.Stdout 28 c.Stderr = os.Stderr 29 c.Stdin = os.Stdin // Prevents "the input device is not a TTY" error. 30 c.Env = []string{ 31 fmt.Sprintf("PATH=%s", os.Getenv("PATH")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 32 fmt.Sprintf("HOME=%s", os.Getenv("HOME")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 33 } 34 if err := c.Run(); err != nil { 35 return err 36 } 37 38 c = exec.Command("git", "config", "--global", "user.email", githubEmail) 39 c.Stdout = os.Stdout 40 c.Stderr = os.Stderr 41 c.Stdin = os.Stdin // Prevents "the input device is not a TTY" error. 42 c.Env = []string{ 43 fmt.Sprintf("PATH=%s", os.Getenv("PATH")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 44 fmt.Sprintf("HOME=%s", os.Getenv("HOME")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 45 } 46 if err := c.Run(); err != nil { 47 return err 48 } 49 50 cmd := fmt.Sprintf(` 51set -ex 52 53eval 'set +o history' 2>/dev/null || setopt HIST_IGNORE_SPACE 2>/dev/null 54touch ~/.gitcookies 55chmod 0600 ~/.gitcookies 56 57git config --global http.cookiefile ~/.gitcookies 58 59tr , \\t <<\__END__ >>~/.gitcookies 60code.googlesource.com,FALSE,/,TRUE,2147483647,%s,%s 61code-review.googlesource.com,FALSE,/,TRUE,2147483647,%s,%s 62__END__ 63eval 'set -o history' 2>/dev/null || unsetopt HIST_IGNORE_SPACE 2>/dev/null 64`, gerritCookieName, gerritCookieValue, gerritCookieName, gerritCookieValue) 65 c = exec.Command("/bin/bash", "-c", cmd) 66 c.Stdout = os.Stdout 67 c.Stderr = os.Stderr 68 c.Stdin = os.Stdin // Prevents "the input device is not a TTY" error. 69 c.Env = []string{ 70 fmt.Sprintf("PATH=%s", os.Getenv("PATH")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 71 fmt.Sprintf("HOME=%s", os.Getenv("HOME")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. 72 } 73 return c.Run() 74} 75