-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: use VERSION file as fallback for version reporting (#614)
Currently, if Alloy was built without setting build information, it reports its version as "v0.0.0". This commit adds using the VERSION file as fallback, so if the version information wasn't present (such as when ussing `go run .`), the reported version is taken from the VERSION file at the root of the repository. The version reported is then appended with `-devel` to indicate that it is not the final release. For example, as the VERSION file currently contains `v1.1.0`, then the fallback reported version will be `v1.1.0-devel`.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
_ "embed" | ||
"strings" | ||
|
||
"github.com/grafana/alloy/internal/build" | ||
) | ||
|
||
//go:embed VERSION | ||
var fallbackVersionText []byte | ||
|
||
// fallbackVersion returns a version string to use for when the version isn't | ||
// explicitly set at build time. The version string will always have -devel | ||
// appended to it. | ||
func fallbackVersion() string { | ||
return fallbackVersionFromText(fallbackVersionText) | ||
} | ||
|
||
func fallbackVersionFromText(text []byte) string { | ||
// Find the first line in fallbackVersionText which isn't a blank line or a | ||
// line starting with #. | ||
scanner := bufio.NewScanner(bytes.NewReader(text)) | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
if len(line) == 0 || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
|
||
return line + "-devel" | ||
} | ||
|
||
// We shouldn't hit this case since we always control the contents of the | ||
// VERSION file, but just in case we'll return the existing version. | ||
return build.Version | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_fallbackVersionFromText(t *testing.T) { | ||
in := `# This is a comment | ||
# This is another comment | ||
v1.2.3 | ||
This line is ignored!` | ||
expect := "v1.2.3-devel" | ||
|
||
actual := fallbackVersionFromText([]byte(in)) | ||
require.Equal(t, expect, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters