refactor(cmd/joj3): use getter/setter in env
This commit is contained in:
parent
439e64be0c
commit
3bd89e9aa5
75
cmd/joj3/env/env.go
vendored
75
cmd/joj3/env/env.go
vendored
|
@ -1,9 +1,9 @@
|
||||||
// Package env stores the environment variables from actions environment.
|
|
||||||
package env
|
package env
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,28 +14,21 @@ const (
|
||||||
CommitMsg = "JOJ3_COMMIT_MSG"
|
CommitMsg = "JOJ3_COMMIT_MSG"
|
||||||
ForceQuitStageName = "JOJ3_FORCE_QUIT_STAGE_NAME"
|
ForceQuitStageName = "JOJ3_FORCE_QUIT_STAGE_NAME"
|
||||||
OutputPath = "JOJ3_OUTPUT_PATH"
|
OutputPath = "JOJ3_OUTPUT_PATH"
|
||||||
|
|
||||||
|
GitHubActor = "GITHUB_ACTOR"
|
||||||
|
GitHubRepository = "GITHUB_REPOSITORY"
|
||||||
|
GitHubSha = "GITHUB_SHA"
|
||||||
|
GitHubRef = "GITHUB_REF"
|
||||||
|
GitHubWorkflow = "GITHUB_WORKFLOW"
|
||||||
|
GitHubRunNumber = "GITHUB_RUN_NUMBER"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Attribute struct {
|
var (
|
||||||
ConfName string
|
runIDOnce sync.Once
|
||||||
CommitMsg string
|
runID string
|
||||||
Groups string
|
)
|
||||||
RunID string
|
|
||||||
Actor string
|
|
||||||
Repository string
|
|
||||||
Sha string
|
|
||||||
Ref string
|
|
||||||
Workflow string
|
|
||||||
RunNumber string
|
|
||||||
ActorName string
|
|
||||||
ActorID string
|
|
||||||
ForceQuitStageName string
|
|
||||||
OutputPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
var Attr Attribute
|
func generateRunID() string {
|
||||||
|
|
||||||
func init() {
|
|
||||||
timestamp := time.Now().UnixNano()
|
timestamp := time.Now().UnixNano()
|
||||||
pid := os.Getpid()
|
pid := os.Getpid()
|
||||||
high := timestamp >> 32
|
high := timestamp >> 32
|
||||||
|
@ -45,20 +38,34 @@ func init() {
|
||||||
combined ^= timestamp >> 16
|
combined ^= timestamp >> 16
|
||||||
combined ^= (combined >> 8)
|
combined ^= (combined >> 8)
|
||||||
combined ^= (combined << 16)
|
combined ^= (combined << 16)
|
||||||
Attr.RunID = fmt.Sprintf("%08X", combined&0xFFFFFFFF)
|
return fmt.Sprintf("%08X", combined&0xFFFFFFFF)
|
||||||
Attr.Actor = os.Getenv("GITHUB_ACTOR")
|
|
||||||
Attr.Repository = os.Getenv("GITHUB_REPOSITORY")
|
|
||||||
Attr.Sha = os.Getenv("GITHUB_SHA")
|
|
||||||
Attr.Ref = os.Getenv("GITHUB_REF")
|
|
||||||
Attr.Workflow = os.Getenv("GITHUB_WORKFLOW")
|
|
||||||
Attr.RunNumber = os.Getenv("GITHUB_RUN_NUMBER")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Set() {
|
func GetRunID() string {
|
||||||
os.Setenv(ConfName, Attr.ConfName)
|
if val := os.Getenv(RunID); val != "" {
|
||||||
os.Setenv(Groups, Attr.Groups)
|
return val
|
||||||
os.Setenv(RunID, Attr.RunID)
|
}
|
||||||
os.Setenv(CommitMsg, Attr.CommitMsg)
|
runIDOnce.Do(func() {
|
||||||
os.Setenv(ForceQuitStageName, Attr.ForceQuitStageName)
|
runID = generateRunID()
|
||||||
os.Setenv(OutputPath, Attr.OutputPath)
|
})
|
||||||
|
return runID
|
||||||
}
|
}
|
||||||
|
func GetConfName() string { return os.Getenv(ConfName) }
|
||||||
|
func GetGroups() string { return os.Getenv(Groups) }
|
||||||
|
func GetCommitMsg() string { return os.Getenv(CommitMsg) }
|
||||||
|
func GetForceQuitStageName() string { return os.Getenv(ForceQuitStageName) }
|
||||||
|
func GetOutputPath() string { return os.Getenv(OutputPath) }
|
||||||
|
|
||||||
|
func SetRunID(val string) { os.Setenv(RunID, val) }
|
||||||
|
func SetConfName(val string) { os.Setenv(ConfName, val) }
|
||||||
|
func SetGroups(val string) { os.Setenv(Groups, val) }
|
||||||
|
func SetCommitMsg(val string) { os.Setenv(CommitMsg, val) }
|
||||||
|
func SetForceQuitStageName(val string) { os.Setenv(ForceQuitStageName, val) }
|
||||||
|
func SetOutputPath(val string) { os.Setenv(OutputPath, val) }
|
||||||
|
|
||||||
|
func GetActor() string { return os.Getenv(GitHubActor) }
|
||||||
|
func GetRepository() string { return os.Getenv(GitHubRepository) }
|
||||||
|
func GetSha() string { return os.Getenv(GitHubSha) }
|
||||||
|
func GetRef() string { return os.Getenv(GitHubRef) }
|
||||||
|
func GetWorkflow() string { return os.Getenv(GitHubWorkflow) }
|
||||||
|
func GetRunNumber() string { return os.Getenv(GitHubRunNumber) }
|
||||||
|
|
|
@ -55,59 +55,46 @@ func (h *multiHandler) WithGroup(name string) slog.Handler {
|
||||||
return &multiHandler{handlers: handlers}
|
return &multiHandler{handlers: handlers}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSlogAttrs(csvPath string) (attrs []slog.Attr) {
|
func newSlogAttrs(csvPath string) []slog.Attr {
|
||||||
env.Attr.ActorName = fmt.Sprintf("Name(%s)", env.Attr.Actor)
|
actor := env.GetActor()
|
||||||
env.Attr.ActorID = fmt.Sprintf("ID(%s)", env.Attr.Actor)
|
actorName := fmt.Sprintf("Name(%s)", actor)
|
||||||
attrs = []slog.Attr{
|
actorID := fmt.Sprintf("ID(%s)", actor)
|
||||||
slog.String("runID", env.Attr.RunID),
|
|
||||||
slog.String("confName", env.Attr.ConfName),
|
if csvPath != "" {
|
||||||
slog.String("actor", env.Attr.Actor),
|
file, err := os.Open(csvPath)
|
||||||
slog.String("actorName", env.Attr.ActorName),
|
|
||||||
slog.String("actorID", env.Attr.ActorID),
|
|
||||||
slog.String("repository", env.Attr.Repository),
|
|
||||||
slog.String("sha", env.Attr.Sha),
|
|
||||||
slog.String("ref", env.Attr.Ref),
|
|
||||||
}
|
|
||||||
// if csvPath is empty, just return
|
|
||||||
if csvPath == "" {
|
|
||||||
return attrs
|
|
||||||
}
|
|
||||||
file, err := os.Open(csvPath)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("open csv", "error", err)
|
|
||||||
return attrs
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
reader := csv.NewReader(file)
|
|
||||||
for {
|
|
||||||
row, err := reader.Read()
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("read csv", "error", err)
|
slog.Error("open csv", "error", err)
|
||||||
return attrs
|
} else {
|
||||||
}
|
defer file.Close()
|
||||||
if len(row) < 3 {
|
reader := csv.NewReader(file)
|
||||||
continue
|
for {
|
||||||
}
|
row, err := reader.Read()
|
||||||
actor := row[2]
|
if err == io.EOF {
|
||||||
if actor == env.Attr.Actor {
|
break
|
||||||
env.Attr.ActorName = row[0]
|
}
|
||||||
env.Attr.ActorID = row[1]
|
if err != nil {
|
||||||
return []slog.Attr{
|
slog.Error("read csv", "error", err)
|
||||||
slog.String("runID", env.Attr.RunID),
|
break
|
||||||
slog.String("confName", env.Attr.ConfName),
|
}
|
||||||
slog.String("actor", env.Attr.Actor),
|
if len(row) >= 3 && row[2] == actor {
|
||||||
slog.String("actorName", env.Attr.ActorName),
|
actorName = row[0]
|
||||||
slog.String("actorID", env.Attr.ActorID),
|
actorID = row[1]
|
||||||
slog.String("repository", env.Attr.Repository),
|
break
|
||||||
slog.String("sha", env.Attr.Sha),
|
}
|
||||||
slog.String("ref", env.Attr.Ref),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return attrs
|
|
||||||
|
return []slog.Attr{
|
||||||
|
slog.String("runID", env.GetRunID()),
|
||||||
|
slog.String("confName", env.GetConfName()),
|
||||||
|
slog.String("actor", actor),
|
||||||
|
slog.String("actorName", actorName),
|
||||||
|
slog.String("actorID", actorID),
|
||||||
|
slog.String("repository", env.GetRepository()),
|
||||||
|
slog.String("sha", env.GetSha()),
|
||||||
|
slog.String("ref", env.GetRef()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupSlog(conf *conf.Conf) error {
|
func setupSlog(conf *conf.Conf) error {
|
||||||
|
|
|
@ -39,7 +39,7 @@ func getCommitMsg() (string, error) {
|
||||||
slog.Error("get commit msg", "error", err)
|
slog.Error("get commit msg", "error", err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
env.Attr.CommitMsg = commitMsg
|
env.SetCommitMsg(commitMsg)
|
||||||
return commitMsg, nil
|
return commitMsg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,8 +52,8 @@ func getConf(commitMsg string) (*joj3Conf.Conf, *joj3Conf.ConventionalCommit, er
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
env.Attr.ConfName = conf.Name
|
env.SetConfName(conf.Name)
|
||||||
env.Attr.OutputPath = conf.Stage.OutputPath
|
env.SetOutputPath(conf.Stage.OutputPath)
|
||||||
if err := showConfStat(confPath, confStat); err != nil {
|
if err := showConfStat(confPath, confStat); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -102,14 +102,12 @@ func validateConf(conf *joj3Conf.Conf) error {
|
||||||
|
|
||||||
func run(conf *joj3Conf.Conf, conventionalCommit *joj3Conf.ConventionalCommit) error {
|
func run(conf *joj3Conf.Conf, conventionalCommit *joj3Conf.ConventionalCommit) error {
|
||||||
groups := joj3Conf.MatchGroups(conf, conventionalCommit)
|
groups := joj3Conf.MatchGroups(conf, conventionalCommit)
|
||||||
env.Attr.Groups = strings.Join(groups, ",")
|
env.SetGroups(strings.Join(groups, ","))
|
||||||
env.Set()
|
|
||||||
_, forceQuitStageName, err := runStages(
|
_, forceQuitStageName, err := runStages(
|
||||||
conf,
|
conf,
|
||||||
groups,
|
groups,
|
||||||
func(stageResults []stage.StageResult, forceQuitStageName string) {
|
func(stageResults []stage.StageResult, forceQuitStageName string) {
|
||||||
env.Attr.ForceQuitStageName = forceQuitStageName
|
env.SetForceQuitStageName(forceQuitStageName)
|
||||||
env.Set()
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user