2020-11-17 05:41:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"go.jolheiser.com/tmpl/cmd/flags"
|
|
|
|
"go.jolheiser.com/tmpl/registry"
|
|
|
|
|
2021-11-02 03:08:41 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-17 05:41:34 +00:00
|
|
|
"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",
|
|
|
|
Action: runSourceList,
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceAdd = &cli.Command{
|
|
|
|
Name: "add",
|
2020-11-18 05:28:07 +00:00
|
|
|
Usage: "Add a source",
|
|
|
|
Description: "Add a new source to the registry",
|
2020-11-22 05:25:24 +00:00
|
|
|
ArgsUsage: "[base URL] [name]",
|
2020-11-17 05:41:34 +00:00
|
|
|
Action: runSourceAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceRemove = &cli.Command{
|
|
|
|
Name: "remove",
|
2020-11-18 05:28:07 +00:00
|
|
|
Usage: "Remove a source",
|
|
|
|
Description: "Remove a source from the registry",
|
2020-11-22 05:25:24 +00:00
|
|
|
ArgsUsage: "[name]",
|
2020-11-17 05:41:34 +00:00
|
|
|
Action: runSourceRemove,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func runSourceList(_ *cli.Context) error {
|
|
|
|
reg, err := registry.Open(flags.Registry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-19 05:19:19 +00:00
|
|
|
wr := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
|
|
|
if _, err := fmt.Fprintf(wr, "NAME\tURL\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-17 05:41:34 +00:00
|
|
|
for _, s := range reg.Sources {
|
2020-11-17 06:04:57 +00:00
|
|
|
if _, err := fmt.Fprintf(wr, "%s\t%s\n", s.Name, s.URL); err != nil {
|
2020-11-17 05:41:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:04:57 +00:00
|
|
|
return wr.Flush()
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runSourceAdd(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 2 {
|
2020-11-22 05:25:24 +00:00
|
|
|
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reg, err := registry.Open(flags.Registry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := reg.AddSource(ctx.Args().First(), ctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-02 03:08:41 +00:00
|
|
|
log.Info().Msgf("Added new source %q", s.Name)
|
2020-11-17 05:41:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runSourceRemove(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 1 {
|
2020-11-22 05:25:24 +00:00
|
|
|
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reg, err := registry.Open(flags.Registry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := reg.RemoveSource(ctx.Args().First()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-02 03:08:41 +00:00
|
|
|
log.Info().Msgf("Successfully removed source for %q", ctx.Args().First())
|
2020-11-17 05:41:34 +00:00
|
|
|
return nil
|
|
|
|
}
|