Skip to content

Commit

Permalink
fix: handle assets dir in homebrew binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefcipa committed Apr 25, 2024
1 parent a168496 commit 6afe4e5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
23 changes: 13 additions & 10 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ archives:
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# Extra files to include in the archive
files:
- LICENSE
- README.md
Expand All @@ -44,18 +45,24 @@ changelog:

brews:
- name: novus
homepage: "https://github.com/jozefcipa/novus"
description: "A local HTTPS proxy for a delightful developer experience."
license: "MIT"

url_template: "https://github.com/jozefcipa/novus/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
download_strategy: CurlDownloadStrategy

commit_author:
name: goreleaserbot
email: bot@goreleaser.com
commit_msg_template: "chore(release): brew formula update for {{ .ProjectName }} version {{ .Tag }}"

repository:
owner: jozefcipa
name: homebrew-novus
git:
url: 'git@github.com:jozefcipa/homebrew-novus.git'
private_key: '{{ .Env.GITHUB_PRIVATE_KEY_PATH }}'
directory: .
homepage: "https://github.com/jozefcipa/novus"
description: "A local HTTPS proxy for a delightful developer experience."
license: "MIT"

# Setting this will prevent goreleaser to actually try to commit the updated
# formula - instead, the formula file will be stored on the dist directory
Expand All @@ -64,9 +71,5 @@ brews:
# in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1
skip_upload: false # TODO set to `auto`

repository:
owner: jozefcipa
name: homebrew-novus
git:
url: 'git@github.com:jozefcipa/homebrew-novus.git'
private_key: '{{ .Env.GITHUB_PRIVATE_KEY_PATH }}'
extra_install: |
prefix.install Dir["assets/*"]
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (config *NovusConfig) validate() {
func createDefaultConfigFile() {
// if we didn't find config file, let's create the default one
err := fs.Copy(
filepath.Join(fs.NovusDir, "assets/novus.example.yml"),
filepath.Join(fs.AssetsDir, "novus.example.yml"),
filepath.Join(fs.CurrentDir, configFileName),
)

Expand Down
35 changes: 25 additions & 10 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var UserHomeDir string
var CurrentDir string
var NovusDir string
var AssetsDir string

// Cannot use `init()` here because the order in which these init() functions are called across packages causes
// that `DebugEnabled` flag is not yet available here (`rootCmd.init()` is called after `fs.init()`)
Expand All @@ -23,33 +23,48 @@ func ResolveDirs() {
logger.Errorf("Failed to get user home directory\n%v\n", err)
os.Exit(1)
}
UserHomeDir = homeDir

currentDir, err := os.Getwd()
if err != nil {
logger.Errorf("Failed to get current working directory\n%v\n", err)
os.Exit(1)
}
CurrentDir = currentDir

executablePath, err := os.Executable()
if err != nil {
logger.Errorf("Failed to get novus binary directory\n%v\n", err)
os.Exit(1)
}
novusDir := filepath.Dir(executablePath)
novusBinaryDir := filepath.Dir(executablePath)
// When running in development with `go run` it gives temporary directory,
// therefore set the novus dir path to the current directory
if strings.Contains(novusDir, "go-build") {
novusDir = currentDir
if strings.Contains(novusBinaryDir, "go-build") {
novusBinaryDir = currentDir
// In local develop environment, the ./assets are stored next to the output binary
// - ./novus
// - ./assets/...
AssetsDir = filepath.Join(currentDir, "assets")
} else {
// If a non-develop binary is used, the ./assets directory is one level above in the filesystem
// This is the Homebrew structure
// - ./bin/novus
// - ./assets/...
AssetsDir = filepath.Join(novusBinaryDir, "..", "assets")
}

// Make sure assets directory is available
if !FileExists(AssetsDir) {
logger.Errorf("Assets directory not found: %s\n", AssetsDir)
os.Exit(1)
}

UserHomeDir = homeDir
NovusDir = novusDir
CurrentDir = currentDir

logger.Debugf(
"Filesystem initialized.\n\tUser Home Directory = %s\n\tNovus Binary Directory = %s\n\tCurrent Directory = %s",
"Filesystem initialized.\n\tUser Home Directory = %s\n\tCurrent Directory = %s\n\tAssets Directory = %s",
UserHomeDir,
NovusDir,
CurrentDir,
AssetsDir,
)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func writeServerConfig(app string, serverConfig string) {

func buildServerConfig(novusConfig config.NovusConfig, sslCerts shared.DomainCertificates) string {
// Read template files
configTemplate := fs.ReadFileOrExit(filepath.Join(fs.NovusDir, "assets/nginx/config.template.conf"))
serverConfigTemplate := fs.ReadFileOrExit(filepath.Join(fs.NovusDir, "assets/nginx/server.template.conf"))
configTemplate := fs.ReadFileOrExit(filepath.Join(fs.AssetsDir, "nginx/config.template.conf"))
serverConfigTemplate := fs.ReadFileOrExit(filepath.Join(fs.AssetsDir, "nginx/server.template.conf"))

// update routes in state
appState, _ := novus.GetAppState()
Expand All @@ -82,15 +82,15 @@ func buildServerConfig(novusConfig config.NovusConfig, sslCerts shared.DomainCer
// create Nginx server block
routeConfig := strings.Replace(serverConfigTemplate, "--SERVER_NAME--", route.Domain, -1)
routeConfig = strings.Replace(routeConfig, "--UPSTREAM_ADDR--", route.Upstream, -1)
routeConfig = strings.Replace(routeConfig, "--ERRORS_DIR--", filepath.Join(fs.NovusDir, "assets/nginx"), -1)
routeConfig = strings.Replace(routeConfig, "--ERRORS_DIR--", filepath.Join(fs.AssetsDir, "nginx"), -1)
routeConfig = strings.Replace(routeConfig, "--SSL_CERT_PATH--", sslCert.CertFilePath, 1)
routeConfig = strings.Replace(routeConfig, "--SSL_KEY_PATH--", sslCert.KeyFilePath, 1)

serversSection += routeConfig + "\n"
}

// Insert servers section into the main config
serverConfig := strings.Replace(configTemplate, "--ERRORS_DIR--", filepath.Join(fs.NovusDir, "assets/nginx"), -1)
serverConfig := strings.Replace(configTemplate, "--ERRORS_DIR--", filepath.Join(fs.AssetsDir, "nginx"), -1)
serverConfig = strings.Replace(serverConfig, "--SERVERS_SECTION--", serversSection, 1)

return serverConfig
Expand Down

0 comments on commit 6afe4e5

Please sign in to comment.