Version printing from command line arguments

This commit is contained in:
Magnus Åhall 2026-07-03 09:09:55 +02:00
parent fdc0fc8b97
commit b76502c034

35
main.go
View file

@ -36,6 +36,7 @@ var (
FlagConfig string FlagConfig string
FlagCreateUser string FlagCreateUser string
FlagChangePassword string FlagChangePassword string
FlagVersion bool
Webengine HTMLTemplate.Engine Webengine HTMLTemplate.Engine
config Config config Config
Log *slog.Logger Log *slog.Logger
@ -66,6 +67,7 @@ func init() { // {{{
flag.BoolVar(&FlagLoremIpsum, "lorem-ipsum", false, "Replace all G- nodes with lorem ipsum paragraphs") flag.BoolVar(&FlagLoremIpsum, "lorem-ipsum", false, "Replace all G- nodes with lorem ipsum paragraphs")
flag.StringVar(&FlagCreateUser, "create-user", "", "Username for creating a new user") flag.StringVar(&FlagCreateUser, "create-user", "", "Username for creating a new user")
flag.StringVar(&FlagChangePassword, "change-password", "", "Change the password for the given username") flag.StringVar(&FlagChangePassword, "change-password", "", "Change the password for the given username")
flag.BoolVar(&FlagVersion, "version", false, "Print version and exit")
flag.Parse() flag.Parse()
RxpBearerToken = regexp.MustCompile("(?i)^\\s*Bearer\\s+(.*?)\\s*$") RxpBearerToken = regexp.MustCompile("(?i)^\\s*Bearer\\s+(.*?)\\s*$")
@ -76,7 +78,14 @@ func initLog() { // {{{
Log = slog.New(slog.NewJSONHandler(os.Stdout, &opts)) Log = slog.New(slog.NewJSONHandler(os.Stdout, &opts))
} // }}} } // }}}
func main() { // {{{ func main() { // {{{
if FlagVersion {
fmt.Println(VERSION)
return
}
initLog() initLog()
Log.Info("application", "version", VERSION)
err := readConfig(FlagConfig) err := readConfig(FlagConfig)
if err != nil { if err != nil {
Log.Error("config", "error", err) Log.Error("config", "error", err)
@ -443,7 +452,7 @@ func actionSyncFileUpload(w http.ResponseWriter, r *http.Request) { // {{{
Log.Info("file_upload", "status", "done", "user", session.UserID, "uuid", uuid, "total", totalBytes) Log.Info("file_upload", "status", "done", "user", session.UserID, "uuid", uuid, "total", totalBytes)
id := fileUUIDCache[uuid] id := fileUUIDCache[uuid]
delete(fileUUIDCache, uuid) delete(fileUUIDCache, uuid)
_, err = db.Exec(`UPDATE public.file SET upload_complete = true WHERE id = $1`, id) _, err = db.Exec(`UPDATE public.file SET upload_complete = true WHERE id = $1`, id)
default: default:
id := fileUUIDCache[uuid] id := fileUUIDCache[uuid]
@ -502,7 +511,7 @@ func actionUserSetPreferences(w http.ResponseWriter, r *http.Request) { // {{{
}) })
} // }}} } // }}}
func actionFileCount(w http.ResponseWriter, r *http.Request) {// {{{ func actionFileCount(w http.ResponseWriter, r *http.Request) { // {{{
session := getUserSession(r) session := getUserSession(r)
count, err := FileCountForUser(session.UserID) count, err := FileCountForUser(session.UserID)
if err != nil { if err != nil {
@ -512,11 +521,11 @@ func actionFileCount(w http.ResponseWriter, r *http.Request) {// {{{
} }
responseData(w, map[string]any{ responseData(w, map[string]any{
"OK": true, "OK": true,
"Count": count, "Count": count,
}) })
}// }}} } // }}}
func actionFileServerUUIDs(w http.ResponseWriter, r *http.Request) {// {{{ func actionFileServerUUIDs(w http.ResponseWriter, r *http.Request) { // {{{
session := getUserSession(r) session := getUserSession(r)
offset, _ := strconv.Atoi(r.PathValue("offset")) offset, _ := strconv.Atoi(r.PathValue("offset"))
uuids, more, err := FileServerUUIDsForUser(session.UserID, offset) uuids, more, err := FileServerUUIDsForUser(session.UserID, offset)
@ -527,12 +536,12 @@ func actionFileServerUUIDs(w http.ResponseWriter, r *http.Request) {// {{{
} }
responseData(w, map[string]any{ responseData(w, map[string]any{
"OK": true, "OK": true,
"UUIDs": uuids, "UUIDs": uuids,
"MoreRowsExist": more, "MoreRowsExist": more,
}) })
}// }}} } // }}}
func actionFile(w http.ResponseWriter, r *http.Request) {// {{{ func actionFile(w http.ResponseWriter, r *http.Request) { // {{{
uuid := r.PathValue("uuid") uuid := r.PathValue("uuid")
session := getUserSession(r) session := getUserSession(r)
f, md, err := FileForUser(session.UserID, uuid) f, md, err := FileForUser(session.UserID, uuid)
@ -543,8 +552,8 @@ func actionFile(w http.ResponseWriter, r *http.Request) {// {{{
} }
http.ServeContent(w, r, md.Name, md.Modified, f) http.ServeContent(w, r, md.Name, md.Modified, f)
}// }}} } // }}}
func actionFileMetadata(w http.ResponseWriter, r *http.Request) {// {{{ func actionFileMetadata(w http.ResponseWriter, r *http.Request) { // {{{
uuid := r.PathValue("uuid") uuid := r.PathValue("uuid")
session := getUserSession(r) session := getUserSession(r)
md, err := FileMetadata(session.UserID, uuid) md, err := FileMetadata(session.UserID, uuid)
@ -556,10 +565,10 @@ func actionFileMetadata(w http.ResponseWriter, r *http.Request) {// {{{
} }
responseData(w, map[string]any{ responseData(w, map[string]any{
"OK": true, "OK": true,
"Metadata": md, "Metadata": md,
}) })
}// }}} } // }}}
func createNewUser(username string) { // {{{ func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)