Home

tmpl @c01d965ad77ba352724d87b182bc68f68879a1c6 - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / cmd / source.go
- raw
  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
package cmd

import (
	"errors"
	"fmt"
	"os"
	"text/tabwriter"

	"go.jolheiser.com/tmpl/cmd/flags"
	"go.jolheiser.com/tmpl/registry"

	"github.com/urfave/cli/v2"
	"go.jolheiser.com/beaver"
)

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",
		Usage:       "AddTemplate a source",
		Description: "AddTemplate a new source to the registry",
		Action:      runSourceAdd,
	}

	SourceRemove = &cli.Command{
		Name:        "remove",
		Usage:       "RemoveTemplate a source",
		Description: "RemoveTemplate a source from the registry",
		Action:      runSourceRemove,
	}
)

func runSourceList(_ *cli.Context) error {
	reg, err := registry.Open(flags.Registry)
	if err != nil {
		return err
	}

	wr := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)

	fmt.Println("Sources")
	for _, s := range reg.Sources {
		if _, err := fmt.Fprintf(wr, "%s\t%s", s.Name, s.URL); err != nil {
			return err
		}
	}
	fmt.Println()
	return nil
}

func runSourceAdd(ctx *cli.Context) error {
	if ctx.NArg() < 2 {
		return errors.New("<repo> <name>")
	}

	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
	}

	beaver.Infof("Added new source %s", s.Name)
	return nil
}

func runSourceRemove(ctx *cli.Context) error {
	if ctx.NArg() < 1 {
		return errors.New("<name>")
	}

	reg, err := registry.Open(flags.Registry)
	if err != nil {
		return err
	}

	if err := reg.RemoveSource(ctx.Args().First()); err != nil {
		return err
	}

	beaver.Infof("Successfully removed source for %s", ctx.Args().First())
	return nil
}