Created
March 22, 2021 17:48
-
-
Save rgon/bf5abb29f06b573fc7c314be6a52a9ef to your computer and use it in GitHub Desktop.
Onyx Fixture Compressor and Packager: creates .Fixture files from uncompressed folders.
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
///bin/true; exec /usr/bin/env go run "$0" "$@" | |
// Example: | |
// 1. Unzip the fixtureYouWishToModify.Fixture | |
// 2. Modify the fixture by editing MANUFACTURER/FIXTURE/FIXTURE.xml | |
// 3. Call this script passing said uncompressed folder as a parameter | |
// USAGE: ./compressOnyxFixture.go uncompressedFixtureFolder | |
// 4. Your uncompressedFixtureFolder.Fixture file will be ready! | |
// With the help of: https://stackoverflow.com/a/63233911 and https://unix.stackexchange.com/a/577613 | |
// MIT Licensed | |
// 2021 by github.com/rgon | |
package main | |
import ( | |
"archive/zip" | |
"fmt" | |
"io" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
const creatorFAT = 0 | |
// Zips "./input" into "./output.zip" | |
func main() { | |
// Get source folder from args | |
if len(os.Args) < 2 { | |
panic ("USAGE: ./compressOnyxFixture.go uncompressedFixtureFolder") | |
} | |
sourcepath := filepath.Base(os.Args[1]) | |
if _, err := os.Stat(sourcepath); os.IsNotExist(err) { | |
panic("Please enter the folder containing the fixture data with a correct structure.") | |
} | |
fmt.Printf(sourcepath) | |
// open output file | |
file, err := os.Create(sourcepath + ".Fixture") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
archive := zip.NewWriter(file) | |
defer archive.Close() | |
walker := func(path string, info os.FileInfo, err error) error { | |
fmt.Printf("Crawling: %#v\n", path) | |
if err != nil { | |
return err | |
} | |
head, _ := zip.FileInfoHeader(info) | |
head.CreatorVersion = creatorFAT | |
head.Name = strings.Join(strings.Split(path, string(os.PathSeparator))[1:], string(os.PathSeparator)) | |
head.ExternalAttrs = 0 | |
if !info.IsDir() { | |
head.Method = zip.Deflate | |
w, _ := archive.CreateHeader(head) | |
file, err := os.Open(path) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
_, err = io.Copy(w, file) | |
if err != nil { | |
return err | |
} | |
} else if strings.ContainsRune(head.Name, '\u005c') { | |
head.Name += "/" | |
archive.CreateHeader(head) | |
} | |
return nil | |
} | |
err = filepath.Walk(sourcepath, walker) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment