Skip to content

Commit

Permalink
Remove repo info from binary (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmgot authored Sep 19, 2024
1 parent 4110266 commit aaa26d1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 137 deletions.
5 changes: 4 additions & 1 deletion build/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -152,7 +153,9 @@ func getBuildBackendCmdInfo(cfg Config) (Config, []string, error) {
"build", "-o", filepath.Join(outputPath, exePath),
}

info := getBuildInfoFromEnvironment()
info := Info{
Time: now().UnixNano() / int64(time.Millisecond),
}
pluginID, err := internal.GetStringValueFromJSON(filepath.Join(pluginJSONPath, "plugin.json"), "id")
if err == nil && len(pluginID) > 0 {
info.PluginID = pluginID
Expand Down
6 changes: 3 additions & 3 deletions build/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "arm64", "GOOS": "darwin"},
PluginJSONPath: filepath.Join(tmpDir, "foobar-datasource"),
},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foo_darwin_arm64"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}' -X 'main.branch=.+' -X 'main.commit=[a-z0-9\\d]{40}'", "./pkg"},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foo_darwin_arm64"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
wantErr: assert.NoError,
},
{
Expand All @@ -174,7 +174,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "arm64", "GOOS": "darwin"},
PluginJSONPath: filepath.Join(tmpDir, "foobar-app"),
},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, defaultNestedDataSourcePath, "gpx_foo_darwin_arm64"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}' -X 'main.branch=.+' -X 'main.commit=[a-z0-9\\d]{40}'", "./pkg"},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, defaultNestedDataSourcePath, "gpx_foo_darwin_arm64"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
wantErr: assert.NoError,
},
{
Expand All @@ -195,7 +195,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "amd64", "GOOS": "windows"},
PluginJSONPath: filepath.Join(tmpDir, "foobarbaz-app"),
},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foobarbaz_windows_amd64.exe"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}' -X 'main.branch=.+' -X 'main.commit=[a-z0-9\\d]{40}'", "./pkg"},
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foobarbaz_windows_amd64.exe"), "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
wantErr: assert.NoError,
},
}
Expand Down
76 changes: 0 additions & 76 deletions build/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package build
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
)

Expand All @@ -21,11 +17,6 @@ type Info struct {
Time int64 `json:"time,omitempty"`
PluginID string `json:"pluginID,omitempty"`
Version string `json:"version,omitempty"`
Repo string `json:"repo,omitempty"`
Branch string `json:"branch,omitempty"`
Hash string `json:"hash,omitempty"`
Build int64 `json:"build,omitempty"`
PR int64 `json:"pr,omitempty"`
}

// this will append build flags -- the keys are picked to match existing
Expand All @@ -37,80 +28,13 @@ func (v Info) appendFlags(flags map[string]string) {
if v.Version != "" {
flags["main.version"] = v.Version
}
if v.Branch != "" {
flags["main.branch"] = v.Branch
}
if v.Hash != "" {
flags["main.commit"] = v.Hash
}

out, err := json.Marshal(v)
if err == nil {
flags["github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON"] = string(out)
}
}

func getEnvironment(check ...string) string {
for _, key := range check {
if strings.HasPrefix(key, "> ") {
parts := strings.Split(key, " ")
cmd := exec.Command(parts[1], parts[2:]...) // #nosec G204
out, err := cmd.CombinedOutput()
if err == nil && len(out) > 0 {
str := strings.TrimSpace(string(out))
if strings.Index(str, " ") > 0 {
continue // skip any output that has spaces
}
return str
}
continue
}

val := os.Getenv(key)
if val != "" {
return strings.TrimSpace(val)
}
}
return ""
}

// getBuildInfoFromEnvironment reads the
func getBuildInfoFromEnvironment() Info {
v := Info{
Time: now().UnixNano() / int64(time.Millisecond),
}

v.Repo = getEnvironment(
"DRONE_REPO_LINK",
"CIRCLE_PROJECT_REPONAME",
"CI_REPONAME",
"> git remote get-url origin")
v.Branch = getEnvironment(
"DRONE_BRANCH",
"CIRCLE_BRANCH",
"CI_BRANCH",
"> git branch --show-current")
v.Hash = getEnvironment(
"DRONE_COMMIT_SHA",
"CIRCLE_SHA1",
"CI_COMMIT_SHA",
"> git rev-parse HEAD")
val, err := strconv.ParseInt(getEnvironment(
"DRONE_BUILD_NUMBER",
"CIRCLE_BUILD_NUM",
"CI_BUILD_NUM"), 10, 64)
if err == nil {
v.Build = val
}
val, err = strconv.ParseInt(getEnvironment(
"DRONE_PULL_REQUEST",
"CI_PULL_REQUEST"), 10, 64)
if err == nil {
v.PR = val
}
return v
}

// InfoGetter is an interface with a method for returning the build info.
type InfoGetter interface {
// GetInfo returns the build info.
Expand Down
57 changes: 0 additions & 57 deletions build/info_test.go

This file was deleted.

0 comments on commit aaa26d1

Please sign in to comment.