Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/go_modules-f1f754ea6b
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf authored Jun 25, 2024
2 parents 211f758 + 2a23543 commit c67fda9
Show file tree
Hide file tree
Showing 135 changed files with 4,047 additions and 750 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ go.mod.bak
dist/
.cache_ggshield
.DS_Store
.project
.classpath
.settings/
*.exe
.secrets
*.log
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ linters:
- sqlclosecheck
- wastedassign

# Disabled because of deprecation
- execinquery

linters-settings:
gci:
sections:
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ COMMIT := $(shell git rev-parse --verify
CONTROLLER_TOOLS_VERSION ?= v0.14.0
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen

PLATFORMS = windows/amd64 darwin/arm64 darwin/amd64 linux/amd64 linux/arm64

CREDS ?=
OCM := go run $(REPO_ROOT)/cmds/ocm $(CREDS)
CTF_TYPE ?= tgz
Expand All @@ -36,17 +38,24 @@ build: ${SOURCES}
CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o bin/ocm ./cmds/ocm
CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o bin/helminstaller ./cmds/helminstaller
CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o bin/demo ./cmds/demoplugin
CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o bin/cliplugin ./cmds/cliplugin
CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o bin/ecrplugin ./cmds/ecrplugin


build-platforms: $(GEN)/.exists $(SOURCES)
@for i in $(PLATFORMS); do \
echo GOARCH=$$(basename $$i) GOOS=$$(dirname $$i); \
GOARCH=$$(basename $$i) GOOS=$$(dirname $$i) CGO_ENABLED=0 go build ./cmds/ocm ./cmds/helminstaller ./cmds/ecrplugin; \
done

.PHONY: install-requirements
install-requirements:
@make -C hack $@

.PHONY: prepare
prepare: generate format generate-deepcopy build test check

EFFECTIVE_DIRECTORIES := $(REPO_ROOT)/cmds/ocm/... $(REPO_ROOT)/cmds/helminstaller/... $(REPO_ROOT)/cmds/ecrplugin/... $(REPO_ROOT)/cmds/demoplugin/... $(REPO_ROOT)/examples/... $(REPO_ROOT)/pkg/...
EFFECTIVE_DIRECTORIES := $(REPO_ROOT)/cmds/ocm/... $(REPO_ROOT)/cmds/helminstaller/... $(REPO_ROOT)/cmds/ecrplugin/... $(REPO_ROOT)/cmds/demoplugin/... $(REPO_ROOT)/cmds/cliplugin/... $(REPO_ROOT)/examples/... $(REPO_ROOT)/cmds/subcmdplugin/... $(REPO_ROOT)/pkg/...

.PHONY: format
format:
Expand Down
110 changes: 110 additions & 0 deletions cmds/cliplugin/cmds/check/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package check

import (
"fmt"
"strconv"
"strings"
"time"

// bind OCM configuration.
_ "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/config"

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/logging"
"github.com/spf13/cobra"

"github.com/open-component-model/ocm/pkg/contexts/ocm"
)

const Name = "check"

var log = logging.DynamicLogger(logging.DefaultContext(), logging.NewRealm("cliplugin/rhabarber"))

func New() *cobra.Command {
cmd := &command{}
c := &cobra.Command{
Use: Name + " <options>",
Short: "determine whether we are in rhubarb season",
Long: "The rhubarb season is between march and april.",
RunE: cmd.Run,
}

c.Flags().StringVarP(&cmd.date, "date", "d", "", "the date to ask for (MM/DD)")
return c
}

type command struct {
date string
}

var months = map[string]int{
"jan": 1,
"feb": 2,
"mär": 3, "mar": 3,
"apr": 4,
"mai": 5, "may": 5,
"jun": 6,
"jul": 7,
"aug": 8,
"sep": 9,
"okt": 10, "oct": 10,
"nov": 11,
"dez": 12, "dec": 12,
}

func (c *command) Run(cmd *cobra.Command, args []string) error {
season := Season{
Start: "mar/1",
End: "apr/30",
}

ctx := ocm.FromContext(cmd.Context())
ctx.ConfigContext().ApplyTo(0, &season)

start, err := ParseDate(season.Start)
if err != nil {
return errors.Wrapf(err, "invalid season start")
}

end, err := ParseDate(season.End)
if err != nil {
return errors.Wrapf(err, "invalid season end")
}
end = end.Add(time.Hour * 24)

d := time.Now()
if c.date != "" {
d, err = ParseDate(c.date)
if err != nil {
return err
}
}

log.Debug("testing rhabarb season", "date", d.String())
if d.After(start) && d.Before(end) {
fmt.Printf("Yeah, it's rhabarb season - happy rhabarbing!\n")
} else {
fmt.Printf("Sorry, but you have to stay hungry.\n")
}
return nil
}

func ParseDate(s string) (time.Time, error) {
parts := strings.Split(s, "/")
if len(parts) != 2 {
return time.Time{}, fmt.Errorf("invalid date, expected MM/DD")
}
month, err := strconv.Atoi(parts[0])
if err != nil {
month = months[strings.ToLower(parts[0])]
if month == 0 {
return time.Time{}, errors.Wrapf(err, "invalid month")
}
}
day, err := strconv.Atoi(parts[1])
if err != nil {
return time.Time{}, errors.Wrapf(err, "invalid day")
}

return time.Date(time.Now().Year(), time.Month(month), day, 0, 0, 0, 0, time.Local), nil //nolint:gosmopolitan // yes
}
70 changes: 70 additions & 0 deletions cmds/cliplugin/cmds/check/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package check

import (
cfgcpi "github.com/open-component-model/ocm/pkg/contexts/config/cpi"
"github.com/open-component-model/ocm/pkg/runtime"
)

const (
ConfigType = "rhabarber.config.acme.org"
ConfigTypeV1 = ConfigType + runtime.VersionSeparator + "v1"
)

var (
RhabarberType cfgcpi.ConfigType
RhabarberTypeV1 cfgcpi.ConfigType
)

func init() {
RhabarberType = cfgcpi.NewConfigType[*Config](ConfigType, usage)
cfgcpi.RegisterConfigType(RhabarberType)
RhabarberTypeV1 = cfgcpi.NewConfigType[*Config](ConfigTypeV1, "")

cfgcpi.RegisterConfigType(RhabarberTypeV1)
}

type Season struct {
Start string `json:"start"`
End string `json:"end"`
}

// Config describes a memory based repository interface.
type Config struct {
runtime.ObjectVersionedType `json:",inline"`
Season `json:",inline"`
}

// NewConfig creates a new memory ConfigSpec.
func NewConfig(start, end string) *Config {
return &Config{
ObjectVersionedType: runtime.NewVersionedTypedObject(ConfigType),
Season: Season{
Start: start,
End: end,
},
}
}

func (a *Config) GetType() string {
return ConfigType
}

func (a *Config) ApplyTo(ctx cfgcpi.Context, target interface{}) error {
t, ok := target.(*Season)
if !ok {
return cfgcpi.ErrNoContext(ConfigType)
}

*t = a.Season
return nil
}

const usage = `
The config type <code>` + ConfigType + `</code> can be used to configure the season for rhubarb:
<pre>
type: ` + ConfigType + `
start: mar/1
end: apr/30
</pre>
`
154 changes: 154 additions & 0 deletions cmds/cliplugin/cmds/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//go:build unix

package cmds_test

import (
"bytes"

. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/cmds/ocm/testhelper"
. "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/testutils"

"github.com/mandelsoft/logging/logrusl"
"github.com/mandelsoft/logging/utils"

"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/version"
)

const KIND = "rhubarb"

var _ = Describe("cliplugin", func() {
Context("lib", func() {
var env *TestEnv
var plugins TempPluginDir

BeforeEach(func() {
env = NewTestEnv(TestData())
plugins = Must(ConfigureTestPlugins(env, "testdata/plugins"))

registry := plugincacheattr.Get(env)
// Expect(registration.RegisterExtensions(env)).To(Succeed())
p := registry.Get("cliplugin")
Expect(p).NotTo(BeNil())
Expect(p.Error()).To(Equal(""))
})

AfterEach(func() {
plugins.Cleanup()
env.Cleanup()
})

It("run plugin based ocm command", func() {
var buf bytes.Buffer

MustBeSuccessful(env.CatchOutput(&buf).Execute("--config", "testdata/config.yaml", "check", KIND, "-d", "jul/10"))

Expect("\n" + buf.String()).To(Equal(`
Yeah, it's rhabarb season - happy rhabarbing!
`))
})

It("runs plugin based ocm command with log", func() {
var stdout bytes.Buffer
var stdlog bytes.Buffer

lctx := env.OCMContext().LoggingContext()
lctx.SetBaseLogger(logrusl.WithWriter(utils.NewSyncWriter(&stdlog)).NewLogr())
MustBeSuccessful(env.CatchOutput(&stdout).
Execute("--config", "testdata/logcfg.yaml", "check", KIND, "-d", "jul/10"))

Expect("\n" + stdout.String()).To(Equal(`
Yeah, it's rhabarb season - happy rhabarbing!
`))
// {"date":".*","level":"debug","msg":"testing rhabarb season","realm":"cliplugin/rhabarber","time":".*"}
Expect(stdlog.String()).To(StringMatchTrimmedWithContext(`
[^ ]* debug \[cliplugin/rhabarber\] "testing rhabarb season" date="[^"]*"
`))
})

It("fails for undeclared config", func() {
var buf bytes.Buffer

Expect(env.CatchOutput(&buf).Execute("--config", "testdata/err.yaml", "check", KIND, "-d", "jul/10")).To(
MatchError(`config type "err.config.acme.org" is unknown`))
})

It("shows command help", func() {
var buf bytes.Buffer

MustBeSuccessful(env.CatchOutput(&buf).Execute("check", KIND, "--help"))
Expect(buf.String()).To(StringEqualTrimmedWithContext(`
ocm check rhubarb — Determine Whether We Are In Rhubarb Season
Synopsis:
ocm check rhubarb <options>
Flags:
-d, --date string the date to ask for (MM/DD)
-h, --help help for check
Description:
The rhubarb season is between march and april.
`))
})

It("shows command help from main command", func() {
var buf bytes.Buffer

MustBeSuccessful(env.CatchOutput(&buf).Execute("help", "check", KIND))
Expect(buf.String()).To(StringEqualTrimmedWithContext(`
ocm check rhubarb — Determine Whether We Are In Rhubarb Season
Synopsis:
ocm check rhubarb <options>
Flags:
-d, --date string the date to ask for (MM/DD)
-h, --help help for check
Description:
The rhubarb season is between march and april.
`))
})

It("describe", func() {
var buf bytes.Buffer

MustBeSuccessful(env.CatchOutput(&buf).Execute("describe", "plugin", "cliplugin"))
Expect(buf.String()).To(StringEqualTrimmedWithContext(`
Plugin Name: cliplugin
Plugin Version: ` + version.Get().String() + `
Path: ` + plugins.Path() + `/cliplugin
Status: valid
Source: manually installed
Capabilities: CLI Commands, Config Types
Description:
The plugin offers the check command for object type rhubarb to check the rhubarb season.
CLI Extensions:
- Name: check (determine whether we are in rhubarb season)
Object: rhubarb
Verb: check
Usage: check rhubarb <options>
The rhubarb season is between march and april.
Config Types for CLI Command Extensions:
- Name: rhabarber.config.acme.org
The config type «rhabarber.config.acme.org» can be used to configure the season for rhubarb:
type: rhabarber.config.acme.org
start: mar/1
end: apr/30
Versions:
- Version: v1
*** found 1 plugins
`))
})
})
})
Loading

0 comments on commit c67fda9

Please sign in to comment.