forked from yogendra/plantuml-go
/
plantuml.go
155 lines (134 loc) · 3.82 KB
/
plantuml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"compress/zlib"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
formatTXT = "txt"
formatPNG = "png"
formatSVG = "svg"
outputTypeHASH = "hash"
outputTypeLINK = "link"
outputTypeSAVE = "save"
mapper = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"
)
type Options struct {
Server string
Format string
OutputType string
FileNames []string
}
func main() {
opts := parseArgs()
if len(opts.FileNames) == 0 {
outFileName := "uml_out"
err := process(&opts, os.Stdin, outFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "error processing stdin %s: %v\n", outFileName, err)
}
}
for _, filename := range opts.FileNames {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening file %s: %v\n", filename, err)
}
err = process(&opts, f, strings.TrimSuffix(filename, filepath.Ext(filename)))
if err != nil {
fmt.Fprintf(os.Stderr, "error processing file %s: %v\n", filename, err)
}
}
}
func process(options *Options, file *os.File, basename string) error {
textFormatB, err := ioutil.ReadAll(file)
if err != nil {
return err
}
textFormat := encodeAsTextFormat(textFormatB)
if options.OutputType == outputTypeHASH {
fmt.Printf("%s: %s\n", basename, textFormat)
} else if options.OutputType == outputTypeLINK {
fmt.Printf("%s: %s/%s/~1%s\n", basename, options.Server, options.Format, textFormat)
} else if options.OutputType == outputTypeSAVE {
link := fmt.Sprintf("%s/%s/~1%s", options.Server, options.Format, textFormat)
fileName := fmt.Sprintf("%s.%s", basename, options.Format)
output, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
response, err := http.Get(link)
if err != nil {
fmt.Println(err)
return err
}
if response.StatusCode != 200 {
return fmt.Errorf("Error in Fetching `%s`: %s", link, response.Status)
}
io.Copy(output, response.Body)
response.Body.Close()
output.Close()
}
return nil
}
func encodeAsTextFormat(raw []byte) string {
compressed := deflate(raw)
return base64_encode(compressed)
}
func deflate(input []byte) []byte {
var b bytes.Buffer
w, _ := zlib.NewWriterLevel(&b, zlib.BestCompression)
w.Write(input)
w.Close()
return b.Bytes()
}
func base64_encode(input []byte) string {
var buffer bytes.Buffer
inputLength := len(input)
for i := 0; i < 3-inputLength%3; i++ {
input = append(input, byte(0))
}
for i := 0; i < inputLength; i += 3 {
b1, b2, b3, b4 := input[i], input[i+1], input[i+2], byte(0)
b4 = b3 & 0x3f
b3 = ((b2 & 0xf) << 2) | (b3 >> 6)
b2 = ((b1 & 0x3) << 4) | (b2 >> 4)
b1 = b1 >> 2
for _, b := range []byte{b1, b2, b3, b4} {
buffer.WriteByte(byte(mapper[b]))
}
}
return string(buffer.Bytes())
}
func parseArgs() Options {
flag.CommandLine.Init(os.Args[0], flag.ExitOnError)
server := flag.String("server", "http://plantuml.com/plantuml", "Plantuml `server` address. Used when generating link or extracting output")
format := flag.String("format", formatPNG, fmt.Sprintf("Output `format` type. (Options: %s,%s,%s)", formatPNG, formatSVG, formatTXT))
outputType := flag.String("type", outputTypeSAVE, fmt.Sprintf("Indicates if output type. (Options: %s,%s,%s)", outputTypeSAVE, outputTypeLINK, outputTypeHASH))
help := flag.Bool("help", false, "Show help (this) text")
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
}
if *format != formatPNG && *format != formatSVG && *format != formatTXT {
fmt.Println("Invalid format")
os.Exit(1)
}
if *outputType != outputTypeHASH && *outputType != outputTypeLINK && *outputType != outputTypeSAVE {
fmt.Println("Invalid output type")
os.Exit(1)
}
opts := Options{
Server: *server,
Format: *format,
OutputType: *outputType,
FileNames: flag.Args()}
return opts
}