Files
Marwan Sulaiman dfb7887080 Allow Athens to Propagate Authentication to Mod Download (#1650)
* Allow Athens to Propagate Authentication to Mod Download

* update readme

* add pattern matching to auth propagation

* Propagate authentication to pre declared static host

* quote redis test

* fix flaky redis error message

* fix config tests

* fix config tests

* Update config.dev.toml

Co-authored-by: Ted Wexler <ted@stuckinacan.com>

* gofmt

Co-authored-by: Ted Wexler <ted@stuckinacan.com>
2020-07-30 17:06:53 -04:00

66 lines
1.6 KiB
Go

package actions
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"runtime"
"strings"
"github.com/mitchellh/go-homedir"
)
// initializeAuthFile checks if provided auth file is at a pre-configured path
// and moves to home directory -- note that this will override whatever
// .netrc/.hgrc file you have in your home directory.
func initializeAuthFile(path string) {
if path == "" {
return
}
fileBts, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("could not read %s: %v", path, err)
}
hdir, err := homedir.Dir()
if err != nil {
log.Fatalf("could not get homedir: %v", err)
}
fileName := transformAuthFileName(filepath.Base(path))
rcp := filepath.Join(hdir, fileName)
if err := ioutil.WriteFile(rcp, fileBts, 0600); err != nil {
log.Fatalf("could not write to file: %v", err)
}
}
// netrcFromToken takes a github token and creates a .netrc
// file for you, overriding whatever might be already there.
func netrcFromToken(tok string) {
fileContent := fmt.Sprintf("machine github.com login %s\n", tok)
hdir, err := homedir.Dir()
if err != nil {
log.Fatalf("netrcFromToken: could not get homedir: %v", err)
}
rcp := filepath.Join(hdir, getNETRCFilename())
if err := ioutil.WriteFile(rcp, []byte(fileContent), 0600); err != nil {
log.Fatalf("netrcFromToken: could not write to file: %v", err)
}
}
func transformAuthFileName(authFileName string) string {
if root := strings.TrimLeft(authFileName, "._"); root == "netrc" {
return getNETRCFilename()
}
return authFileName
}
func getNETRCFilename() string {
if runtime.GOOS == "windows" {
return "_netrc"
}
return ".netrc"
}