annotate zs_util.go @ 23:40f55059fbfa draft

fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
author zaitsev.serge
date Sun, 30 Aug 2015 12:20:35 +0000
parents
children d052f3a44195
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)
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
22 if err := run(cmd, args, vars, out); 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
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 {
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
31 exec := func(s ...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
32 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
33 }
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
34 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
35 "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
36 "zs": 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
37 cmd := []string{"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
38 cmd = append(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
39 return exec(cmd...)
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 },
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 }
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 }
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
43
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 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
45 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
46 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
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 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
49 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
50 } 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
51 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
52 }
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 }
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
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 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
56 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
57 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
58 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
59 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
60 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
61 }
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 }
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 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
64 }
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 // 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
67 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
68 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
69 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
70 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
71 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
72 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
73 }
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 }
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 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
76 }
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 // 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
79 // 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
80 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
81 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
82 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
83 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
84 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
85 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
86
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 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
88
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 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
90 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
91 }
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 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
94 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
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 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
97 }
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
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 // 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
100 // 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
101 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
102 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
103 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
104 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
105 } 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
106 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
107 }
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 }