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
|
package cmd
import (
"encoding/json"
"fmt"
"os"
"text/tabwriter"
"go.jolheiser.com/tmpl/registry"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var (
Source = &cli.Command{
Name: "source",
Usage: "Commands for working with sources",
Description: "Commands for working with sources, short-hand flags for easier downloads",
Action: SourceList.Action,
Subcommands: []*cli.Command{
SourceList,
SourceAdd,
SourceRemove,
},
}
SourceList = &cli.Command{
Name: "list",
Usage: "List available sources",
Description: "List all available sources in the registry",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "JSON format",
},
},
Action: runSourceList,
}
SourceAdd = &cli.Command{
Name: "add",
Usage: "Add a source",
Description: "Add a new source to the registry",
ArgsUsage: "[base URL] [name]",
Action: runSourceAdd,
}
SourceRemove = &cli.Command{
Name: "remove",
Usage: "Remove a source",
Description: "Remove a source from the registry",
ArgsUsage: "[name]",
Action: runSourceRemove,
}
)
func runSourceList(ctx *cli.Context) error {
reg, err := registry.Open(registryFlag)
if err != nil {
return err
}
if ctx.Bool("json") {
return json.NewEncoder(os.Stdout).Encode(reg.Sources)
}
wr := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
if _, err := fmt.Fprintf(wr, "NAME\tURL\n"); err != nil {
return err
}
for _, s := range reg.Sources {
if _, err := fmt.Fprintf(wr, "%s\t%s\n", s.Name, s.URL); err != nil {
return err
}
}
return wr.Flush()
}
func runSourceAdd(ctx *cli.Context) error {
if ctx.NArg() < 2 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
}
reg, err := registry.Open(registryFlag)
if err != nil {
return err
}
s, err := reg.AddSource(ctx.Args().First(), ctx.Args().Get(1))
if err != nil {
return err
}
log.Info().Msgf("Added new source %q", s.Name)
return nil
}
func runSourceRemove(ctx *cli.Context) error {
if ctx.NArg() < 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
}
reg, err := registry.Open(registryFlag)
if err != nil {
return err
}
if err := reg.RemoveSource(ctx.Args().First()); err != nil {
return err
}
log.Info().Msgf("Successfully removed source for %q", ctx.Args().First())
return nil
}
|