Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [CI-14828] : Added Error and Output Secret Handling. #43

Merged
merged 29 commits into from
Dec 9, 2024
Merged
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
06247f4
Added Error and Output Secret Handling.
DevanshMathur19 Oct 22, 2024
070cccc
Update harness/variables.go
Ompragash Oct 29, 2024
e92ac96
Update harness/variables.go
Ompragash Oct 29, 2024
60cff6e
Update harness/variables.go
Ompragash Oct 29, 2024
c73ad8c
Update harness/variables.go
Ompragash Oct 29, 2024
152cb7c
Using CI_ERROR_MESSAGE and CI_ERROR_CODE
DevanshMathur19 Nov 13, 2024
d3480b4
Using CI_ERROR_MESSAGE and CI_ERROR_CODE
DevanshMathur19 Nov 13, 2024
c3cb2ba
Fixing go.mod
DevanshMathur19 Nov 13, 2024
5f6c33d
Update harness/variables.go
Ompragash Nov 13, 2024
daa1d98
Update harness/variables.go
Ompragash Nov 13, 2024
49acda8
Update harness/variables.go
Ompragash Nov 13, 2024
108862e
Update harness/variables.go
Ompragash Nov 13, 2024
cb2df35
Update harness/variables.go
Ompragash Nov 13, 2024
0594eed
Update harness/variables.go
Ompragash Nov 13, 2024
ca32e59
Updating variables and functions.
DevanshMathur19 Nov 18, 2024
7fdd7d8
Update harness/variables.go
Ompragash Nov 19, 2024
bbf67d8
Adding new functions and updating Update and Delete methods.
DevanshMathur19 Nov 20, 2024
85e2820
Optimizing code .
DevanshMathur19 Dec 2, 2024
deedf89
Fix staticcheck issue in .drone.yml
Ompragash Dec 2, 2024
3de8df9
Update .drone.yml
Ompragash Dec 2, 2024
ad9dad0
Merge pull request #44 from drone-plugins/fix-staticcheck
Ompragash Dec 2, 2024
05a2bbb
Adding UT's.
DevanshMathur19 Dec 3, 2024
4891b90
Optimizing Tests.
DevanshMathur19 Dec 3, 2024
cc345e6
Fixing lint errors.
DevanshMathur19 Dec 3, 2024
d92cff4
Rebasing and fixing.
DevanshMathur19 Dec 3, 2024
b8d1f0c
Merge branch 'CI-14828' of https://github.com/DevanshMathur19/drone-p…
DevanshMathur19 Dec 3, 2024
ff7bb9a
Changing functionality of ParseKeyValue and adding tests.
DevanshMathur19 Dec 5, 2024
4f13a99
Adding multiline tests.
DevanshMathur19 Dec 5, 2024
ba76946
Using godotenv for .env files and new tests.
DevanshMathur19 Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions harness/variables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package harness

import (
"fmt"
"os"
"path/filepath"
"strings"
)

const (
CIErrorMessageKey = "CI_ERROR_MESSAGE"
CIErrorCodeKey = "CI_ERROR_CODE"
CIMetadataFileEnv = "CI_ERROR_METADATA"
DroneOutputFileEnv = "DRONE_OUTPUT"
)

// SetSecret sets a new secret by adding it to the DRONE_OUTPUT file
func SetSecret(name, value string) error {
DevanshMathur19 marked this conversation as resolved.
Show resolved Hide resolved
return WriteEnvToOutputFile(DroneOutputFileEnv, name, value)
}

// UpdateSecret updates an existing secret with a new value in the DRONE_OUTPUT file
func UpdateSecret(name, value string) error {
return WriteEnvToOutputFile(DroneOutputFileEnv, name, value)
}

// DeleteSecret removes a secret by setting it to an empty value in the DRONE_OUTPUT file
func DeleteSecret(name string) error {
return WriteEnvToOutputFile(DroneOutputFileEnv, name, "")
}

// SetError sets the error message and error code, writing them to the CI_ERROR_METADATA file
func SetError(message, code string) error {
if err := WriteEnvToOutputFile(CIMetadataFileEnv, CIErrorMessageKey, message); err != nil {
return err
}
return WriteEnvToOutputFile(CIMetadataFileEnv, CIErrorCodeKey, code)
}

// WriteEnvToFile writes a key-value pair to the specified file, determined by an environment variable
func WriteEnvToOutputFile(envVar, key, value string) error {
// Get the file path from the specified environment variable
filePath := os.Getenv(envVar)
if filePath == "" {
return fmt.Errorf("environment variable %s is not set", envVar)
}

// Check the extension of the file (.env or .out)
ext := strings.ToLower(filepath.Ext(filePath))

var content string
if ext == ".env" {
// Write in .env format (KEY=VALUE)
content = fmt.Sprintf("%s=%s\n", key, value)
} else if ext == ".out" {
// Write in .out format (export KEY="VALUE")
DevanshMathur19 marked this conversation as resolved.
Show resolved Hide resolved
content = fmt.Sprintf("%s \"%s\"\n", key, value)
} else {
return fmt.Errorf("unsupported file extension: %s", ext)
}

return writeToFile(filePath, content)
}

// Helper function to append content to the file
func writeToFile(filename, content string) error {
DevanshMathur19 marked this conversation as resolved.
Show resolved Hide resolved
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()

_, err = f.WriteString(content)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}