diff --git a/.ownbrew.yaml b/.ownbrew.yaml index 5a57d4f..1115610 100644 --- a/.ownbrew.yaml +++ b/.ownbrew.yaml @@ -1,5 +1,5 @@ # yaml-language-server: $schema=ownbrew.schema.json -version: '1.0' +version: '1.1' binDir: "bin" tapDir: ".ownbrew/tap" @@ -8,6 +8,7 @@ cellarDir: ".ownbrew/bin" packages: ## https://github.com/golangci/golangci-lint/releases - name: golangci-lint + tags: [ci] tap: foomo/tap/golangci/golangci-lint version: 1.61.0 ## https://github.com/go-courier/husky/releases diff --git a/cmd/config.go b/cmd/config.go index 5fcb183..6baa4a0 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -26,7 +26,7 @@ func NewConfig(root *cobra.Command) { return err } - fmt.Println(util.Highlight(string(out))) + fmt.Println(util.Highlight(string(out), "yaml")) return nil }, } diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..126eb8f --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "fmt" + "os" + "strings" + + pkgcmd "github.com/foomo/ownbrew/pkg/cmd" + "github.com/foomo/ownbrew/pkg/config" + "github.com/foomo/ownbrew/pkg/util" + "github.com/spf13/cobra" +) + +const initTemplate = ` +# yaml-language-server: $schema=https://raw.githubusercontent.com/foomo/ownbrew/refs/heads/main/ownbrew.schema.json +version: '%s' + +binDir: "bin" +tapDir: ".ownbrew/tap" +tempDir: ".ownbrew/tmp" +cellarDir: ".ownbrew/bin" +packages: + ## https://github.com/golangci/golangci-lint/releases + - name: golangci-lint + tags: [ci] + tap: foomo/tap/golangci/golangci-lint + version: 1.61.0 + ## https://github.com/go-courier/husky/releases + - name: husky + tap: foomo/tap/go-courier/husky + version: 1.8.1 +` + +// NewInit represents the init command +func NewInit(root *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "init", + Short: "Init ownbrew", + RunE: func(cmd *cobra.Command, args []string) error { + l := pkgcmd.Logger() + filename := ".ownbrew.yaml" + body := fmt.Sprintf(strings.Trim(initTemplate, "\n"), config.Version) + + if _, err := os.Stat(filename); err != nil && !os.IsNotExist(err) { + return err + } else if err == nil { + l.Warn("ownbrew already initialized") + fmt.Println(util.Highlight(body, "yaml")) + return nil + } + + return os.WriteFile(".ownbrew.yaml", []byte(body), 0600) + }, + } + + root.AddCommand(cmd) + + return cmd +} diff --git a/cmd/install.go b/cmd/install.go index f588896..6bfea04 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -29,6 +29,11 @@ func NewInstall(root *cobra.Command) *cobra.Command { return err } + tags, err := cmd.Flags().GetStringSlice("tags") + if err != nil { + return err + } + brew, err := ownbrew.New(l, ownbrew.WithDry(dry), ownbrew.WithBinDir(cfg.BinDir), @@ -40,11 +45,12 @@ func NewInstall(root *cobra.Command) *cobra.Command { if err != nil { return err } - return brew.Install(cmd.Context()) + return brew.Install(cmd.Context(), tags...) }, } - cmd.Flags().Bool("dry", false, "dry run") + cmd.Flags().Bool("dry", false, "print out the taps that will be installed") + cmd.Flags().StringSlice("tags", nil, "filter by tags (e.g. ci,-test") root.AddCommand(cmd) diff --git a/cmd/root.go b/cmd/root.go index 6310df5..37d2832 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,9 +12,10 @@ var root *cobra.Command func init() { root = NewRoot() - NewVersion(root) - NewInstall(root) NewConfig(root) + NewInit(root) + NewInstall(root) + NewVersion(root) cobra.OnInitialize(pkgcmd.InitConfig) } diff --git a/ownbrew.schema.json b/ownbrew.schema.json index 10d2f8b..bae50a5 100644 --- a/ownbrew.schema.json +++ b/ownbrew.schema.json @@ -36,11 +36,7 @@ "additionalProperties": false, "type": "object", "required": [ - "version", - "binDir", - "tapDir", - "tempDir", - "cellarDir" + "version" ], "description": "Ownbrew configuration" }, @@ -71,13 +67,21 @@ "version": { "type": "string", "description": "Version of the package to install" + }, + "tags": { + "items": { + "type": "string" + }, + "type": "array", + "description": "List of tags" } }, "additionalProperties": false, "type": "object", "required": [ "tap", - "version" + "version", + "tags" ] } } diff --git a/pkg/config/config.go b/pkg/config/config.go index 476da35..2874537 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,13 +5,13 @@ type Config struct { // Config version Version string `json:"version" yaml:"version"` // Path to the executable symlinks - BinDir string `json:"binDir" yaml:"binDir"` + BinDir string `json:"binDir,omitempty" yaml:"binDir,omitempty"` // Path to your project taps - TapDir string `json:"tapDir" yaml:"tapDir"` + TapDir string `json:"tapDir,omitempty" yaml:"tapDir,omitempty"` // Path for the downloaded sources - TempDir string `json:"tempDir" yaml:"tempDir"` + TempDir string `json:"tempDir,omitempty" yaml:"tempDir,omitempty"` // Path to the versioned executables - CellarDir string `json:"cellarDir" yaml:"cellarDir"` + CellarDir string `json:"cellarDir,omitempty" yaml:"cellarDir,omitempty"` // List of packages that should be installed Packages []Package `json:"packages,omitempty" yaml:"packages,omitempty"` } diff --git a/pkg/config/package.go b/pkg/config/package.go index 6662a09..7f0500a 100644 --- a/pkg/config/package.go +++ b/pkg/config/package.go @@ -16,6 +16,8 @@ type Package struct { Args []string `json:"args,omitempty" yaml:"args,omitempty"` // Version of the package to install Version string `json:"version" yaml:"version"` + // List of tags + Tags []string `json:"tags" yaml:"tags"` } func (c Package) AllNames() []string { diff --git a/pkg/config/version.go b/pkg/config/version.go index 0f29af7..15abda1 100644 --- a/pkg/config/version.go +++ b/pkg/config/version.go @@ -1,3 +1,3 @@ package config -const Version = "1.0" +const Version = "1.1" diff --git a/pkg/ownbrew/ownbrew.go b/pkg/ownbrew/ownbrew.go index 648d94e..f86d0c5 100644 --- a/pkg/ownbrew/ownbrew.go +++ b/pkg/ownbrew/ownbrew.go @@ -12,6 +12,7 @@ import ( "path" "path/filepath" "runtime" + "slices" "strings" "time" @@ -120,11 +121,34 @@ func New(l *slog.Logger, opts ...Option) (*Ownbrew, error) { // ~ Public methods // ------------------------------------------------------------------------------------------------ -func (o *Ownbrew) Install(ctx context.Context) error { +func (o *Ownbrew) Install(ctx context.Context, tags ...string) error { o.l.Debug("install:", "os", runtime.GOOS, "arch", runtime.GOARCH) for _, pkg := range o.packages { var install bool + + if len(tags) > 0 { + if len(pkg.Tags) == 0 { + continue + } + var include bool + for _, tag := range tags { + if !strings.HasPrefix(tag, "-") && slices.Contains(pkg.Tags, tag) { + include = true + break + } + } + for _, tag := range tags { + if strings.HasPrefix(tag, "-") && slices.Contains(pkg.Tags, strings.TrimPrefix(tag, "-")) { + include = false + break + } + } + if !include { + continue + } + } + cellarFilenames, err := o.cellarFilenames(pkg) if err != nil { return errors.Wrap(err, "failed to retrieve cellar filename for package") @@ -249,7 +273,7 @@ func (o *Ownbrew) installLocal(ctx context.Context, pkg config.Package) error { if err != nil { return errors.Wrap(err, "failed to read file") } - util.Code(o.l, filename, string(value), "sh") + fmt.Println(util.Highlight(string(value), "sh")) return nil } @@ -304,7 +328,7 @@ func (o *Ownbrew) installRemote(ctx context.Context, pkg config.Package) error { } if o.dry { - util.Code(o.l, url, string(script), "sh") + fmt.Println(util.Highlight(string(script), "sh")) return nil } @@ -326,7 +350,7 @@ func (o *Ownbrew) installRemote(ctx context.Context, pkg config.Package) error { cmd.Stderr = os.Stderr } if err := cmd.Run(); err != nil { - util.Code(o.l, url, string(script), "sh") + fmt.Println(util.Highlight(string(script), "sh")) return errors.Wrap(err, "failed to install") } diff --git a/pkg/util/code.go b/pkg/util/code.go deleted file mode 100644 index 06a31f0..0000000 --- a/pkg/util/code.go +++ /dev/null @@ -1,19 +0,0 @@ -package util - -import ( - "fmt" - "log/slog" - "os" - "strings" - - "github.com/alecthomas/chroma/quick" -) - -func Code(l *slog.Logger, title, code, lexer string) { - border := strings.Repeat("-", 80) - l.Info(fmt.Sprintf("\n%s\n%s\n%s", border, title, border)) - if err := quick.Highlight(os.Stdout, code, lexer, "terminal", "monokai"); err != nil { - l.Debug(err.Error()) - fmt.Println(code) - } -} diff --git a/pkg/util/highlight.go b/pkg/util/highlight.go index 1ecc76b..fd730cd 100644 --- a/pkg/util/highlight.go +++ b/pkg/util/highlight.go @@ -11,13 +11,13 @@ import ( "github.com/pterm/pterm" ) -func Highlight(source string) string { +func Highlight(source, lexer string) string { out := &numberWriter{ w: bytes.NewBufferString(""), currentLine: 1, } // Determine lexer. - l := lexers.Get("yaml") + l := lexers.Get(lexer) if l == nil { l = lexers.Analyse(source) }