Skip to content

Commit

Permalink
goreleaser
Browse files Browse the repository at this point in the history
  • Loading branch information
dsokolyuk committed Jun 29, 2024
1 parent 4d5e804 commit fb6b9c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ builds:
- CGO_ENABLED=1
goos:
- windows
- linux
goarch:
- amd64
nfpms:
Expand Down
8 changes: 8 additions & 0 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"strings"
)

func Exists(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
}

return true
}

func Load(filePath string) (*Config, error) {
file, err := os.Open(filePath)
if err != nil {
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
_ "embed"
"fmt"
"log"
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"

_ "net/http/pprof"
"path"

"github.com/DaniilSokolyuk/go-pcap2socks/cfg"
"github.com/DaniilSokolyuk/go-pcap2socks/core"
Expand All @@ -19,13 +20,33 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/stack"
)

//go:embed config.json
var configData string

func main() {
// get config file from first argument or use config.json
var cfgFile string
if len(os.Args) > 1 {
cfgFile = os.Args[1]
} else {
cfgFile = "config.json"
executable, err := os.Executable()
if err != nil {
slog.Error("get executable error", "error", err)
return
}

cfgFile = path.Join(path.Dir(executable), "config.json")
}

cfgExists := cfg.Exists(cfgFile)
if !cfgExists {
slog.Info("Config file not found, creating a new one", "file", cfgFile)
//path to near executable file
err := os.WriteFile(cfgFile, []byte(configData), 0666)
if err != nil {
slog.Error("write config error", "file", cfgFile, "error", err)
return
}
}

config, err := cfg.Load(cfgFile)
Expand Down

0 comments on commit fb6b9c8

Please sign in to comment.