forked from misaka00251/go2spec
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 29d57da520 |
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
@@ -22,10 +23,12 @@ Use "go2spec [command] --help" for more information about a command.
|
||||
|
||||
If there are no commands provided, the tool will default to executing the 'pack' command.
|
||||
`
|
||||
println(helpText)
|
||||
fmt.Print(helpText)
|
||||
}
|
||||
|
||||
func main() {
|
||||
printVersion(os.Stdout)
|
||||
|
||||
pkgsiteHTTPClient = &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
Transport: httpcache.NewMemoryCacheTransport(),
|
||||
@@ -43,12 +46,12 @@ func main() {
|
||||
printHelp()
|
||||
case "pack":
|
||||
// Placeholder for the pack command implementation
|
||||
println("Executing 'pack' command...")
|
||||
fmt.Println("Executing 'pack' command...")
|
||||
// Actual packing logic would go here
|
||||
mainPack(args[1:], nil)
|
||||
default:
|
||||
// Default to 'pack' command if no command is provided
|
||||
println("No command provided. Defaulting to 'pack' command...")
|
||||
fmt.Println("No command provided. Defaulting to 'pack' command...")
|
||||
// Actual packing logic would go here
|
||||
mainPack(args, printHelp)
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
||||
// SPDX header
|
||||
fmt.Fprintf(f, "# SPDX-FileCopyrightText: (C) 2026 Institute of Software, Chinese Academy of Sciences (ISCAS)\n")
|
||||
fmt.Fprintf(f, "# SPDX-FileCopyrightText: (C) 2026 openRuyi Project Contributors\n")
|
||||
fmt.Fprintf(f, "# SPDX-FileContributor: \n")
|
||||
fmt.Fprintf(f, "#\n")
|
||||
fmt.Fprintf(f, "# SPDX-License-Identifier: MulanPSL-2.0\n")
|
||||
fmt.Fprintf(f, "\n")
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
)
|
||||
|
||||
const unknownVersion = "unknown"
|
||||
|
||||
func runtimeVersion() string {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return unknownVersion
|
||||
}
|
||||
return versionFromBuildSettings(info.Settings)
|
||||
}
|
||||
|
||||
func versionFromBuildSettings(settings []debug.BuildSetting) string {
|
||||
var revision string
|
||||
var commitTime string
|
||||
for _, setting := range settings {
|
||||
switch setting.Key {
|
||||
case "vcs.revision":
|
||||
revision = setting.Value
|
||||
case "vcs.time":
|
||||
commitTime = setting.Value
|
||||
}
|
||||
}
|
||||
if revision == "" || commitTime == "" {
|
||||
return unknownVersion
|
||||
}
|
||||
if len(revision) > 7 {
|
||||
revision = revision[:7]
|
||||
}
|
||||
if t, err := time.Parse(time.RFC3339, commitTime); err == nil {
|
||||
commitTime = t.Format("2006-01-02")
|
||||
}
|
||||
return fmt.Sprintf("%s %s", commitTime, revision)
|
||||
}
|
||||
|
||||
func printVersion(w io.Writer) {
|
||||
fmt.Fprintf(w, "Version: %s\n", runtimeVersion())
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersionFromBuildSettings(t *testing.T) {
|
||||
settings := []debug.BuildSetting{
|
||||
{Key: "vcs.revision", Value: "1234567890abcdef"},
|
||||
{Key: "vcs.time", Value: "2026-06-27T15:33:29Z"},
|
||||
}
|
||||
|
||||
if got, want := versionFromBuildSettings(settings), "2026-06-27 1234567"; got != want {
|
||||
t.Fatalf("versionFromBuildSettings() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionFromBuildSettingsUnknown(t *testing.T) {
|
||||
if got := versionFromBuildSettings(nil); got != unknownVersion {
|
||||
t.Fatalf("versionFromBuildSettings(nil) = %q, want %q", got, unknownVersion)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user