annotate zs_util.go @ 29:dd669a7e582f draft

file, url and output should not be overridden by globals
author zaitsev.serge
date Sun, 30 Aug 2015 14:01:05 +0000
parents d052f3a44195
children b2f491299cee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
1 package main
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
2
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
3 import (
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
4 "bytes"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
5 "io"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
6 "log"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
7 "os"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
8 "os/exec"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
9 "path/filepath"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
10 "strings"
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
11 )
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
12
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
13 func varFunc(s string) func() string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
14 return func() string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
15 return s
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
16 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
17 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
18
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
19 func pluginFunc(cmd string, vars Vars) func(args ...string) string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
20 return func(args ...string) string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
21 out := bytes.NewBuffer(nil)
24
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
22 if err := run(filepath.Join(ZSDIR, cmd), args, vars, out); err != nil {
23
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
23 return cmd + ":" + err.Error()
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
24 } else {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
25 return string(out.Bytes())
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
26 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
27 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
28 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
29
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
30 func builtins() Funcs {
24
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
31 exec := func(cmd string, args ...string) string {
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
32 out := bytes.NewBuffer(nil)
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
33 if err := run(cmd, args, Vars{}, out); err != nil {
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
34 return cmd + ":" + err.Error()
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
35 } else {
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
36 return string(out.Bytes())
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
37 }
23
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
38 return ""
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
39 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
40 return Funcs{
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
41 "exec": exec,
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
42 "zs": func(args ...string) string {
24
d052f3a44195 fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents: 23
diff changeset
43 return exec(os.Args[0], args...)
23
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
44 },
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
45 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
46 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
47
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
48 func renameExt(path, from, to string) string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
49 if from == "" {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
50 from = filepath.Ext(path)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
51 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
52 if strings.HasSuffix(path, from) {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
53 return strings.TrimSuffix(path, from) + to
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
54 } else {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
55 return path
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
56 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
57 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
58
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
59 func globals() Vars {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
60 vars := Vars{}
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
61 for _, e := range os.Environ() {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
62 pair := strings.Split(e, "=")
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
63 if strings.HasPrefix(pair[0], "ZS_") {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
64 vars[strings.ToLower(pair[0][3:])] = pair[1]
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
65 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
66 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
67 return vars
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
68 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
69
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
70 // Converts zs markdown variables into environment variables
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
71 func env(vars Vars) []string {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
72 env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
73 env = append(env, os.Environ()...)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
74 if vars != nil {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
75 for k, v := range vars {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
76 env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
77 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
78 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
79 return env
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
80 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
81
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
82 // Runs command with given arguments and variables, intercepts stderr and
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
83 // redirects stdout into the given writer
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
84 func run(cmd string, args []string, vars Vars, output io.Writer) error {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
85 var errbuf bytes.Buffer
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
86 c := exec.Command(cmd, args...)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
87 c.Env = env(vars)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
88 c.Stdout = output
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
89 c.Stderr = &errbuf
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
90
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
91 err := c.Run()
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
92
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
93 if errbuf.Len() > 0 {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
94 log.Println("ERROR:", errbuf.String())
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
95 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
96
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
97 if err != nil {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
98 return err
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
99 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
100 return nil
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
101 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
102
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
103 // Splits a string in exactly two parts by delimiter
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
104 // If no delimiter is found - the second string is be empty
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
105 func split2(s, delim string) (string, string) {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
106 parts := strings.SplitN(s, delim, 2)
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
107 if len(parts) == 2 {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
108 return parts[0], parts[1]
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
109 } else {
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
110 return parts[0], ""
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
111 }
40f55059fbfa fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
zaitsev.serge
parents:
diff changeset
112 }