Skip to content

Commit

Permalink
feat: add support for excluding OS
Browse files Browse the repository at this point in the history
Allows the user to specify a list of excluded OS's for a given release.
Which is particularly useful for avoiding errors when repos don't
support a specific OS.

Signed-off-by: Aaron U'Ren <aauren@gmail.com>
  • Loading branch information
aauren committed Oct 1, 2024
1 parent 04b63ec commit a76f74e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ These options can be set per release
| version | pin to a specific release version |
| postcommands | see [post commands](../docs/postcommands.md)|
| postonly | only run [post commands](../docs/postcommands.md) after we have checked for new versions. This allows binman to trigger apt/yum/brew or something like that |
| excludeos | list of Operating Systems to exclude this release from, useful when you know there are certain OS's that a specific repo doesn't support so you don't get an error |

## Binman Config subcommand

Expand Down
10 changes: 7 additions & 3 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ func Main(bm *BMConfig) error {
continue
}

// Todo create an error here and use errors.Is
if msg.Err.Error() == "Noupdate" {
switch msg.Err.(type) {
case *NoUpdateError:
output["Up to Date"] = append(output["Up to Date"], msg)
continue
case *ExcludeError:
continue
default:
// Todo create an error here and use errors.Is
output["Error"] = append(output["Error"], msg)
}
output["Error"] = append(output["Error"], msg)
}

bm.OutputOptions.SendSpin(fmt.Sprintf("spinstop%s", SetStopMessage(output)))
Expand Down
21 changes: 15 additions & 6 deletions pkg/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package binman
import (
"fmt"
"reflect"
"runtime"
"sync"

db "github.com/rjbrown57/binman/pkg/db"
Expand Down Expand Up @@ -33,12 +34,18 @@ func (r *BinmanRelease) runActions() error {
for _, task := range r.actions {
log.Debugf("Executing %s for %s", reflect.TypeOf(task), r.Repo)
err = task.execute()
if err != nil && err.Error() == "Noupdate" {
log.Debugf("%s(%s) is up to date", r.Repo, r.Version)
return err
} else if err != nil {
log.Debugf("Unable to complete action %s : %v", reflect.TypeOf(task), err)
return err
if err != nil {
switch err.(type) {
case *ExcludeError:
log.Debugf("%s is excluded for OS (%s), skipping", r.Repo, runtime.GOOS)
return err
case *NoUpdateError:
log.Debugf("%s(%s) is up to date", r.Repo, r.Version)
return err
default:
log.Debugf("Unable to complete action %s : %v", reflect.TypeOf(task), err)
return err
}
}
}

Expand All @@ -50,6 +57,8 @@ func (r *BinmanRelease) setPreActions(releasePath string, binPath string) []Acti

var actions []Action

actions = append(actions, r.AddReleaseExcludeAction())

switch r.source.Apitype {
case "gitlab":
glClient := gl.GetGLClient(r.source.URL, r.source.Tokenvar)
Expand Down
12 changes: 6 additions & 6 deletions pkg/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,33 @@ func TestSetPreActions(t *testing.T) {
{
"relwithoutpublish",
relWithOutPublish.setPreActions("/tmp/", "/tmp/"),
[]string{"*binman.GetGHReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGHReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
{
// this release has a preset publish path this means it's a binman get and we don't need to use releasestatusaction
"relWithPublish",
relWithPublish.setPreActions("/tmp/", "/tmp/"),
[]string{"*binman.GetGHReleaseAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGHReleaseAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
{
"relQueryByTag",
relQueryByTag.setPreActions("/tmp/", "/tmp/"),
[]string{"*binman.GetGHReleaseAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGHReleaseAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
{
"relPostOnly",
relPostOnly.setPreActions("/tmp/", "/tmp/"),
[]string{"*binman.GetGHReleaseAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGHReleaseAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
{
"relExternalUrl",
relExternalUrl.setPreActions("/tmp/", "/tmp/"),
[]string{"*binman.GetGHReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGHReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
{
"relGLBasic",
relGLBasic.setPreActions("/tmp/", "/tmp"),
[]string{"*binman.GetGLReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
[]string{"*binman.ReleaseExcludeAction", "*binman.GetGLReleaseAction", "*binman.ReleaseStatusAction", "*binman.SetUrlAction", "*binman.SetArtifactPathAction", "*binman.SetPostActions"},
},
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/binman.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ func goSyncRepo(rel BinmanRelease, c chan<- BinmanMsg, wg *sync.WaitGroup) {

for rel.actions != nil {
if err = rel.runActions(); err != nil {
switch err.Error() {
case "Noupdate":
switch err.(type) {
case *NoUpdateError:
c <- BinmanMsg{Rel: rel, Err: err}
return
case *ExcludeError:
c <- BinmanMsg{Rel: rel, Err: err}
return
default:
Expand Down
19 changes: 19 additions & 0 deletions pkg/binmanRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ var (
ErrNoVersionsFound = errors.New("No Versions detected for release")
)

type NoUpdateError struct {
RepoName string
Version string
}

func (e *NoUpdateError) Error() string {
return fmt.Sprintf("%s is already at %s version, nothing to do here", e.RepoName, e.Version)
}

type ExcludeError struct {
RepoName string
Criteria string
}

func (e *ExcludeError) Error() string {
return fmt.Sprintf("%s was excluded from consideration because: %s", e.RepoName, e.Criteria)
}

// BinmanRelease contains info on specifc releases to hunt for
type BinmanRelease struct {
Os string `yaml:"os,omitempty"`
Expand All @@ -44,6 +62,7 @@ type BinmanRelease struct {
SourceIdentifier string `yaml:"source,omitempty"` // Allow setting of source individually
PublishPath string `yaml:"publishpath,omitempty"` // Path Release will be set up at. Typically only set by set commands or library use.
ArtifactPath string `yaml:"-"` // Will be set by BinmanRelease.setPaths. This is the source path for the link aka the executable binary
ExcludeOs []string `yaml:"excludeos,omitempty"` // Allows excluding certain OS's because we know that we'll never have releases for this OS

createdAtTime int64 // Unix time that release was created at
metric *prometheus.GaugeVec
Expand Down
30 changes: 29 additions & 1 deletion pkg/preactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"

"github.com/google/go-github/v50/github"
"github.com/rjbrown57/binman/pkg/gh"
Expand All @@ -16,6 +17,30 @@ import (
"github.com/xanzy/go-gitlab"
)

type ReleaseExcludeAction struct {
r *BinmanRelease
}

func (r *BinmanRelease) AddReleaseExcludeAction() Action {
return &ReleaseExcludeAction{
r,
}
}

func (action *ReleaseExcludeAction) execute() error {
if len(action.r.ExcludeOs) > 0 {
for _, os := range action.r.ExcludeOs {
if os == runtime.GOOS {
return &ExcludeError{
RepoName: action.r.Repo,
Criteria: fmt.Sprintf("OS %s matches an excluded OS", os),
}
}
}
}
return nil
}

type ReleaseStatusAction struct {
r *BinmanRelease
releasePath string
Expand Down Expand Up @@ -47,7 +72,10 @@ func (action *ReleaseStatusAction) execute() error {
switch err {
case nil:
// TODO: Use a pre-defined error variable here
return fmt.Errorf("%s", "Noupdate")
return &NoUpdateError{
RepoName: action.r.Repo,
Version: action.r.Version,
}
default:
if errors.Is(err, fs.ErrNotExist) {
return nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/preactions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package binman

import (
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -45,7 +46,8 @@ func TestReleaseStatusAction(t *testing.T) {
t.Fatalf("Expected no error, got %s", err)
}

if err = rNoUpdate.AddReleaseStatusAction(d).execute(); err != nil && err.Error() != "Noupdate" {
noUpErr := &NoUpdateError{}
if err = rNoUpdate.AddReleaseStatusAction(d).execute(); err != nil && !errors.As(err, &noUpErr) {
t.Fatalf("Expected no error, got %s", err)
}

Expand Down

0 comments on commit a76f74e

Please sign in to comment.