summarylogtreecommitdiffstats
path: root/plugins.go
blob: 75d16ffff15e7564d27311b9f0f4c1e0f5370ff0 (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
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"sort"
)

const URL = "https://caddyserver.com/api/download-page"

type Plugin struct {
	Name       string
	ImportPath string
}
type PluginList struct {
	Plugins []Plugin `json:"plugins"`
}

func getPlugins() []Plugin {
	resp, err := http.Get(URL)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	list := PluginList{}
	err = json.Unmarshal(body, &list)
	if err != nil {
		log.Fatal(err)
	}
	return list.Plugins
}
func main() {
	plugins := getPlugins()
	sort.Slice(plugins, func(i, j int) bool {
		return plugins[i].Name < plugins[j].Name
	})
	if len(os.Args) == 1 {
		fmt.Println("plugins=(")
		for _, plugin := range plugins {
			fmt.Printf("#    '%s'\n", plugin.Name)
		}
		fmt.Println(")")
		return
	}
	pluginsMap := make(map[string]string)
	for _, plugin := range plugins {
		pluginsMap[plugin.Name] = plugin.ImportPath
	}
	for _, name := range os.Args[1:] {
		path, ok := pluginsMap[name]
		if !ok {
			log.Fatalf("cannot find plugin %s\n", name)
		}
		fmt.Printf(`_ "%s"`+"\n", path)
	}
}