Skip to content

Commit

Permalink
Start using GochanVersion struct
Browse files Browse the repository at this point in the history
TODO: replace current db update check with config.Version.CompareString()
  • Loading branch information
user.email committed Nov 23, 2018
1 parent 6ecfc8e commit 0d36172
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/gochan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
connectToSQLServer()
parseCommandLine()

printf(0, "Starting gochan v%s.%s, using verbosity level %d\n", config.Version, buildtimeString, config.Verbosity)
printf(0, "Starting gochan v%s.%s, using verbosity level %d\n", config.Version.String(), buildtimeString, config.Verbosity)
println(0, "Loading and parsing templates...")
if err := initTemplates(); err != nil {
handleError(0, customError(err))
Expand Down
11 changes: 9 additions & 2 deletions src/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ var funcMap = template.FuncMap{
return getThumbnailPath("catalog", img)
},
"getThreadID": func(post_i interface{}) (thread int) {
post := post_i.(PostTable)
if post.ParentID == 0 {
post, ok := post_i.(PostTable)
if !ok {
thread = 0
} else if post.ParentID == 0 {
thread = post.ID
} else {
thread = post.ParentID
Expand Down Expand Up @@ -235,6 +237,11 @@ var funcMap = template.FuncMap{
"isStyleDefault": func(style string) bool {
return style == config.DefaultStyle
},

// Version functions
"formatVersion": func(v GochanVersion) string {
return v.String()
},
}

var (
Expand Down
12 changes: 6 additions & 6 deletions src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ type GochanConfig struct {
// Verbosity = 0 for no debugging info. Critical errors and general output only
// Verbosity = 1 for non-critical warnings and important info
// Verbosity = 2 for all debugging/benchmarks/warnings
Verbosity int `description:"The level of verbosity to use in error/warning messages. 0 = critical errors/startup messages, 1 = warnings, 2 = benchmarks/notices." default:"0"`
EnableAppeals bool `description:"If checked, allow banned users to appeal their bans.<br />This will likely be removed (permanently allowing appeals) or made board-specific in the future." default:"checked"`
MaxLogDays int `description:"The maximum number of days to keep messages in the moderation/staff log file."`
RandomSeed string `critical:"true"`
Version string `critical:"true"`
Verbosity int `description:"The level of verbosity to use in error/warning messages. 0 = critical errors/startup messages, 1 = warnings, 2 = benchmarks/notices." default:"0"`
EnableAppeals bool `description:"If checked, allow banned users to appeal their bans.<br />This will likely be removed (permanently allowing appeals) or made board-specific in the future." default:"checked"`
MaxLogDays int `description:"The maximum number of days to keep messages in the moderation/staff log file."`
RandomSeed string `critical:"true"`
Version GochanVersion `critical:"true"`
}

func initConfig() {
Expand Down Expand Up @@ -675,5 +675,5 @@ func initConfig() {
bbcompiler.SetTag("quote", nil)
bbcompiler.SetTag("size", nil)

config.Version = version
config.Version = ParseVersion(version)
}
35 changes: 30 additions & 5 deletions src/version.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
// used for version parsing, printing, and comparison

package main

import (
"fmt"
)

type GochanVersion struct {
Major uint
Minor uint
Revision uint
Major int
Minor int
Revision int
Extra string
}

func ParseVersion(vStr string) GochanVersion {
var v GochanVersion
fmt.Sscanf(vStr, "%d.%d.%d-%s", &v.Major, &v.Minor, &v.Revision, &v.Extra)
v.Normalize()
return v
}

func (v *GochanVersion) Normalize() bool {
valid := true
if v.Major < 0 {
v.Major = 0
valid = false
}
if v.Minor < 0 {
v.Minor = 0
valid = false
}
if v.Revision < 0 {
v.Revision = 0
}
return valid
}

func (v *GochanVersion) Compare(v2 GochanVersion) int {
v.Normalize()
v2.Normalize()
if v.Major > v2.Major {
return 1
}
Expand All @@ -39,11 +60,15 @@ func (v *GochanVersion) Compare(v2 GochanVersion) int {
return 0
}

func (v *GochanVersion) CompareString(v2 string) int {
return v.Compare(ParseVersion(v2))
func (v *GochanVersion) CompareString(v2str string) int {
v.Normalize()
v2 := ParseVersion(v2str)
v2.Normalize()
return v.Compare(v2)
}

func (v *GochanVersion) String() string {
v.Normalize()
str := fmt.Sprintf("%d.%d", v.Major, v.Minor)
if v.Revision > 0 {
str += fmt.Sprintf(".%d", v.Revision)
Expand Down
2 changes: 1 addition & 1 deletion templates/global_footer.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="footer">
<a href="{{$.config.SiteWebfolder}}">Home</a> | <a href="{{$.config.SiteWebfolder}}#boards">Boards</a> | <a href="{{$.config.SiteWebfolder}}#rules">Rules</a> | <a href="{{$.config.SiteWebfolder}}#faq">FAQ</a><br />
Powered by <a href="http://github.com/eggbertx/gochan/">Gochan {{.config.Version}}</a><br />
Powered by <a href="http://github.com/eggbertx/gochan/">Gochan {{formatVersion .config.Version}}</a><br />
</div>
</body>
</html>

0 comments on commit 0d36172

Please sign in to comment.