From 75112d023579b5a411cd8453fb7e8b6369463060 Mon Sep 17 00:00:00 2001 From: vladopajic Date: Tue, 31 Oct 2023 14:15:02 +0100 Subject: [PATCH] action default values (#33) --- action.yml | 52 +++++++++++++------------- main.go | 105 ++++++++++++++++++++++++++++------------------------- 2 files changed, 81 insertions(+), 76 deletions(-) diff --git a/action.yml b/action.yml index e1084c5..6d5c21e 100644 --- a/action.yml +++ b/action.yml @@ -5,17 +5,17 @@ inputs: config: description: Path of configuration file. required: false - default: "''" + default: "" type: string profile: description: Path of coverage profile file. required: false - default: "''" + default: "" type: string local-prefix: description: When specified reported file paths will not contain local prefix in the output. required: false - default: "''" + default: "" type: string threshold-file: description: The minimum coverage that each file should have. @@ -35,37 +35,37 @@ inputs: badge-file-name: description: When specified coverage badge will be created and saved to file with this name. required: false - default: "''" + default: "" type: string cdn-key: description: CDN key. required: false - default: "''" + default: "" type: string cdn-secret: description: CDN secret. required: false - default: "''" + default: "" type: string cdn-region: description: CDN region. required: false - default: "''" + default: "" type: string cdn-endpoint: description: CDN endpoint. required: false - default: "''" + default: "" type: string cdn-file-name: description: CDN file name. required: false - default: "''" + default: "" type: string cdn-bucket-name: description: CDN bucket name. required: false - default: "''" + default: "" type: string cdn-force-path-style: description: CDN force path style. @@ -75,7 +75,7 @@ inputs: git-token: description: Github token. required: false - default: "''" + default: "" type: string git-repository: description: Github repository in format {owner}/{repository}. @@ -85,7 +85,7 @@ inputs: git-branch: description: git branch where badge will be stored. required: false - default: "''" + default: "" type: string git-file-name: description: File name of badge stored to repository. @@ -103,25 +103,25 @@ runs: using: docker image: docker://ghcr.io/vladopajic/go-test-coverage:v2.7.1 args: - - --config=${{ inputs.config }} - - --profile=${{ inputs.profile }} + - --config=${{ inputs.config || '''''' }} + - --profile=${{ inputs.profile || '''''' }} - --github-action-output=true - - --local-prefix=${{ inputs.local-prefix }} + - --local-prefix=${{ inputs.local-prefix || '''''' }} - --threshold-file=${{ inputs.threshold-file }} - --threshold-package=${{ inputs.threshold-package }} - --threshold-total=${{ inputs.threshold-total }} - - --badge-file-name=${{ inputs.badge-file-name }} - - --cdn-key=${{ inputs.cdn-key }} - - --cdn-secret=${{ inputs.cdn-secret }} - - --cdn-region=${{ inputs.cdn-region }} - - --cdn-endpoint=${{ inputs.cdn-endpoint }} - - --cdn-file-name=${{ inputs.cdn-file-name }} - - --cdn-bucket-name=${{ inputs.cdn-bucket-name }} + - --badge-file-name=${{ inputs.badge-file-name || '''''' }} + - --cdn-key=${{ inputs.cdn-key || '''''' }} + - --cdn-secret=${{ inputs.cdn-secret || '''''' }} + - --cdn-region=${{ inputs.cdn-region || '''''' }} + - --cdn-endpoint=${{ inputs.cdn-endpoint || '''''' }} + - --cdn-file-name=${{ inputs.cdn-file-name || '''''' }} + - --cdn-bucket-name=${{ inputs.cdn-bucket-name || '''''' }} - --cdn-force-path-style=${{ inputs.cdn-force-path-style }} - - --git-token=${{ inputs.git-token }} - - --git-repository=${{ inputs.git-repository }} - - --git-branch=${{ inputs.git-branch }} - - --git-file-name=${{ inputs.git-file-name }} + - --git-token=${{ inputs.git-token || '''''' }} + - --git-branch=${{ inputs.git-branch || '''''' }} + - --git-repository=${{ inputs.git-repository || ''''''}} + - --git-file-name=${{ inputs.git-file-name || '''''' }} branding: icon: 'code' color: 'blue' \ No newline at end of file diff --git a/main.go b/main.go index cf93085..c67fc24 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,17 @@ import ( "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage" ) -const Version = "v2.7.1" +const ( + Version = "v2.7.1" + Name = "go-test-coverage" +) + +const ( + // default value of string variables passed by CI + ciDefaultString = `''` + // default value of int variables passed by CI + ciDefaultnt = -1 +) type args struct { ConfigPath string `arg:"-c,--config"` @@ -35,44 +45,43 @@ type args struct { GitFileName string `arg:"--git-file-name"` } -const ( - magicString = `''` - magicInt = -1 -) - func newArgs() args { return args{ - ConfigPath: magicString, - Profile: magicString, - LocalPrefix: magicString, + ConfigPath: ciDefaultString, + Profile: ciDefaultString, + LocalPrefix: ciDefaultString, GithubActionOutput: false, - ThresholdFile: magicInt, - ThresholdPackage: magicInt, - ThresholdTotal: magicInt, - BadgeFileName: magicString, - - CDNKey: magicString, - CDNSecret: magicString, - CDNRegion: magicString, - CDNEndpoint: magicString, - CDNFileName: magicString, - CDNBucketName: magicString, + ThresholdFile: ciDefaultnt, + ThresholdPackage: ciDefaultnt, + ThresholdTotal: ciDefaultnt, + + // Badge + BadgeFileName: ciDefaultString, + + // CDN + CDNKey: ciDefaultString, + CDNSecret: ciDefaultString, + CDNRegion: ciDefaultString, + CDNEndpoint: ciDefaultString, + CDNFileName: ciDefaultString, + CDNBucketName: ciDefaultString, CDNForcePathStyle: false, - GitToken: magicString, - GitRepository: magicString, - GitBranch: magicString, - GitFileName: magicString, + // Git + GitToken: ciDefaultString, + GitRepository: ciDefaultString, + GitBranch: ciDefaultString, + GitFileName: ciDefaultString, } } func (args) Version() string { - return "go-test-coverage " + Version + return Name + " " + Version } //nolint:cyclop,maintidx // relax func (a *args) overrideConfig(cfg testcoverage.Config) testcoverage.Config { - if !isMagicString(a.Profile) { + if !isCIDefaultString(a.Profile) { cfg.Profile = a.Profile } @@ -80,44 +89,44 @@ func (a *args) overrideConfig(cfg testcoverage.Config) testcoverage.Config { cfg.GithubActionOutput = true } - if !isMagicString(a.LocalPrefix) { + if !isCIDefaultString(a.LocalPrefix) { cfg.LocalPrefix = a.LocalPrefix } - if !isMagicInt(a.ThresholdFile) { + if !isCIDefaultnt(a.ThresholdFile) { cfg.Threshold.File = a.ThresholdFile } - if !isMagicInt(a.ThresholdPackage) { + if !isCIDefaultnt(a.ThresholdPackage) { cfg.Threshold.Package = a.ThresholdPackage } - if !isMagicInt(a.ThresholdPackage) { + if !isCIDefaultnt(a.ThresholdPackage) { cfg.Threshold.Total = a.ThresholdTotal } - if !isMagicString(a.BadgeFileName) { + if !isCIDefaultString(a.BadgeFileName) { cfg.Badge.FileName = a.BadgeFileName } - if !isMagicString(a.CDNSecret) { + if !isCIDefaultString(a.CDNSecret) { cfg.Badge.CDN.Secret = a.CDNSecret - cfg.Badge.CDN.Key = escapeMagicString(a.CDNKey) - cfg.Badge.CDN.Region = escapeMagicString(a.CDNRegion) - cfg.Badge.CDN.FileName = escapeMagicString(a.CDNFileName) - cfg.Badge.CDN.BucketName = escapeMagicString(a.CDNBucketName) + cfg.Badge.CDN.Key = escapeCiDefaultString(a.CDNKey) + cfg.Badge.CDN.Region = escapeCiDefaultString(a.CDNRegion) + cfg.Badge.CDN.FileName = escapeCiDefaultString(a.CDNFileName) + cfg.Badge.CDN.BucketName = escapeCiDefaultString(a.CDNBucketName) cfg.Badge.CDN.ForcePathStyle = a.CDNForcePathStyle - if !isMagicString(a.CDNEndpoint) { + if !isCIDefaultString(a.CDNEndpoint) { cfg.Badge.CDN.Endpoint = a.CDNEndpoint } } - if !isMagicString(a.GitToken) { + if !isCIDefaultString(a.GitToken) { cfg.Badge.Git.Token = a.GitToken - cfg.Badge.Git.Repository = escapeMagicString(a.GitRepository) - cfg.Badge.Git.Branch = escapeMagicString(a.GitBranch) - cfg.Badge.Git.FileName = escapeMagicString(a.GitFileName) + cfg.Badge.Git.Repository = escapeCiDefaultString(a.GitRepository) + cfg.Badge.Git.Branch = escapeCiDefaultString(a.GitBranch) + cfg.Badge.Git.FileName = escapeCiDefaultString(a.GitFileName) } return cfg @@ -148,7 +157,7 @@ func readConfig() (testcoverage.Config, error) { cfg := testcoverage.Config{} // Load config from file - if !isMagicString(cmdArgs.ConfigPath) { + if !isCIDefaultString(cmdArgs.ConfigPath) { err := testcoverage.ConfigFromFile(&cfg, cmdArgs.ConfigPath) if err != nil { return testcoverage.Config{}, fmt.Errorf("failed loading config from file: %w", err) @@ -165,16 +174,12 @@ func readConfig() (testcoverage.Config, error) { return cfg, nil } -func isMagicString(v string) bool { - return v == magicString -} +func isCIDefaultString(v string) bool { return v == ciDefaultString } -func isMagicInt(v int) bool { - return v == magicInt -} +func isCIDefaultnt(v int) bool { return v == ciDefaultnt } -func escapeMagicString(v string) string { - if v == magicString { +func escapeCiDefaultString(v string) string { + if v == ciDefaultString { return "" }