Skip to content

Commit

Permalink
Merge pull request #3 from foomo/init
Browse files Browse the repository at this point in the history
feat: add init and tags
  • Loading branch information
franklinkim authored Oct 26, 2024
2 parents 33c61f1 + 2a57bda commit a2f5caf
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .ownbrew.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# yaml-language-server: $schema=ownbrew.schema.json
version: '1.0'
version: '1.1'

binDir: "bin"
tapDir: ".ownbrew/tap"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
59 changes: 59 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 8 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
16 changes: 10 additions & 6 deletions ownbrew.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
"additionalProperties": false,
"type": "object",
"required": [
"version",
"binDir",
"tapDir",
"tempDir",
"cellarDir"
"version"
],
"description": "Ownbrew configuration"
},
Expand Down Expand Up @@ -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"
]
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 2 additions & 0 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package config

const Version = "1.0"
const Version = "1.1"
32 changes: 28 additions & 4 deletions pkg/ownbrew/ownbrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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")
}

Expand Down
19 changes: 0 additions & 19 deletions pkg/util/code.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/util/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a2f5caf

Please sign in to comment.