Skip to content

Commit

Permalink
Update core upstream (#19)
Browse files Browse the repository at this point in the history
* Add SetFailedf and IsDebug helpers

SetFailedf prevents from having to use Sprintf in caller methods

* Backport new ExportVariable and SetPath

Backport implementation from actions/toolkit#571
  • Loading branch information
tjamet committed Nov 2, 2020
1 parent e2e00e7 commit 11a3d51
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions core/command.go
Expand Up @@ -45,6 +45,21 @@ func IssueCommand(kind string, properties map[string]string, message string) {
stdoutSetter.Unlock()
}

// issueFileCommand implements stores the command in a file
// see https://github.com/actions/toolkit/pull/571/files#diff-9ce6eb99f5fb5529e795254801e03ae56d67d3d5fcbec635f91e9a8a61ad8b64R10
func issueFileCommand(command string, message string) error {
path, ok := os.LookupEnv("GITHUB_" + command)
if ok {
fd, err := os.OpenFile(path, os.O_RDWR, 0)
if err != nil {
return err
}
fmt.Fprintln(fd, message)
return nil
}
return fmt.Errorf("unable to find command file GITHUB_%s", command)
}

type command struct {
command string
properties map[string]string
Expand Down
19 changes: 17 additions & 2 deletions core/core.go
Expand Up @@ -22,8 +22,11 @@ var (

// ExportVariable sets the environment varaible name (for this action and future actions)
func ExportVariable(name, value string) {
const delimiter = "_GitHubActionsFileCommandDelimeter_"
if err := issueFileCommand("ENV", fmt.Sprintf("%s<<%s%s%s%s%s", name, delimiter, EOF, value, delimiter, EOF)); err != nil {
IssueCommand("set-env", map[string]string{"name": name}, value)
}
os.Setenv(name, value)
IssueCommand("set-env", map[string]string{"name": name}, value)
}

// SetSecret registers a secret which will get masked from logs
Expand All @@ -33,8 +36,10 @@ func SetSecret(secret string) {

// AddPath prepends inputPath to the PATH (for this action and future actions)
func AddPath(path string) {
if err := issueFileCommand("PATH", path); err != nil {
Issue("add-path", path)
}
// TODO js: process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
Issue("add-path", path)
}

// GetBoolInput gets the value of an input and returns whether it equals "true".
Expand Down Expand Up @@ -63,6 +68,11 @@ func SetOutput(name, value string) {
IssueCommand("set-output", map[string]string{"name": name}, value)
}

// SetFailedf sets the action status to failed and sets an error message
func SetFailedf(format string, args ...interface{}) {
SetFailed(fmt.Sprintf(format, args...))
}

// SetFailed sets the action status to failed and sets an error message
func SetFailed(message string) {
statusAccess.Lock()
Expand Down Expand Up @@ -139,3 +149,8 @@ func SaveState(name, value string) {
func GetState(name string) string {
return os.Getenv("STATE_" + name)
}

// IsDebug returns whether the github actions is currently under debug
func IsDebug() bool {
return os.Getenv("RUNNER_DEBUG") == "1"
}
3 changes: 3 additions & 0 deletions core/core_unix.go
@@ -0,0 +1,3 @@
package core

const EOF = "\n"
3 changes: 3 additions & 0 deletions core/core_windows.go
@@ -0,0 +1,3 @@
package core

const EOF = "\r\n"

0 comments on commit 11a3d51

Please sign in to comment.