-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from wking/version-option
main: Add -version
- Loading branch information
Showing
4 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
||
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |