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

main: Add -version #444

Merged
merged 1 commit into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
``` openSUSE 42.2/ Centos7/ Ubuntu.. ```

### Terraform Version Report
(Provided by running `terraform -v`.)

### Libvirt version
```sh
terraform -v
```

```virsh --version```
### Provider and libvirt versions

### terraform-provider-libvirt plugin version (git-hash)
```sh
terraform-provider-libvirt -version
```

``` git log```
If that gives you "was not built correctly", get the Git commit hash from your local provider repository:

```sh
git describe --always --abbrev=40 --dirty
```
___
# Description of Issue/Question

Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
LDFLAGS += -X main.version=$$(git describe --always --abbrev=40 --dirty)

default: build

build: gofmtcheck golint vet
go build
go build -ldflags "${LDFLAGS}"

install:
go install

test:
go test -v -covermode=count -coverprofile=profile.cov ./libvirt
go test -v -covermode=count -coverprofile=profile.cov . ./libvirt

testacc:
./travis/run-tests-acceptance
Expand Down
67 changes: 65 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
package main

import (
"github.com/dmacvicar/terraform-provider-libvirt/libvirt"
"github.com/hashicorp/terraform/plugin"
"flag"
"fmt"
"io"
"log"
"math/rand"
"os"
"time"

"github.com/dmacvicar/terraform-provider-libvirt/libvirt"
"github.com/hashicorp/terraform/plugin"
libvirtgo "github.com/libvirt/libvirt-go"
MalloZup marked this conversation as resolved.
Show resolved Hide resolved
)

var version = "was not built correctly" // set via the Makefile

func main() {
versionFlag := flag.Bool("version", false, "print version information and exit")
flag.Parse()
if *versionFlag {
err := printVersion(os.Stdout)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}

defer libvirt.CleanupLibvirtConnections()

plugin.Serve(&plugin.ServeOpts{
ProviderFunc: libvirt.Provider,
})
}

func printVersion(writer io.Writer) error {
fmt.Fprintf(writer, "%s %s\n", os.Args[0], version)

fmt.Fprintf(writer, "Compiled against library: libvirt %s\n", parseVersion(libvirtgo.VERSION_NUMBER))

libvirtVersion, err := libvirtgo.GetVersion()
if err != nil {
return err
}
fmt.Fprintf(writer, "Using library: libvirt %s\n", parseVersion(libvirtVersion))

conn, err := libvirtgo.NewConnect("")
if err != nil {
return err
}
defer conn.Close()

hvType, err := conn.GetType()
if err != nil {
return err
}
libvirtVersion, err = conn.GetVersion()
if err != nil {
return err
}
fmt.Fprintf(writer, "Running hypervisor: %s %s\n", hvType, parseVersion(libvirtVersion))

libvirtVersion, err = conn.GetLibVersion()
if err != nil {
return err
}
fmt.Fprintf(writer, "Running against daemon: %s\n", parseVersion(libvirtVersion))

return nil
}

func parseVersion(version uint32) string {
release := version % 1000
version /= 1000
minor := version % 1000
major := version / 1000
return fmt.Sprintf("%d.%d.%d", major, minor, release)
}

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
21 changes: 21 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"bytes"
"regexp"
"testing"
)

func TestPrintVersion(t *testing.T) {
buf := &bytes.Buffer{}
err := printVersion(buf)
if err != nil {
t.Fatal(err)
}
output := buf.Bytes()

re := regexp.MustCompile("^.*terraform-provider-libvirt.test was not built correctly\\nCompiled against library: libvirt [0-9.]*\\nUsing library: libvirt [0-9.]*\\nRunning hypervisor: .* [0-9.]*\\nRunning against daemon: [0-9.]*\\n$")
if !re.Match(output) {
t.Fatalf("unexpected output:\n%q", string(output))
}
}