Skip to content

Commit

Permalink
feat: move system options to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed May 6, 2024
1 parent 5efbdca commit cd4a466
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
19 changes: 2 additions & 17 deletions internal/lpac/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"os/exec"
"path/filepath"
"runtime"

"github.com/damonto/estkme-cloud/internal/config"
"github.com/damonto/estkme-cloud/internal/driver"
Expand All @@ -24,13 +23,10 @@ func NewCmder(APDU driver.APDU) *Cmder {
func (c *Cmder) Run(arguments []string, dst any, progress Progress) error {
c.APDU.Lock()
defer c.APDU.Unlock()
cmd := exec.Command(c.binName(), arguments...)
cmd := exec.Command(filepath.Join(config.C.DataDir, lpacPath()), arguments...)
cmd.Dir = config.C.DataDir
cmd.Env = append(cmd.Env, "LPAC_APDU=stdio")
// Windows requires libcurl.dll to be in the same directory as the binary
if runtime.GOOS == "windows" {
cmd.Env = append(cmd.Env, "LIBCURL="+filepath.Join(config.C.DataDir, "libcurl.dll"))
}
configureSystemOptions(cmd)

// We don't need check the error output, because we are using the stdio interface. (most of the time, the error output is empty.)
stdout, _ := cmd.StdoutPipe()
Expand All @@ -53,17 +49,6 @@ func (c *Cmder) Run(arguments []string, dst any, progress Progress) error {
return cmd.Wait()
}

func (c *Cmder) binName() string {
var binName string
switch runtime.GOOS {
case "windows":
binName = "lpac.exe"
default:
binName = "lpac"
}
return filepath.Join(config.C.DataDir, binName)
}

func (c *Cmder) handleOutput(output string, input io.WriteCloser, dst any, progress Progress) error {
var commandOutput CommandOutput
if err := json.Unmarshal([]byte(output), &commandOutput); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/lpac/proc_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !windows

package lpac

import (
"os/exec"
)

func configureSystemOptions(cmd *exec.Cmd) {
// Do nothing on non-Windows systems.
}

func lpacPath() string {
return "lpac"
}
22 changes: 22 additions & 0 deletions internal/lpac/proc_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows

package lpac

import (
"os/exec"
"path/filepath"
"syscall"

"github.com/damonto/estkme-cloud/internal/config"
)

func configureSystemOptions(cmd *exec.Cmd) {
cmd.Env = append(cmd.Env, "LIBCURL="+filepath.Join(config.C.DataDir, "libcurl.dll"))
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
}
}

func lpacPath() string {
return "lpac.exe"
}

0 comments on commit cd4a466

Please sign in to comment.