From c6cecf13be303a037ce579e8ff11fca5db409c21 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 5 Sep 2017 08:27:40 -0500 Subject: [PATCH] meta: add a command to be used for testing It opens, reads and writes a file out. This is to be used for testing random docx/xlsx files to ensure we don't break them. --- cmd/test-open-write/main.go | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cmd/test-open-write/main.go diff --git a/cmd/test-open-write/main.go b/cmd/test-open-write/main.go new file mode 100644 index 00000000..dfdde403 --- /dev/null +++ b/cmd/test-open-write/main.go @@ -0,0 +1,52 @@ +// Copyright 2017 Baliance. All rights reserved. +// +// Use of this source code is governed by the terms of the Affero GNU General +// Public License version 3.0 as published by the Free Software Foundation and +// appearing in the file LICENSE included in the packaging of this file. A +// commercial license can be purchased by contacting sales@baliance.com. + +package main + +import ( + "flag" + "fmt" + "log" + "os" + "strings" + + "baliance.com/gooxml/document" + "baliance.com/gooxml/spreadsheet" +) + +func main() { + flag.Parse() + if flag.NArg() == 0 { + fmt.Println("usage: test-open-write filename.docx/.xlsx") + os.Exit(1) + } + fn := flag.Arg(0) + if strings.HasSuffix(strings.ToLower(fn), ".xlsx") { + xlsx, err := spreadsheet.Open(fn) + if err != nil { + log.Fatalf("error opening %s: %s", fn, err) + } + if err := xlsx.Validate(); err != nil { + log.Printf("validation error: %s", err) + } + if err := xlsx.SaveToFile("converted.xlsx"); err != nil { + log.Fatalf("error saving: %s", err) + } + } else if strings.HasSuffix(strings.ToLower(fn), ".docx") { + docx, err := document.Open(fn) + if err != nil { + log.Fatalf("error opening %s: %s", fn, err) + } + if err := docx.Validate(); err != nil { + log.Printf("validation error: %s", err) + } + if err := docx.SaveToFile("converted.docx"); err != nil { + log.Fatalf("error saving: %s", err) + } + } + fmt.Println("reading", fn) +}