Commit 490dfad5 authored by Mickael Desfrenes's avatar Mickael Desfrenes
Browse files

Slightly better error handling. README edited.

parent 2ae79dcb
# circe-helper
## Build
Development build for your current platform:
go build circe.go
Release build For windows:
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" circe.go
Release build for MacOS:
GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" circe.go
Release build for Linux:
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" circe.go
(Optional) Binary compression, if UPX is available:
upx circe
## Usage
./circe [optional flags] source_file transformation_name destination_file
Example:
./circe test1.html html2pdf test1.pdf
Optional flags:
-endpoint=[circe service endpoint] # will default to env var CIRCE_ENDPOINT
-appuuid=[your application UUID] # will default to env var CIRCE_APP_UUID
-secret=[your application secret] # will default to env var CIRCE_SECRET
\ No newline at end of file
......@@ -9,6 +9,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
......@@ -210,13 +211,18 @@ func (client CirceClient) SendJob(job *Job) error {
httpClient := &http.Client{}
postRequest, err := http.NewRequest("POST", client.Endpoint+"job/", archiveFilePointer)
checkerr(err)
if err != nil {
return err
}
postRequest.Header.Add("Authorization", client.ApplicationUuid+" "+hmacDigest)
response, err := httpClient.Do(postRequest)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New(fmt.Sprintf("Response status error: %v", response.StatusCode))
}
jobUuid, _ := ioutil.ReadAll(response.Body)
job.Uuid = string(jobUuid)
defer os.Remove(archiveFilePointer.Name())
......@@ -268,15 +274,21 @@ func (client CirceClient) Poll(job *Job, destinationFilePath string, timeout int
}
func showHelpAndExit() {
message := `-= Command-line client to the Circe API =-
message := `Command-line client to the Circe API
Usage:
%s [source file] [transformation] [destination file]
%s [optional flags] source_file transformation_name destination_file
Example:
%s test1.html html2pdf test1.pdf
Optional flags:
-endpoint=[circe service endpoint]
-appuuid=[your application UUID]
-secret=[your application secret]
`
fmt.Printf(message, os.Args[0], os.Args[0])
os.Exit(0)
......@@ -290,16 +302,20 @@ func getEnv(key, fallback string) string {
}
func main() {
circeEndpoint := getEnv("CIRCE_ENDPOINT", "https://circe.bontards.com/")
circeSecret := getEnv("CIRCE_SECRET", "@Jy_<b)Fy%/TkVlJ=T#kZIyFt3E_?B.T")
circeAppUuid := getEnv("CIRCE_APP_UUID", "7f5a3ce0-a729-4e41-80df-cad73aee96b2")
/*
if circeEndpoint == "" || circeAppUuid == "" || circeSecret == "" {
log.Fatalln("Please set the following environment variables: CIRCE_ENDPOINT, CIRCE_SECRET, CIRCE_APP_UUID")
}
*/
args := os.Args[1:]
if len(args) < 3 {
var circeEndpoint string
var circeSecret string
var circeAppUuid string
var seeHelp bool
flag.BoolVar(&seeHelp, "h", false, "Show help and exit")
flag.BoolVar(&seeHelp, "help", false, "Show help and exit")
flag.StringVar(&circeEndpoint, "endpoint", getEnv("CIRCE_ENDPOINT", "https://circe.bontards.com/"), "Service Endpoint")
flag.StringVar(&circeSecret, "secret", getEnv("CIRCE_SECRET", "@Jy_<b)Fy%/TkVlJ=T#kZIyFt3E_?B.T"), "Secret Key")
flag.StringVar(&circeAppUuid, "appuuid", getEnv("CIRCE_APP_UUID", "7f5a3ce0-a729-4e41-80df-cad73aee96b2"), "Application UUID")
flag.Parse()
args := flag.Args()
if len(args) < 3 || seeHelp {
showHelpAndExit()
}
......@@ -320,11 +336,18 @@ func main() {
}
tmpTgzFile, err := ioutil.TempFile(os.TempDir(), "*.tar.gz")
checkerr(err)
if err != nil {
log.Fatal("Could not create temporary tar")
}
defer os.Remove(tmpTgzFile.Name())
_, err = circe.Poll(&job, tmpTgzFile.Name(), 30)
checkerr(err)
if err != nil {
log.Println("Could not complete document conversion. Reason:")
log.Fatal(err)
}
ExtractTarGz(tmpTgzFile, destination)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment