aboutsummaryrefslogtreecommitdiffgithub
path: root/tools/tools.go
blob: c0a355a8e770eeb45a942e9b832c08cfd4a01bff (plain)
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
// toolbag - dynamic tools for my website
// Copyright (C) 2015 Austin Adams
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package tools

import (
	"errors"
	"html/template"
	"net/http"

	tb "code.austinjadams.com/toolbag"
)

type Tools struct {
	tb    *tb.ToolBag
	templ *template.Template

	args struct {
		source, template string
	}
}

func NewTools() *Tools {
	return &Tools{}
}

func (t *Tools) Name() string { return "tools" }
func (t *Tools) Desc() string { return "list tools" }

// be a catch-all
func (t *Tools) Path() string { return "/" + t.Name() }

func (t *Tools) AddArgs(toolbag *tb.ToolBag) {
	// XXX kind of a hack, but no regrets here
	t.tb = toolbag
	toolbag.StringVar(&t.args.template, tb.Arg(t, "template"), "", "path to template")
	toolbag.StringVar(&t.args.source, tb.Arg(t, "source"), "", "url of toolbag source")
}

func (t *Tools) Init() error {
	if t.args.template == "" {
		return errors.New("missing template arg")
	}
	if t.args.source == "" {
		return errors.New("missing source arg")
	}

	templ, err := template.ParseFiles(t.args.template)
	if err != nil {
		return err
	}
	t.templ = templ
	return nil
}

func (t *Tools) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	err := t.templ.Execute(w, struct {
		Source string
		Tools  []tb.Tool
	}{t.args.source, t.tb.Tools})

	if err != nil {
		tb.LogWhine(t, err)
	}
}