fdfbda0f9b
(cherry picked from commitb7fe7cf401
) (cherry picked from commitcf339eed4f
) (cherry picked from commit4f3a16168b
) (cherry picked from commit6f5bbc53fc
) (cherry picked from commitaca42b422e
) (cherry picked from commit5a7f7580e5
) (cherry picked from commit06c383c807
) (cherry picked from commitfe831dcb53
) (cherry picked from commitcd12cd0dbc
) (cherry picked from commitcc79163703
) (cherry picked from commitf3ad9d345f
) (cherry picked from commit1a01baaa0f
) (cherry picked from commit21123afcfd
)
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
// SPDX-License-Identifier: MIT
|
|
|
|
package semver
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"github.com/hashicorp/go-version"
|
|
)
|
|
|
|
func init() {
|
|
db.RegisterModel(new(ForgejoSemVer))
|
|
}
|
|
|
|
type ForgejoSemVer struct {
|
|
Version string
|
|
}
|
|
|
|
func GetVersion(ctx context.Context) (*version.Version, error) {
|
|
return GetVersionWithEngine(db.GetEngine(ctx))
|
|
}
|
|
|
|
func GetVersionWithEngine(e db.Engine) (*version.Version, error) {
|
|
versionString := "v1.0.0"
|
|
exists, err := e.IsTableExist("forgejo_sem_ver")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if exists {
|
|
var semver ForgejoSemVer
|
|
has, err := e.Get(&semver)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if has {
|
|
versionString = semver.Version
|
|
}
|
|
}
|
|
|
|
v, err := version.NewVersion(versionString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func SetVersionString(ctx context.Context, versionString string) error {
|
|
return SetVersionStringWithEngine(db.GetEngine(ctx), versionString)
|
|
}
|
|
|
|
func SetVersionStringWithEngine(e db.Engine, versionString string) error {
|
|
v, err := version.NewVersion(versionString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return SetVersionWithEngine(e, v)
|
|
}
|
|
|
|
func SetVersion(ctx context.Context, v *version.Version) error {
|
|
return SetVersionWithEngine(db.GetEngine(ctx), v)
|
|
}
|
|
|
|
func SetVersionWithEngine(e db.Engine, v *version.Version) error {
|
|
var semver ForgejoSemVer
|
|
has, err := e.Get(&semver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !has {
|
|
_, err = e.Exec("insert into forgejo_sem_ver values (?)", v.String())
|
|
} else {
|
|
_, err = e.Exec("update forgejo_sem_ver set version = ?", v.String())
|
|
}
|
|
return err
|
|
}
|