Skip to content

Commit

Permalink
added functionality to configure the location of the cygwin environme…
Browse files Browse the repository at this point in the history
…nt called by the wrapper
  • Loading branch information
Niels Bertram committed May 27, 2016
1 parent a4c889e commit 5f1660b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ _testmain.go
*.test
*.prof
/.project
.idea/
*.iml
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ We assume you have installed a working cygwin shell on your workstation.

### Installation

##### Cygwin Configuration

The wrapper must be able to find the installed cygwin environment. By default it assumes the cygwin home is `c:\cygwin`. If your cygwin is installed in a different location there are 2 options available to tell the wrapper where to find the cygwin environment.

1. Using the `CYGWIN_HOME` Environment Variable

```sh
export CYGWIN_HOME=c:\\cygwin64
```

2. Using the `cygwin.ini` File

The wrapper will look into the directory of the wrapper executable for a `cygwin.ini` file that contains the below ini information.

```ini
[cygwin]
home=c:\cygwin64
```

##### Install Cygwin Packages

Use the cygwin package installer from https://cygwin.com/ and install the following packages:
Expand Down
41 changes: 31 additions & 10 deletions ansible-playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,52 @@ package main
import (
"bufio"
"fmt"
"github.com/vaughan0/go-ini"
"os"
"os/exec"
"path/filepath"
"strings"
"path/filepath"
)

func main() {

var wrapperExecutableName = filepath.Base(os.Args[0])
var wrapperExecutableDir = filepath.Dir(os.Args[0])
var extension = filepath.Ext(wrapperExecutableName)
var executable = wrapperExecutableName[0 : len(wrapperExecutableName)-len(extension)]
// pwd, err := os.Getwd()
var executable = wrapperExecutableName[0 : len(wrapperExecutableName) - len(extension)]
// default cygwin home
var cygHome = "c:/cygwin"
// now check if we need to override the path with env variable
var cygHomeEnv = os.Getenv("CYGWIN_HOME")
if cygHomeEnv != "" {
cygHome = cygHomeEnv
} else {
// try to lookup from ini file
var iniFile = filepath.ToSlash(wrapperExecutableDir + "/cygwin.ini")
file, err := ini.LoadFile(iniFile)
if err == nil {
p, ok := file.Get("cygwin", "home")
if ok {
cygHome = p
}
}
}

args := make([]string, len(os.Args[1:])+1, len(os.Args[1:])+1)
args := make([]string, len(os.Args[1:]) + 1, len(os.Args[1:]) + 1)
args[0] = "/usr/bin/" + executable
for i, a := range os.Args[1:] {
// fmt.Printf("arg[%d] is: %s\n", i+1, a)
args[i+1] = a
args[i + 1] = a
}
cmd := exec.Command("c:\\cygwin\\bin\\bash.exe", "-c", strings.Join(args[:], " "))

var bashCmd = filepath.ToSlash(cygHome + "/bin/bash.exe")
//fmt.Println("execute: " + bashCmd)
cmd := exec.Command(bashCmd, "-c", strings.Join(args[:], " "))

// echo stdin back to console
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating "+executable+" wrapper stdout pipeline", err)
fmt.Fprintln(os.Stderr, "Error creating " + executable + " wrapper stdout pipeline", err)
os.Exit(1)
}

Expand All @@ -53,7 +74,7 @@ func main() {
// echo stderr back to console
errReader, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating "+executable+" wrapper stderr pipeline", err)
fmt.Fprintln(os.Stderr, "Error creating " + executable + " wrapper stderr pipeline", err)
os.Exit(1)
}

Expand All @@ -66,13 +87,13 @@ func main() {

err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting "+executable+" wrapper", err)
fmt.Fprintln(os.Stderr, "Error starting " + executable + " wrapper", err)
os.Exit(1)
}

err = cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for "+executable+" wrapper to finish", err)
fmt.Fprintln(os.Stderr, "Error waiting for " + executable + " wrapper to finish", err)
os.Exit(1)
}

Expand Down

0 comments on commit 5f1660b

Please sign in to comment.