Skip to content

Instantly share code, notes, and snippets.

@kemokemo
Last active January 13, 2017 17:51
Show Gist options
  • Save kemokemo/ff9f40a57b3957cc837b60e53e1cbbb8 to your computer and use it in GitHub Desktop.
Save kemokemo/ff9f40a57b3957cc837b60e53e1cbbb8 to your computer and use it in GitHub Desktop.
Update a xml file of installer information using "MsiProperty.vbs".
<?xml version="1.0" encoding="UTF-8"?>
<Softwares xmlns="http://t2wonderland.blogspot.jp/ns/softwares">
<Name>Softwares</Name>
<Version>1.0.0</Version>
<SoftwareList>
<SoftwareInfo>
<MsiPath>data/go1.7.3.windows-amd64.msi</MsiPath>
<ProductVersion>1.0.0</ProductVersion>
</SoftwareInfo>
</SoftwareList>
</Softwares>
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
type Softwares struct {
XMLName xml.Name `xml:"Softwares"`
Xmlns string `xml:"xmlns,attr"`
Name string `xml:"Name"`
Version string `xml:"Version"`
SoftwareList []SoftwareInfo `xml:"SoftwareList>SoftwareInfo"`
}
type SoftwareInfo struct {
XMLName xml.Name `xml:"SoftwareInfo"`
MsiPath string `xml:"MsiPath"`
ProductVersion string `xml:"ProductVersion"`
}
func main() {
for _, xmlPath := range os.Args[1:] {
infos, err := GetSoftwares(xmlPath)
if err != nil {
log.Print("Failed to unmarshal xml: ", err)
os.Exit(1)
}
for i, info := range infos.SoftwareList {
out, err := GetPropertyValue(info.MsiPath, "ProductVersion")
if err != nil {
log.Print("Failed to load propery: ", err)
os.Exit(1)
}
infos.SoftwareList[i].ProductVersion = out
}
data, err := GetIndentedXML(infos)
if err != nil {
log.Print("Failed to marshal xml: ", err)
os.Exit(1)
}
if err = ioutil.WriteFile(xmlPath, data, os.ModePerm); err != nil {
log.Print("Failed to write xml: ", err)
os.Exit(1)
}
}
}
func GetSoftwares(xmlPath string) (Softwares, error) {
infos := Softwares{}
data, err := ioutil.ReadFile(xmlPath)
if err != nil {
return infos, err
}
if err = xml.Unmarshal([]byte(data), &infos); err != nil {
return infos, err
}
return infos, nil
}
func GetPropertyValue(msiPath string, property string) (string, error) {
data, err := exec.Command("cscript", "/nologo", "MsiProperty.vbs", msiPath, property).Output()
if err != nil {
return "", err
}
// Windows標準出力の末尾の改行コードを削除
return strings.Replace(string(data), "\r\n", "", -1), nil
}
func GetIndentedXML(infos Softwares) ([]byte, error) {
data, err := xml.MarshalIndent(infos, "", " ")
if err != nil {
return nil, err
}
data = []byte(xml.Header + string(data))
return data, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment