Skip to content

Commit

Permalink
feat: changes, add versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Mar 15, 2024
1 parent 65bf57e commit fbf84ab
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ build/bin
node_modules
frontend/dist
.flatpak-builder
deploy.sh
build.sh
deploy.sh
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
From golang:alpine as builder
WORKDIR /build/
COPY . .
RUN apk add nodejs npm
RUN npm i -g pnpm
RUN CGO_ENABLED=0 go build ./cmd/server
RUN cd frontend && pnpm install && pnpm build
RUN apk add nodejs npm git
RUN ./build.sh server
RUN ./build.sh frontend

FROM alpine
RUN apk --update add ca-certificates curl mailcap restic
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ $ go install github.com/wailsapp/wails/v2/cmd/wails@latest
# cd into resticity
$ cd resticity
# Build using wails
$ wails build
# Run as dev
$ ./build.sh dev
# Build desktop
$ ./build.sh desktop
# run
$ ./build/bin/resticity
```

## TODOs
Expand Down
22 changes: 22 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
VERSION=$(git describe --tags)
BUILD=$(date +%FT%T%z)
LD_FLAGS="-X main.Version=$VERSION -X main.Build=$BUILD"

case $1 in
"desktop")
CGO_ENABLED=0 wails build -ldflags="$LD_FLAGS"
;;
"server")
CGO_ENABLED=0 go build -ldflags="$LD_FLAGS" ./cmd/server
;;
"frontend")
npm i -g pnpm
cd frontend && pnpm install && pnpm build
;;
"dev")
wails dev -ldflags="$LD_FLAGS" -loglevel "Error"
;;
*)
;;
esac
19 changes: 19 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/ad-on-is/resticity/internal"

"github.com/charmbracelet/log"
)

var (
Version string
Build string
)

func main() {
internal.SetLogLevel()
r, err := internal.NewResticity()
if r.FlagArgs.Version {
fmt.Println("resticity - version=" + Version + ", build=" + Build + "")
os.Exit(0)
}

if r.FlagArgs.Help {
fmt.Println("resticity " + Version + " (build " + Build + ")")
flag.PrintDefaults()
os.Exit(0)
}
if err == nil {
(r.Scheduler).RescheduleBackups()
internal.RunServer(
Expand All @@ -20,6 +37,8 @@ func main() {

&r.OutputChan,
&r.ErrorChan,
Version,
Build,
)

} else {
Expand Down
2 changes: 2 additions & 0 deletions frontend/composables/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const useApi = defineStore('useApi', () => {
const autoCompletePath = async (path: string) => (await useHttp.get(`/path/autocomplete`, { path })) ?? []
const getLogs = async () => (await useHttp.get(`/logs`)) ?? ({ logs: [], errors: [] } as { logs: string[]; errors: string[] })
const getLogFile = async (file: string) => (await useHttp.get(`/logs/${file}`)) ?? ''
const getVersion = async () => (await useHttp.get(`/version`)) ?? { version: 'unknown', build: 'unknown' }
return {
browseSnapshot,
restoreFromSnapshot,
Expand All @@ -45,5 +46,6 @@ export const useApi = defineStore('useApi', () => {
autoCompletePath,
getLogs,
getLogFile,
getVersion,
}
})
9 changes: 8 additions & 1 deletion frontend/pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</UAlert>
</div>
</div>
<div class="text-xs text-center mt-10">Resticity<br />Version: {{ version }}<br />Build: {{ build }}</div>
</div>
</template>

Expand All @@ -54,11 +55,14 @@
const preserveErrorLogsDays = ref(7)
const version = ref('')
const build = ref('')
const update = _.debounce(() => {
saveSettings()
}, 300)
onMounted(() => {
onMounted(async () => {
theme.value = useSettings().settings.app_settings.theme
notifiyOnScheduleError.value = useSettings().settings.app_settings.notifications.on_schedule_error
notifiyOnScheduleStart.value = useSettings().settings.app_settings.notifications.on_schedule_start
Expand All @@ -74,6 +78,9 @@
useColorMode().preference = theme.value
}
)
const vb = await useApi().getVersion()
version.value = vb.version
build.value = vb.build
})
function saveSettings() {
Expand Down
21 changes: 1 addition & 20 deletions internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package internal

import (
"flag"
"fmt"
"os"

"github.com/charmbracelet/log"
)

func GetVersion() string {
return "0.0.1"
}

type FlagArgs struct {
ConfigFile string
Help bool
Expand All @@ -31,7 +26,6 @@ type Resticity struct {

func NewResticity() (Resticity, error) {
flagArgs := ParseFlags()
flagArgs.PrintVersionOrHelp()

outputChan := make(chan ChanMsg)
errorChan := make(chan ChanMsg)
Expand Down Expand Up @@ -59,24 +53,11 @@ func ParseFlags() FlagArgs {
return flagArgs
}

func (flagArgs *FlagArgs) PrintVersionOrHelp() {
if flagArgs.Version {
fmt.Println("resticity " + GetVersion())
os.Exit(0)
}

if flagArgs.Help {
fmt.Println("resticity " + GetVersion())
flag.PrintDefaults()
os.Exit(0)
}
}

func SetLogLevel() {
l, err := log.ParseLevel(os.Getenv("RESTICITY_LOG_LEVEL"))
if err == nil {
log.SetLevel(l)
} else {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.WarnLevel)
}
}
7 changes: 7 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func RunServer(

outputChan *chan ChanMsg,
errorChan *chan ChanMsg,
version string,
build string,
) {

server := fiber.New()
Expand Down Expand Up @@ -237,6 +239,11 @@ func RunServer(
return c.SendString(c.Params("action") + " schedule in the background")
})

api.Get("/version", func(c *fiber.Ctx) error {
log.Debug(version, build)
return c.JSON(fiber.Map{"version": version, "build": build})
})

api.Get("/logs", func(c *fiber.Ctx) error {
logs, erros := GetLogFiles()
return c.JSON(fiber.Map{"logs": logs, "errors": erros})
Expand Down
27 changes: 20 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"embed"
"flag"
"fmt"
"os"
"strings"

"github.com/ad-on-is/resticity/internal"
"github.com/thoas/go-funk"

"github.com/charmbracelet/log"
"github.com/wailsapp/wails/v2"
Expand All @@ -18,15 +18,26 @@ import (
//go:embed all:frontend/.output/public
var assets embed.FS

func test(arr *[]string, n string) bool {
return funk.Find(*arr, func(s string) bool {
return strings.Contains(s, n)
}) != nil
}
var (
Version string
Build string
)

func main() {
internal.SetLogLevel()
r, err := internal.NewResticity()

if r.FlagArgs.Version {
fmt.Println("resticity - version=" + Version + ", build=" + Build + "")
os.Exit(0)
}

if r.FlagArgs.Help {
fmt.Println("resticity " + Version + " (build " + Build + ")")
flag.PrintDefaults()
os.Exit(0)
}

r.Scheduler.Assets = &assets
if err == nil {
(r.Scheduler).RescheduleBackups()
Expand All @@ -36,6 +47,8 @@ func main() {
r.Settings,
&r.OutputChan,
&r.ErrorChan,
Version,
Build,
)
Desktop(r.Scheduler, r.Restic, r.Settings, r.FlagArgs.Background)
} else {
Expand Down

0 comments on commit fbf84ab

Please sign in to comment.