Skip to content

Commit

Permalink
Merge pull request #1 from tgulacsi/master
Browse files Browse the repository at this point in the history
Add cmd/goversioninfo command-line app, separate out code, added constants for all the langid and charsetid, increased test coverage, fixed code using vet and lint
  • Loading branch information
josephspurrier committed Jan 22, 2015
2 parents eb3ce99 + 8b4a647 commit a8bab91
Show file tree
Hide file tree
Showing 7 changed files with 1,013 additions and 587 deletions.
205 changes: 205 additions & 0 deletions cmd/goversioninfo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright 2015 Tamás Gulácsi
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"

"github.com/josephspurrier/goversioninfo"
)

func main() {
flagExample := flag.Bool("example", false, "just dump out an example versioninfo.json to stdout")
flagOut := flag.String("o", "resource.syso", "output file name")

flagComment := flag.String("comment", "", "StringFileInfo.Comments")
flagCompany := flag.String("company", "", "StringFileInfo.CompanyName")
flagDescription := flag.String("description", "", "StringFileInfo.FileDescription")
flagFileVersion := flag.String("file-version", "", "StringFileInfo.FileVersion")
flagInternalName := flag.String("internal-name", "", "StringFileInfo.InternalName")
flagCopyright := flag.String("copyright", "", "StringFileInfo.LegalCopyright")
flagTrademark := flag.String("trademark", "", "StringFileInfo.LegalTrademarks")
flagOriginalName := flag.String("original-name", "", "StringFileInfo.OriginalFilename")
flagPrivateBuild := flag.String("private-build", "", "StringFileInfo.PrivateBuild")
flagProductName := flag.String("product-name", "", "StringFileInfo.ProductName")
flagProductVersion := flag.String("product-version", "", "StringFileInfo.ProductVersion")
flagSpecialBuild := flag.String("special-build", "", "StringFileInfo.SpecialBuild")

flagTranslation := flag.Int("translation", 0, "translation ID")
flagCharset := flag.Int("charset", 0, "charset ID")

flagVerMajor := flag.Int("ver-major", -1, "FileVersion.Major")
flagVerMinor := flag.Int("ver-minor", -1, "FileVersion.Minor")
flagVerPatch := flag.Int("ver-patch", -1, "FileVersion.Patch")
flagVerBuild := flag.Int("ver-build", -1, "FileVersion.Build")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <versioninfo.json>\n\nPossible flags:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *flagExample {
io.WriteString(os.Stdout, example)
return
}

configFile := flag.Arg(0)
if configFile == "" {
configFile = "versioninfo.json"
}
var err error
var input = io.ReadCloser(os.Stdin)
if configFile != "-" {
if input, err = os.Open(configFile); err != nil {
log.Printf("Cannot open %q: %v", configFile, err)
os.Exit(1)
}
}

// Read the config file
jsonBytes, err := ioutil.ReadAll(input)
input.Close()
if err != nil {
log.Printf("Error reading %q: %v", configFile, err)
os.Exit(1)
}

// Create a new container
vi := &goversioninfo.VersionInfo{}

// Parse the config
if err := vi.ParseJSON(jsonBytes); err != nil {
log.Printf("Could not parse the .json file: %v", err)
os.Exit(2)
}

// Override from flags
if *flagComment != "" {
vi.StringFileInfo.Comments = *flagComment
}
if *flagCompany != "" {
vi.StringFileInfo.CompanyName = *flagCompany
}
if *flagDescription != "" {
vi.StringFileInfo.FileDescription = *flagDescription
}
if *flagFileVersion != "" {
vi.StringFileInfo.FileVersion = *flagFileVersion
}
if *flagInternalName != "" {
vi.StringFileInfo.InternalName = *flagInternalName
}
if *flagCopyright != "" {
vi.StringFileInfo.LegalCopyright = *flagCopyright
}
if *flagTrademark != "" {
vi.StringFileInfo.LegalTrademarks = *flagTrademark
}
if *flagOriginalName != "" {
vi.StringFileInfo.OriginalFilename = *flagOriginalName
}
if *flagPrivateBuild != "" {
vi.StringFileInfo.PrivateBuild = *flagPrivateBuild
}
if *flagProductName != "" {
vi.StringFileInfo.ProductName = *flagProductName
}
if *flagProductVersion != "" {
vi.StringFileInfo.ProductVersion = *flagProductVersion
}
if *flagSpecialBuild != "" {
vi.StringFileInfo.SpecialBuild = *flagSpecialBuild
}

if *flagTranslation > 0 {
vi.VarFileInfo.Translation.LangID = goversioninfo.LangID(*flagTranslation)
}
if *flagCharset > 0 {
vi.VarFileInfo.Translation.CharsetID = goversioninfo.CharsetID(*flagCharset)
}

if *flagVerMajor >= 0 {
vi.FixedFileInfo.FileVersion.Major = *flagVerMajor
}
if *flagVerMinor >= 0 {
vi.FixedFileInfo.FileVersion.Minor = *flagVerMinor
}
if *flagVerPatch >= 0 {
vi.FixedFileInfo.FileVersion.Patch = *flagVerPatch
}
if *flagVerBuild >= 0 {
vi.FixedFileInfo.FileVersion.Build = *flagVerBuild
}

// Fill the structures with config data
vi.Build()

// Write the data to a buffer
vi.Walk()

// Create the file
if err := vi.WriteSyso(*flagOut); err != nil {
log.Printf("Error writing syso: %v", err)
os.Exit(3)
}
}

const example = `{
"FixedFileInfo": {
"FileVersion": {
"Major": 6,
"Minor": 3,
"Patch": 9600,
"Build": 17284
},
"ProductVersion": {
"Major": 6,
"Minor": 3,
"Patch": 9600,
"Build": 17284
},
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo": {
"Comments": "",
"CompanyName": "Joseph Spurrier Ltd.",
"FileDescription": "",
"FileVersion": "6.3.9600.17284 (aaa.140822-1915)",
"InternalName": "goversioninfo",
"LegalCopyright": "© Joseph Spurrier. Licensed under the Apache License, Version 2.0",
"LegalTrademarks": "",
"OriginalFilename": "goversioninfo",
"PrivateBuild": "",
"ProductName": "Go Version Info",
"ProductVersion": "6.3.9600.17284",
"SpecialBuild": ""
},
"VarFileInfo": {
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
}
}`
24 changes: 12 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// Author: Joseph Spurrier (http://josephspurrier.com)
// License: http://www.apache.org/licenses/LICENSE-2.0.html

package goversioninfo_test
package goversioninfo

import (
"fmt"
"github.com/josephspurrier/goversioninfo"
"io/ioutil"
)

Expand All @@ -25,19 +24,20 @@ func logic() {
}

// Create a new container
vi := &goversioninfo.VersionInfo{}
vi := &VersionInfo{}

// Parse the config
if ok := vi.ParseJSON(jsonBytes); ok {
// Fill the structures with config data
vi.Build()
if err := vi.ParseJSON(jsonBytes); err != nil {
fmt.Println("Could not parse the .json file")
}
// Fill the structures with config data
vi.Build()

// Write the data to a buffer
vi.Walk()
// Write the data to a buffer
vi.Walk()

// Create the file
vi.WriteSyso("resource.syso")
} else {
fmt.Println("Could not parse the .json file")
// Create the file
if err := vi.WriteSyso("resource.syso"); err != nil {
fmt.Printf("Could not write resource.syso: %v", err)
}
}
Loading

0 comments on commit a8bab91

Please sign in to comment.