Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: embed font file into generated binary #95

Merged
merged 12 commits into from
Jul 15, 2023
Merged

feat: embed font file into generated binary #95

merged 12 commits into from
Jul 15, 2023

Conversation

danstis
Copy link
Owner

@danstis danstis commented Jul 15, 2023

fixes #87

… app and write it to disk if it does not exist.

# Aider chat conversation:

USER: add a function that embeds the ass

ASSISTANT: I
^C KeyboardInterrupt

USER: add a function that embeds the `assets\fonts\Go-Bold.ttf` file into the app, then checks the filesystem to see if the file exists. If it does not exist on disk the function should save the embedded file to disk at the path `assets\fonts\Go-Bold.ttf`.

ASSISTANT: To achieve this, we need to modify the `main.go` file. We will use the `embed` package in Go to embed the font file into the binary. Then, we will add a function to check if the file exists on disk and if not, write the embedded file to disk.

Here are the changes:

```go
main.go
<<<<<<< ORIGINAL
import (
	"bytes"
	"fmt"
	"log"
	"time"

	"fyne.io/systray"
	"github.com/BurntSushi/toml"
	ico "github.com/Kodeworks/golang-image-ico"
	"github.com/fogleman/gg"
	"gopkg.in/resty.v1"
)

// Version info maintained by goreleaser: https://goreleaser.com/cookbooks/using-main.version/
var (
	version = "dev"
)
=======
import (
	"bytes"
	_ "embed"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"

	"fyne.io/systray"
	"github.com/BurntSushi/toml"
	ico "github.com/Kodeworks/golang-image-ico"
	"github.com/fogleman/gg"
	"gopkg.in/resty.v1"
)

// Version info maintained by goreleaser: https://goreleaser.com/cookbooks/using-main.version/
var (
	version = "dev"
	//go:embed assets/fonts/Go-Bold.ttf
	embeddedFont []byte
)
>>>>>>> UPDATED
```

```go
main.go
<<<<<<< ORIGINAL
func onReady() {
	// Get the settings
	var config Settings
	_, err := toml.DecodeFile(configFile, &config)
	if err != nil {
		log.Fatalf("Failed to read configuration file: %v", err)
	}
	err = config.getUserDetail()
	if err != nil {
		log.Printf("Failed to get Toggl user details: %v\n", err)
	}
	log.Printf("- Workspaces:%v\n", config.Workspaces)

	// Configure the systray item
	updateIcon(0, config.HighlightThreshold)
	mTitle := systray.AddMenuItem("Toggl Weekly Tracker", "Title")
	mTitle.Disable()
	mVersion := systray.AddMenuItem(fmt.Sprintf("v%v", version), "Version")
	mVersion.Disable()
	systray.AddSeparator()
	systray.SetTitle("Toggl Weekly Time")
	menuItems := make(map[int32]*systray.MenuItem)
	for _, item := range config.Workspaces {
		menuItems[item.ID] = systray.AddMenuItem(fmt.Sprintf(templateFormat, item.Name, 0, 0), item.Name)
	}
	menuItems[0] = systray.AddMenuItem(fmt.Sprintf(templateFormat, "Total", 0, 0), "Total")
	systray.AddSeparator()
	mRefresh := systray.AddMenuItem("Force Refresh", "Force Refresh the data")
	systray.AddSeparator()
	mQuit := systray.AddMenuItem("Quit", "Quit the app")
=======
func onReady() {
	// Check if the font file exists, if not, write the embedded file to disk
	fontPath := "assets/fonts/Go-Bold.ttf"
	if _, err := os.Stat(fontPath); os.IsNotExist(err) {
		err = ioutil.WriteFile(fontPath, embeddedFont, 0644)
		if err != nil {
			log.Fatalf("Failed to write font file to disk: %v", err)
		}
	}

	// Get the settings
	var config Settings
	_, err := toml.DecodeFile(configFile, &config)
	if err != nil {
		log.Fatalf("Failed to read configuration file: %v", err)
	}
	err = config.getUserDetail()
	if err != nil {
		log.Printf("Failed to get Toggl user details: %v\n", err)
	}
	log.Printf("- Workspaces:%v\n", config.Workspaces)

	// Configure the systray item
	updateIcon(0, config.HighlightThreshold)
	mTitle := systray.AddMenuItem("Toggl Weekly Tracker", "Title")
	mTitle.Disable()
	mVersion := systray.AddMenuItem(fmt.Sprintf("v%v", version), "Version")
	mVersion.Disable()
	systray.AddSeparator()
	systray.SetTitle("Toggl Weekly Time")
	menuItems := make(map[int32]*systray.MenuItem)
	for _, item := range config.Workspaces {
		menuItems[item.ID] = systray.AddMenuItem(fmt.Sprintf(templateFormat, item.Name, 0, 0), item.Name)
	}
	menuItems[0] = systray.AddMenuItem(fmt.Sprintf(templateFormat, "Total", 0, 0), "Total")
	systray.AddSeparator()
	mRefresh := systray.AddMenuItem("Force Refresh", "Force Refresh the data")
	systray.AddSeparator()
	mQuit := systray.AddMenuItem("Quit", "Quit the app")
>>>>>>> UPDATED
```

These changes will embed the `assets/fonts/Go-Bold.ttf` file into the binary and write it to disk if it does not exist when the application starts.
SA1019: "io/ioutil" has been deprecated
…ger to this file.

# Aider chat conversation:

USER: write all log messages to a log file called log.txt in the directory the app is running in

ASSISTANT: To write all log messages to a log file, we need to modify the `main.go` file. We will use the `os` package in Go to create a log file and the `log` package to set the output of the logger to this file.

Here are the changes:

```go
main.go
<<<<<<< ORIGINAL
// Main entry point for the app.
func main() {
	unpackFont()
	systray.Run(onReady, onExit)
}
=======
// Main entry point for the app.
func main() {
	unpackFont()

	// Create a log file
	logFile, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	defer logFile.Close()

	// Set the output of the logger to the log file
	log.SetOutput(logFile)

	systray.Run(onReady, onExit)
}
>>>>>>> UPDATED
```

This change will make all log messages be written to a file named `log.txt` in the directory the application is running in.
# Aider chat conversation:
…rts of the application logs.

# Aider chat conversation:

USER: add log rollover when app starts, keep last 3 starts of the application logs

ASSISTANT: To implement log rollover, we need to modify the `main.go` file. We will use the `os` package in Go to rename old log files and delete the oldest one if more than 3 exist.

Here are the changes:

```go
main.go
<<<<<<< ORIGINAL
// Main entry point for the app.
func main() {
	// Create a log file
	logFile, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	defer logFile.Close()

	unpackFont()

	// Set the output of the logger to the log file
	log.SetOutput(logFile)

	systray.Run(onReady, onExit)
}
=======
// Main entry point for the app.
func main() {
	// Implement log rollover
	if _, err := os.Stat("log.txt.3"); err == nil {
		os.Remove("log.txt.3")
	}
	if _, err := os.Stat("log.txt.2"); err == nil {
		os.Rename("log.txt.2", "log.txt.3")
	}
	if _, err := os.Stat("log.txt.1"); err == nil {
		os.Rename("log.txt.1", "log.txt.2")
	}
	if _, err := os.Stat("log.txt"); err == nil {
		os.Rename("log.txt", "log.txt.1")
	}

	// Create a log file
	logFile, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	defer logFile.Close()

	unpackFont()

	// Set the output of the logger to the log file
	log.SetOutput(logFile)

	systray.Run(onReady, onExit)
}
>>>>>>> UPDATED
```

This change will implement log rollover, keeping the logs of the last 3 starts of the application.
…nd log file creation, and update font path

# Aider chat conversation:
@danstis danstis linked an issue Jul 15, 2023 that may be closed by this pull request
@danstis danstis merged commit 6073b97 into main Jul 15, 2023
10 checks passed
@danstis danstis deleted the danstis/issue87 branch July 15, 2023 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TOG-9] Move assets into the generated binary
1 participant